#!/bin/zsh
_nginx_ver=1.6.0
_nginx_ver_nopool=1.7.2
_nginx_ver_old=1.0.1
_nginx_ver_very_old=0.8.40

OPTIONS=(!strip debug docs libtool staticlibs emptydirs zipman purge !upx)
if [[ -z $NO_DEBUG ]]; then
    CFLAGS="-ggdb -O0 -Wall"
fi
if [[ $NO_POOL == 1 ]]; then
  _nginx_ver=$_nginx_ver_nopool
elif [[ $NGINX_OLDVERSION == 1 ]]; then
  _nginx_ver=$_nginx_ver_old
elif [[ $NGINX_VERYOLDVERSION == 1 ]]; then
  _nginx_ver=$_nginx_ver_very_old
fi


PKGEXT=".pkg.tar"

_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"
_access_log="/dev/stdout"
_error_log="errors.log"
_pushmodule_dir="${startdir}/pushmodule"

_pkgver() {
  pushd "$startdir/pushmodule" >/dev/null
  printf "%s.%s.%s.%s" "$_nginx_ver" "$(git rev-parse --abbrev-ref HEAD | sed -r 's/-/_/g')" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
  popd >/dev/null
}

pkgname=nginx-pushmodule-dev
pkgver=$(_pkgver)
pkgrel=1
pkgdesc="Nginx Push Module dev version"
arch=('i686' 'x86_64')
install=install


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_nopool}-no_pool.patch")
md5sums=('SKIP'
         '1fe7a3ca0773ce13f9f92e239a99f8b9'
         'ab1eb640c978536c1dad16674d6b3c3c'
         '62d494d23aef31d0b867161f9fffa6eb'
         'SKIP')

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 [[ $NGX_SLAB_PATCH == 1 ]]; then
    echo "using the ngx_slab patch to fix large alloc/frees"
    patch -p1 < "${startdir}/ngx_slab.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-log-path=${_access_log}
    --error-log-path=${_error_log}
    --user=${_user}
    --group=${_group}
    --with-debug
    --add-module=${_pushmodule_dir})

  if [[ -z $NGINX_VERYOLDVERSION ]]; then
    CONFIGURE+=( "--http-scgi-temp-path=${_tmp_path}scgi_temp" )
  fi

  if [[ $CC == *clang* ]]; then
    #not a valid clang parameter
    CFLAGS="${CFLAGS/-fvar-tracking-assignments/}"
  elif [[ $CC == "cc" ]] || [[ $CC == "gcc" ]] || [[ -z $CC ]]; then
    CFLAGS="-fdiagnostics-color=always $CFLAGS"
  fi

  export CCACHE_CPP2=yes
  if ! [[ -z $CLANG_ANALYZER ]]; then
    scan-build -o "$CLANG_ANALYZER" ./configure ${CONFIGURE[@]}
    scan-build -o "$CLANG_ANALYZER" make
  elif ! [[ -z $CONTINUE ]]; then
    make $REMAKE
  else
    ./configure ${CONFIGURE[@]}
    make
  fi
}

package() {
  echo "make install >/dev/null"
  cd "${srcdir}/nginx-${_nginx_ver}"
  make DESTDIR="$pkgdir/" install >/dev/null
  
  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"
  if [[ -z $NGINX_VERYOLDVERSION ]]; then
    install -D -m644 "man/nginx.8" "${pkgdir}/usr/share/man/man8/nginx.8"
  fi
}
