#!/bin/sh

check() {
    if [ -z "$1" ]; then echo "ERROR: $2" 1>&2; exit 1; fi
}

BUILD_CACHE='/tmp/cloudpub'
TARGET=$1

check "$TARGET"  "Target not set"

set +e

mkdir -p $BUILD_CACHE
cd $BUILD_CACHE

if [ ! -d nginx-1.0.11 ] ; then
    echo "Downloading nginx"
    wget -q -c http://nginx.org/download/nginx-1.0.11.tar.gz
    tar xzf nginx-1.0.11.tar.gz
fi

if [ ! -f "$TARGET/sbin/nginx" ] ; then
    echo "Building nginx"
    cd nginx-1.0.11
    ./configure --prefix=$TARGET --without-http_rewrite_module
    make
    make install
fi
