#!/bin/zsh
OPTIONS=(!strip debug docs libtool staticlibs emptydirs zipman purge !upx)
if [[ -z $NO_DEBUG ]]; then
    CFLAGS="-ggdb -O0 -Wall"
fi

_user="http"
_group="http"
_doc_root="/usr/share/${_pkgname}/http"
_sysconf_path="etc"
_conf_path="${_sysconf_path}/${_pkgname}"
_tmp_path="/tmp/"
_pid_path="/run"
_lock_path="/var/lock"
_log_path="/var/log/${_pkgname}"
_pushmodule_dir="${startdir}/pushmodule"

pkgname=nginx-pushmodule-dev
_nginx_ver=1.4.3
pkgver=1.4.3.master.412.908425f
pkgrel=1
pkgdesc="Nginx Push Module dev version"
arch=('i686' 'x86_64')
install=install
pkgver() {
  cd "$startdir/pushmodule"
  printf "%s.%s.%s.%s" "$_nginx_ver" "$(git rev-parse --abbrev-ref HEAD)" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

depends=('pcre' 'zlib' 'openssl')
url="http://nginx.org"
license=('custom')
conflicts=('nginx' 'nginx-unstable' 'nginx-svn' 'nginx-devel' 'nginx-custom-dev' 'nginx-custom-slact') 
provides=('nginx' 'nginx-custom')
backup=("${_conf_path}/conf/nginx.conf"
  "${_conf_path}/conf/koi-win"
  "${_conf_path}/conf/koi-utf"
  "${_conf_path}/conf/win-utf"
  "${_conf_path}/conf/mime.types"
  "${_conf_path}/conf/fastcgi.conf"
  "${_conf_path}/conf/fastcgi_params"
  "${_conf_path}/conf/scgi_params"
  "${_conf_path}/conf/uwsgi_params"
  "etc/logrotate.d/nginx")
_user=http
_group=http

source=("http://nginx.org/download/nginx-${_nginx_ver}.tar.gz"
  "nginx.conf"
  "nginx.logrotate"
  "nginx.service"
  "https://raw.github.com/shrimp/no-pool-nginx/master/nginx-${_nginx_ver}-no_pool.patch")
md5sums=('4740dad2f0ed7f5fa16e8caaa8a9438e'
         '1fe7a3ca0773ce13f9f92e239a99f8b9'
         'ab1eb640c978536c1dad16674d6b3c3c'
         '62d494d23aef31d0b867161f9fffa6eb'
         '7dacbc03562d496d556d5b3e954cd384')


build() {
  local _src_dir="${srcdir}/nginx-$_nginx_ver"
  ln -sf $_src_dir $srcdir/nginx
  local _build_dir="${_src_dir}/objs"
  cd $_src_dir

  if [[ $NO_POOL == 1 ]]; then
    echo "using the no-pool patch"
    patch -p1 < "${srcdir}/nginx-${_nginx_ver}-no_pool.patch"
  fi
  if [[ $MUDFLAP == 1 ]]; then
    export CFLAGS="$CFLAGS -fmudflap"
  fi

  CONFIGURE="--prefix=/${_conf_path} \
    --sbin-path=/usr/bin/nginx \
    --pid-path=${_pid_path}/nginx.pid \
    --lock-path=${_pid_path}/nginx.lock \
    --http-client-body-temp-path=${_tmp_path}/client_body_temp \
    --http-proxy-temp-path=${_tmp_path}/proxy_temp \
    --http-fastcgi-temp-path=${_tmp_path}/fastcgi_temp \
    --http-uwsgi-temp-path=${_tmp_path}/uwsgi_temp \
    --http-scgi-temp-path=${_tmp_path}scgi_temp \
    --http-log-path=${_log_path}/access.log \
    --error-log-path=${_log_path}/error.log \
    --user=${_user} \
    --group=${_group} \
    --with-debug \
    --add-module=${_pushmodule_dir}"

  if ! [[ -z $CONTINUE ]]; then
    ./configure $CONFIGURE
  elif ! [[ -z $CLANG_ANALYZER ]]; then
    scan-build -o "$CLANG_ANALYZER" ./configure $CONFIGURE
    scan-build -o "$CLANG_ANALYZER" make
  else
    ./configure $CONFIGURE
    make
  fi
}

package() {
  echo "pkgdir is $pkgdir"
  cd "${srcdir}/nginx-${_nginx_ver}"
  make DESTDIR="$pkgdir/" install
  
  sed -i -e "s/\<user\s\+\w\+;/user $_user;/g" $pkgdir/$_conf_path/conf/nginx.conf
  
  install -d "${pkgdir}/${_tmp_path}"
  install -d "${pkgdir}/${_doc_root}"
  
  mv "${pkgdir}/${_conf_path}/html/"* "${pkgdir}/${_doc_root}"
  rm -rf "${pkgdir}/${_conf_path}/html"
  
  #install -D -m644 "${srcdir}/nginx.logrotate" "${pkgdir}/etc/logrotate.d/${_pkgname}" #no default logrotate.
  install -D -m644 "${srcdir}/nginx.conf" "${pkgdir}/etc/conf.d/nginx"
  install -D -m644 "${srcdir}/nginx.service" "${pkgdir}/lib/systemd/system/nginx.service"
  install -D -m644 "LICENSE" "${pkgdir}/usr/share/licenses/nginx/LICENSE"
  install -D -m644 "man/nginx.8" "${pkgdir}/usr/share/man/man8/nginx.8"
}
