#!/bin/sh
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin

if [ -d '/etc/nginx' ]; then
	echo; echo "Please un-install nginx first!"
	exit 0
fi
if [ $(id -u) != "0" ]; then
    echo; echo "You must be root to run this script!"
    exit 0
fi
clear
echo; echo 'Installing Jailed Shell'; echo
sed -i "s#jail=no#jail=yes#g" /usr/local/directadmin/custombuild/options.conf > /dev/null 2>&1
cd /usr/local/directadmin/custombuild/
./build all_jail
cp -f /etc/passwd /etc/passwd.backup
cp -f /etc/group /etc/group.backup
cp -f /etc/shadow /etc/shadow.backup
cp -f /etc/master.passwd /etc/master.passwd.backup
cd /usr/local/src
echo; echo 'Installing Nginx on Directadmin'; echo
wget http://icodex.org/dl/directadmin/nginxinstall.tar.gz
tar -zxf nginxinstall.tar.gz
cd nginxinstall
/bin/cp domain_create_post.sh /usr/local/directadmin/scripts/custom/domain_create_post.sh
/bin/cp domain_destroy_post.sh /usr/local/directadmin/scripts/custom/domain_destroy_post.sh
/bin/cp domain_change_post.sh /usr/local/directadmin/scripts/custom/domain_change_post.sh
/bin/cp domain_pointer_create_post.sh /usr/local/directadmin/scripts/custom/domain_pointer_create_post.sh
/bin/cp domain_pointer_destroy_post.sh /usr/local/directadmin/scripts/custom/domain_pointer_destroy_post.sh
/bin/cp subdomain_create_post.sh /usr/local/directadmin/scripts/custom/subdomain_create_post.sh
/bin/cp subdomain_destroy_post.sh /usr/local/directadmin/scripts/custom/subdomain_destroy_post.sh
/bin/cp user_create_post.sh /usr/local/directadmin/scripts/custom/user_create_post.sh
/bin/cp user_destroy_post.sh /usr/local/directadmin/scripts/custom/user_destroy_post.sh
/bin/cp user_modify_post.sh /usr/local/directadmin/scripts/custom/user_modify_post.sh
chmod 0755 /usr/local/directadmin/scripts/custom/*.sh
chown diradmin:diradmin /usr/local/directadmin/scripts/custom/*.sh
tar -zxf pcre-8.13.tar.gz
tar -zxf nginx-1.0.8.tar.gz
curdir=$(pwd)
cd nginx-1.0.8
./configure --user=apache --group=apache --with-pcre=$curdir/pcre-8.13 --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx  --conf-path=/etc/nginx/conf/nginx.conf --pid-path=/var/log/nginx/nginx.pid --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --with-http_stub_status_module
make
make install
cd ..
mkdir /etc/nginx/conf/vhosts
chmod 0600 /etc/nginx/conf/vhosts
mv /etc/nginx/conf/nginx.conf /etc/nginx/conf/nginx.conf.bak
/bin/cp nginx.init /etc/init.d/nginx
/bin/cp nginx.conf /etc/nginx/conf/nginx.conf
/bin/cp proxy.inc /etc/nginx/conf/proxy.inc
/bin/cp cache.inc /etc/nginx/conf/cache.inc
chmod 755 /etc/init.d/nginx
tar -zxf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6
/usr/sbin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
cd ..
echo '#!/bin/sh' > /usr/local/directadmin/scripts/setips.sh
echo 'cp /etc/httpd/conf/ips.conf /etc/httpd/conf/ips_hack.conf' >> /usr/local/directadmin/scripts/setips.sh
echo 'sed -i "s#:80#:81#g" /etc/httpd/conf/ips_hack.conf' >> /usr/local/directadmin/scripts/setips.sh
echo 'iplist="/usr/local/directadmin/data/admin/ip.list"' >> /usr/local/directadmin/scripts/setips.sh
echo 'cat >/etc/httpd/conf/rpaf.conf<<EOF' >> /usr/local/directadmin/scripts/setips.sh
echo 'LoadModule rpaf_module /usr/lib/apache/mod_rpaf-2.0.so' >> /usr/local/directadmin/scripts/setips.sh
echo '#Mod_rpaf settings' >> /usr/local/directadmin/scripts/setips.sh
echo 'RPAFenable On' >> /usr/local/directadmin/scripts/setips.sh
echo "RPAFproxy_ips 127.0.0.1\`awk '{printf \" %s\",\$0}' \$iplist\`" >> /usr/local/directadmin/scripts/setips.sh
echo 'RPAFsethostname On' >> /usr/local/directadmin/scripts/setips.sh
echo 'RPAFheader X-Forwarded-For' >> /usr/local/directadmin/scripts/setips.sh
echo 'EOF' >> /usr/local/directadmin/scripts/setips.sh
echo 'echo "action=httpd&value=restart" > /usr/local/directadmin/data/task.queue' >> /usr/local/directadmin/scripts/setips.sh
echo 'exit 0;' >> /usr/local/directadmin/scripts/setips.sh
chmod 755 /usr/local/directadmin/scripts/setips.sh
sed -i '/ServerAdmin/ i\Include /etc/httpd/conf/rpaf.conf' /etc/httpd/conf/httpd.conf
sed -i "s#ips.conf#ips_hack.conf#g" /etc/httpd/conf/extra/httpd-vhosts.conf
sed -i "s#:80#:81#g" /etc/httpd/conf/extra/httpd-vhosts.conf
sed -i "s#:80#:81#g" /usr/local/directadmin/data/templates/ips_virtual_host.conf

sed -i "s#:|PORT_80|#:81#g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s#:80#:81#g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host2_sub.conf
sed -i "s#:|PORT_80|#:81#g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s#:80#:81#g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host.conf
sed -i "s#:|PORT_80|#:81#g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s#:80#:81#g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host_sub.conf
sed -i "s#:|PORT_80|#:81#g" /usr/local/directadmin/data/templates/redirect_virtual_host.conf
sed -i "s#:80#:81#g" /usr/local/directadmin/data/templates/redirect_virtual_host.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/redirect_virtual_host.conf
sed -i "s#:|PORT_80|#:81#g" /usr/local/directadmin/data/templates/virtual_host2.conf
sed -i "s#:80#:81#g" /usr/local/directadmin/data/templates/virtual_host2.conf
sed -i "s# |MULTI_IP|##g" /usr/local/directadmin/data/templates/virtual_host2.conf
sed -i "s/CustomLog/#CustomLog/g" /usr/local/directadmin/data/templates/virtual_host2.conf

sed -i "s#Port 80#Port 81#g" /usr/local/directadmin/data/templates/httpd.conf
sed -i "s#Listen 80#Listen 81#g" /usr/local/directadmin/data/templates/httpd.conf
sed -i "s#Listen 80#Listen 81#g" /etc/httpd/conf/httpd.conf
/usr/local/directadmin/scripts/setips.sh
mv /etc/init.d/httpd /etc/init.d/httpd.beforenginx
/bin/cp httpd.init /etc/init.d/httpd
chmod 755 /etc/init.d/httpd
echo; echo 'Installation Complete.'
echo; echo 'Restart httpd and nginx...'
/etc/init.d/httpd restart
echo '...'
echo 'Nginx sbin is at /usr/sbin/nginx'
echo 'Nginx config file is at /etc/nginx/conf'
echo 'Apache Rpaf_Mod config file is /etc/httpd/conf/rpaf.conf'
echo 'Directadmin custom scripts is at /usr/local/directadmin/scripts/custom'
echo 'If you change the ips, please run "/usr/local/directadmin/scripts/setips.sh" after.'
echo 'Please send in your comments and/or suggestions to admin@evlit.com, Thank you!'
