#!/bin/zsh
_nginx_ver_latest=1.11.3
_nginx_ver_stable=1.10.1
_nginx_ver_nopool=1.11.2
_nginx_ver_legacy=1.8.1
_nginx_ver_old=1.6.3
_nginx_ver_very_old=0.8.55

OPTIONS=(!strip debug docs libtool staticlibs emptydirs zipman purge !upx)
if [[ -z $NO_DEBUG ]]; then
  #debug build. clear cflags
  CFLAGS=" -ggdb -fvar-tracking-assignments -O$OPTIMIZE_LEVEL"
fi

CFLAGS="$CFLAGS -Wall -Wextra -Wno-unused-parameter -Wpointer-arith -Wshadow -Wnested-externs -Wsign-compare"

_include_http2=0

if [[ $NO_POOL == 1 ]]; then
  _nginx_ver=$_nginx_ver_nopool
  _include_http2=1
elif [[ $NGINX_LEGACYVERSION == 1 ]]; then
  _nginx_ver=$_nginx_ver_legacy
elif [[ $NGINX_OLDVERSION == 1 ]]; then
  _nginx_ver=$_nginx_ver_old
elif [[ $NGINX_VERYOLDVERSION == 1 ]]; then
  _nginx_ver=$_nginx_ver_very_old
elif [[ $NGINX_STABLEVERSION == 1 ]]; then
  _nginx_ver=$_nginx_ver_stable
  _include_spdy=1
else
  _nginx_ver=$_nginx_ver_latest
  _include_http2=1
fi

_pkgname="nginx"
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"
_nchan_dir="${startdir}/nchan"

_pkgver() {
  pushd "$startdir/nchan" >/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-nchan-dev
pkgver=0
pkgrel=1
pkgdesc="Nginx + Nchan dev version"
arch=('i686' 'x86_64' 'armv7h')
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

_nginx_source="http://nginx.org/download/nginx-${_nginx_ver}.tar.gz"
_no_pool_patch_source="https://raw.github.com/shrimp/no-pool-nginx/master/nginx-${_nginx_ver_nopool}-no_pool.patch"
_ngx_debug_pool_url="https://github.com/chobits/ngx_debug_pool.git"

source=("$_nginx_source"
  "nginx.conf"
  "nginx.logrotate"
  "nginx.service"
  "git+${_ngx_debug_pool_url}"
  "$_no_pool_patch_source"
  "bl.txt")

md5sums=('SKIP'
         '1fe7a3ca0773ce13f9f92e239a99f8b9'
         'ab1eb640c978536c1dad16674d6b3c3c'
         '62d494d23aef31d0b867161f9fffa6eb'
         'SKIP'
         'SKIP'
         'SKIP')

build() {
  local _src_dir="${srcdir}/nginx-$_nginx_ver"
  ln -sfT "$_src_dir" "$srcdir/nginx"
  ln -sfT "$_nchan_dir" "$srcdir/nchan"
  local _build_dir="${_src_dir}/objs"
  cd "$srcdir/nginx"
  
  if [[ -z $CONTINUE ]] && [[ -z $NO_EXTRACT_SOURCE ]]; then
    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 [[ $NGX_DEBUG_POOL == 1 ]]; then
      echo "patch nginx to debug pools"
      patch -p1 < "${srcdir}/ngx_debug_pool/debug_pool.patch"
    fi
  fi

  if [[ $MUDFLAP == 1 ]]; then
    export CFLAGS="$CFLAGS -fmudflap"
  fi

  CFLAGS="${CFLAGS/-Werror/}" #no warning-as-error
  
  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}
    --with-http_ssl_module
  )

  if [[ $DYNAMIC == 1 ]]; then
    CONFIGURE+=( --add-dynamic-module=../nchan )
  else
    CONFIGURE+=( --add-module=../nchan )
  fi

  if [[ $CONFIGURE_WITH_DEBUG == 1 ]]; then
    CONFIGURE+=( "--with-debug" )
  fi
  
  if [[ -z $NO_NGINX_USER ]]; then
    CONFIGURE+=( "--user=${_user}" "--group=${_group}" )
  fi
    
  if [[ $_include_http2 == 1 ]]; then
    CONFIGURE+=( "--with-http_v2_module" )
  fi
  
  if [[ $_include_spdy == 1 ]]; then
    CONFIGURE+=( "--with-http_spdy_module" )
  fi
    
  if [[ $NGX_DEBUG_POOL == 1 ]]; then
    CONFIGURE+=( "--add-module=../ngx_debug_pool" )
  fi
  
  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/}"
    CFLAGS="${CFLAGS/-fvar-tracking-assignments/}"
    if [[ -z $CLANG_ANALYZER ]]; then
      CFLAGS="-ferror-limit=5 $CFLAGS"
    fi
  elif [[ $CC == "cc" ]] || [[ $CC == "gcc" ]] || [[ -z $CC ]] && [[ -z $NO_GCC_COLOR ]]; then
    CFLAGS="-fdiagnostics-color=always  -Wmaybe-uninitialized $CFLAGS"
  fi
  
  echo $CFLAGS
  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
    ln -svf $srcdir/bl.txt

    if [[ ! -z $EXPLICIT_CFLAGS ]]; then
      CONFIGURE+=( "--with-cc-opt=${CFLAGS}" )
    fi
    echo "./configure ${CONFIGURE[@]}"
    ./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
}
