#!/bin/bash

mkdir /tmp/a
cd /tmp/a
rm -f /tmp/a/*

grep 'CentOS Linux release 6.' /etc/redhat-release
if [ $? -eq 0 ]; then
	echo "centos6"
	yum install -y MySQL-python cronie mysql-server
	chkconfig mysqld on
	service mysqld start
	mysqladmin -uroot password root
	wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm
	rpm -ivh nginx-release-centos-6-0.el6.ngx.noarch.rpm
	yum install -y nginx
	chkconfig nginx on
elif [ -f /etc/redhat-release ]; then
	echo "CentOS 5 is not supported."
	exit 1
else
	echo "Debian or Ubuntu"
	apt-get update
	apt-get install -y python python-mysqldb nginx mysql-server
	apt-get install -y syslog-ng
fi

rm -f /etc/logrotate.d/nginx
cat > /etc/nginx/proxy.conf <<END

 proxy_redirect off;
 proxy_set_header   Host             \$host;
 proxy_set_header   X-Real-IP        \$remote_addr;
 proxy_set_header   X-Forwarded-For  \$proxy_add_x_forwarded_for;

 client_max_body_size       50m;
 client_body_buffer_size    128k;

 proxy_connect_timeout      600;
 proxy_send_timeout         600;
 proxy_read_timeout         600;

 proxy_buffer_size          516k;
 proxy_buffers              532 516k;
 proxy_busy_buffers_size    564k;
 proxy_temp_file_write_size 564k;

 error_page 404 = http://\$host/404.html;
 error_page 502 = http://\$host/502.html;
END

wget dl.rashost.com/syncbb.py
wget dl.rashost.com/logcollector.py
cp -f syncbb.py /usr/bin
cp -f logcollector.py /usr/bin
chmod 755 /usr/bin/syncbb.py
chmod 755 /usr/bin/logcollector.py

echo '*/5 * * * * root /usr/bin/syncbb.py >> /var/log/sync.log 2>>/var/log/syncerr.log' > /etc/cron.d/syncbb
echo '13,43 * * * * root /usr/bin/logcollector.py >> /var/log/logcollector.log 2>>/var/log/logcollectorerr.log' > /etc/cron.d/logcollector

echo 'nameserver 208.67.222.222' >/etc/resolv.conf
echo 'nameserver 208.67.220.220' >>/etc/resolv.conf

/usr/bin/syncbb.py
echo "ok"
reboot

