#!/bin/zsh
_nginx_ver=1.10.1

_openssl_ver=1.0.2h
_pcre_ver=8.38
_zlib_ver=1.2.8


_nchan_tag=`git describe --abbrev=0 --tags`

OPTIONS=(!strip debug) #nchan is still young, in case something goes wrong we want good coredumps

if [[ ! -z $MTUNE_GENERIC ]]; then
  CFLAGS="${CFLAGS//mtune=native/mtune=generic}"
fi

_pkgname=nginx

_user=nginx
_group=nginx

_doc_root="/usr/share/${_pkgname}/http"
_sysconf_path="etc"
_conf_path="${_sysconf_path}/${_pkgname}"
_log_path="/var/log/nginx"
_cache_path="/var/cache/nginx"
_pid_path="/var/run"
_lock_path="/var/lock"
_access_log="/dev/stdout"
_error_log="errors.log"

pkgver() {
  echo "${_nginx_ver}.nchan${_nchan_tag:1}"
}


pkgname=nginx-nchan-static
pkgver=1.10.0.nchan0.99.14
pkgrel=1
pkgdesc="Nginx + Nchan - a flexible pub/sub server"
arch=('i686' 'x86_64')


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

source=("http://nginx.org/download/nginx-${_nginx_ver}.tar.gz"
  "http://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-${_pcre_ver}.tar.gz"
  "https://www.openssl.org/source/openssl-${_openssl_ver}.tar.gz"
  "http://zlib.net/zlib-${_zlib_ver}.tar.gz"
  "nginx.conf"
  "nginx.logrotate"
  "nginx.service"
  "git+https://github.com/slact/nchan.git#tag=${_nchan_tag}"
       )

md5sums=('088292d9caf6059ef328aa7dda332e44'
	 '8a353fe1450216b6655dfcf3561716d9'
	 '9392e65072ce4b614c1392eefc1f23d0'
	 '44d667c142d7cda120332623eab69f40'
	 '845cab784b50f1666bbf89d7435ac7af'
	 '79031b58828462dec53a9faed9ddb36a'
	 '6696dc228a567506bca3096b5197c9db'
         'SKIP')

build() {
  cd ${srcdir}/openssl-${_openssl_ver}
  #don't install_docs
  sed -i 's/^install: all install_docs install_sw$/install: all install_sw/' Makefile.org
  rm Makefile #damn, openssl...
    
  local _src_dir="${srcdir}/nginx-$_nginx_ver"
  local _build_dir="${_src_dir}/objs"
  cd $_src_dir
  
  CONFIGURE=(
    --prefix=/${_conf_path}
    --sbin-path=/usr/sbin/nginx
    --conf-path=/${_conf_path}/nginx.conf
    --error-log-path=${_log_path}/error.log
    --http-log-path=${_log_path}/access.log
    --pid-path=${_pid_path}/nginx.pid
    --lock-path=${_pid_path}/nginx.lock
    
    --http-client-body-temp-path=${_cache_path}/client_temp
    --http-proxy-temp-path=${_cache_path}/proxy_temp
    --http-fastcgi-temp-path=${_cache_path}/fastcgi_temp
    --http-uwsgi-temp-path=${_cache_path}/uwsgi_temp
    --http-scgi-temp-path=${_cache_path}/scgi_temp
    
    --user=${_user}
    --group=${_group}
    
    --with-zlib="../zlib-${_zlib_ver}"
    --with-pcre="../pcre-${_pcre_ver}"
    --with-openssl="../openssl-${_openssl_ver}"
    
    --with-http_ssl_module
    --with-http_realip_module
    --with-http_addition_module
    --with-http_sub_module
    --with-http_dav_module
    --with-http_flv_module
    --with-http_mp4_module    
    --with-http_gunzip_module
    --with-http_gzip_static_module
    --with-http_random_index_module
    --with-http_secure_link_module
    --with-http_stub_status_module
    --with-http_auth_request_module
    --with-mail
    --with-mail_ssl_module
    --with-file-aio
    --with-http_v2_module
    --with-ipv6
    --with-debug
    --with-ld-opt="-static-libgcc" #totally statically link everything
    --with-cc-opt="-static"
    --add-module="../nchan")

  CFLAGS="$CFLAGS -ggdb" #make sure debug symbols are present
  
  ./configure ${CONFIGURE[@]}

  make -j1
}

package() {
  cd "${srcdir}/nginx-${_nginx_ver}"

  mkdir -p ${pkgdir}/$_conf_path/sites-enabled/
  mkdir -p ${pkgdir}/$_conf_path/sites-available/
  mkdir -p ${pkgdir}${_cache_path}
  
  make DESTDIR="$pkgdir/" install >/dev/null
  
  sed -i -e "s/\<user\s\+\w\+;/user $_user;/g" $pkgdir/$_conf_path/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}"
  install -D -m644 "${srcdir}/nginx.conf" "${pkgdir}/etc/conf.d/${_pkgname}"
  install -D -m644 "${srcdir}/nginx.service" "${pkgdir}/lib/systemd/system/nginx.service"
  install -D -m644 "LICENSE" "${pkgdir}/usr/share/licenses/nginx/LICENSE"

}
