#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

# Check if user is root
if [ $(id -u) != "0" ]; then
    echo "Error: You must be root to run this script, please use root to install nginx"
    exit 1
fi

clear

clear
echo "========================================================================="
echo "Nginx for Debian/Ubuntu VPS ,  Written by Vica "
echo "========================================================================="
echo "A tool to auto-compile & install Nginx(upstream+cache) on Linux "
echo ""
echo "For more information please visit http://4i4u.com/"
echo "========================================================================="
cur_dir=$(pwd)

if [ "$1" != "--help" ]; then


#set main domain name

	domain="4i4u.com"
	echo "Please input domain:"
	read -p "(Default domain: 4i4u.com):" domain
	if [ "$domain" = "" ]; then
		domain="4i4u.com"
	fi
	echo "==========================="
	echo "domain=$domain"
	echo "==========================="

#set area

	area="america"
	echo "Where are your servers located? asia,america,europe,oceania or africa "
	read -p "(Default area: america):" area
	if [ "$area" = "" ]; then
		area="america"
	fi
	echo "==========================="
	echo  "area=$area"
	echo "==========================="

#Synchronization time
rm -rf /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

apt-get update

apt-get remove -y apache*

apt-get install -y ntp
ntpdate -d cn.pool.ntp.org
date
 
apt-get install -y apt-spy
cp /etc/apt/sources.list /etc/apt/sources.list.bak
apt-spy update
apt-spy -d stable -a $area -t 5

apt-get update
apt-get -y install libpcre3 libpcre3-dev libpcrecpp0 libssl-dev zlib1g-dev build-essential libgd2-xpm-dev

	echo "============================check files=================================="
if [ -s pcre-8.10.tar.gz ]; then
  echo "pcre-8.10.tar.gz [found]"
  else
  echo "Error: pcre-8.10.tar.gz not found!!!download now......"
wget -c ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.10.tar.gz
fi

if [ -s nginx-0.8.53.tar.gz ]; then
  echo "nginx-0.8.53.tar.gz [found]"
  else
  echo "Error: nginx-0.8.53.tar.gz not found!!!download now......"
  wget -c http://nginx.org/download/nginx-0.8.53.tar.gz
fi

if [ -s ngx_cache_purge-1.2.tar.gz ]; then
  echo "ngx_cache_purge-1.2.tar.gz [found]"
  else
  echo "Error: ngx_cache_purge-1.2.tar.gz not found!!!download now......"
  wget -c http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
fi
echo "============================check files=================================="



echo "========================== nginx install ==============================="
groupadd www
useradd -g www www
mkdir -p /home/wwwroot
chmod +w /home/wwwroot
mkdir -p /var/cache/nginx/client_body_temp
mkdir +w /var/cache/nginx/client_body_temp
mkdir -p /var/cache/nginx/proxy_temp
mkdir +w /var/cache/nginx/proxy_temp
mkdir -p /home/wwwlogs
chmod 777 /home/wwwlogs
touch /home/wwwlogs/nginx_error.log

cd $cur_dir
chown -R www:www /home/wwwroot

echo "ulimit -SHn 51200" >/root/run.sh

# nginx
cd $cur_dir
tar zxvf pcre-8.10.tar.gz;tar zxvf ngx_cache_purge-1.2.tar.gz
cd pcre-8.10/
./configure
make && make install
cd ../

if [ `getconf WORD_BIT` = '32' ] && [ `getconf LONG_BIT` = '64' ] ; then
        wget -c http://launchpadlibrarian.net/33469184/libpcre3_7.8-3_amd64.deb
	dpkg -i libpcre3_7.8-3_amd64.deb
else
        wget -c http://launchpadlibrarian.net/33465361/libpcre3_7.8-3_i386.deb
        dpkg -i libpcre3_7.8-3_i386.deb
fi

cd $cur_dir
tar zxvf nginx-0.8.53.tar.gz
cd nginx-0.8.53/
./configure --user=www --group=www --add-module=../ngx_cache_purge-1.2 --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --http-client-body-temp-path=/var/cache/nginx/client_body_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp
make && make install
cd ../

cd $cur_dir
sed -i 's/4i4u.com/'$domain'/g' /usr/local/nginx/conf/nginx.conf

echo "==================== nginx install completed ==========================="

echo "/usr/local/nginx/sbin/nginx" >>/root/run.sh
chmod 777 /root/run.sh
/root/run.sh

#start up
cp /root/run.sh /etc/init.d/nginx.sh
update-rc.d nginx.sh defaults

#set timezone
cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime


echo "===================================== Check install ==================================="
clear
if [ -s /usr/local/nginx ]; then
  echo "/usr/local/nginx [found]"
  else
  echo "Error: /usr/local/nginx not found!!!"
fi

echo "========================== Check install ================================" 
fi
netstat -ntl


