#!/bin/sh

SERVIP=install.enix.org
URL="http://$SERVIP/"

function mkautoupdate() {
	rm mkenixconf.sh
	wget $URL/mkenixconf.sh
	source mkenixconf.sh
}

function prepare() {
	apt-get install lynx wget
	TEST=`lynx -source $URL/testfile`
	if [ "$TEST" != "test ok" ]; then
		echo Error, Abording 
		exit 1;
	fi
	echo Test Ok.
	pushd .
	cd /tmp
	mkdir install
	cd install
}

function restore() {
	popd
	rm -r /tmp/install
} 

function mkfsckfix () {
	prepare
	cat /etc/default/rcS  | sed "s/FSCKFIX=\(.*\)/FSCKFIX=yes/" > rcS.tmp
	cat rcS.tmp > /etc/default/rcS
	restore
}

function mkhdparm () {
	prepare
	apt-get install hdparm
	if [ -e /etc/init.d/local ]; then
	echo 'for t in /proc/ide/hd*; do /sbin/hdparm -c1 -d1 -m16 `echo $t | sed s-/proc/ide-/dev-g`; done' >> /etc/init.d/local
	fi
	restore
}

function mkrclocal () {
	prepare
	if ! [ -e /etc/init.d/local ]; then
	echo "#!/bin/sh" > /etc/init.d/local
	fi;
	chmod +x /etc/init.d/local
	ln -s /etc/init.d/local /etc/rc2.d/SAAlocal	
	ln -s /etc/init.d/local /etc/rc3.d/SAAlocal	
	ln -s /etc/init.d/local /etc/rc4.d/SAAlocal	
	ln -s /etc/init.d/local /etc/rc5.d/SAAlocal	
	restore
}

function mkapacheconfig () {
	prepare
	lynx -source $URL/httpd.conf > /etc/apache/httpd.conf
	lynx -source $URL/httpd.conf.virtual > /etc/apache/httpd.conf.virtual.sample
	mkdir /etc/apache/virtual
	NAME=`hostname`
	cat /etc/apache/httpd.conf.virtual.sample | sed "s/_IP_/$NAME/" > /etc/apache/virtual/$NAME
	mkdir /home/www-$NAME
	lynx -source $URL/phpmyadmin-alias.conf > /etc/apache/conf.d/phpmyadmin-alias.conf
	apt-get install phpmyadmin
	cd /home/www-$NAME
	ln -s . `hostname -i`
	apache-modconf apache enable mod_vhost_alias
	restore
}

function mkstat () {
	prepare
	apt-get install python apache logrotate webalizer file
	wget $URL/run_analog
	wget $URL/splitlog.py
	wget $URL/gendomainsplit
	chmod +x run_analog gendomainsplit splitlog.py
	mv splitlog.py gendomainsplit run_analog /usr/local/sbin
	lynx -source $URL/webalizer.conf > /etc/webalizer.conf
	lynx -source $URL/logrotate.apache > /etc/logrotate.d/apache
	echo HostName `cat /etc/hostname` >> /etc/webalizer.conf
	mkdir -p /home/www-dummy/dummy/logs
	restore
}

function mkntp () {
	prepare
	wget $URL/default.ntp-servers
	cat default.ntp-servers > /etc/default/ntp-servers
	apt-get install ntp ntpdate
	restore
}

function mksourceslist () {
	prepare
	lynx -source $URL/sources.list > /etc/apt/sources.list
	lynx -source $URL/apt-preferences > /etc/apt/preferences
	if ! grep -q debian /etc/hosts; then 
		echo "62.210.169.1	debian" >> /etc/hosts
	fi
	echo "APT::Cache-Limit 16777216;" >> /etc/apt/apt.conf
	restore
}

function mklastkernel () {
	prepare
	LASTEST=`lynx -source $URL/lastest-kernel`
	cd /usr/src
	if [ ! -e $LASTEST ]; then
		wget $URL/$LASTEST
	fi
	dpkg -i $LASTEST
	restore
}

function mkbaseapt () {
	apt-get update
	apt-get install apt-utils
	apt-get install perl
	apt-get install dpkg
	apt-get -u dist-upgrade
	apt-get install less apache mysql-server mysql-client php4 php4-gd php4-imap php4-mysql less man-db postfix ssh screen redir rsync lftp wget lynx proftpd mtr-tiny lynx logrotate jed emacs20 console-data bzip2 bc webmin-status webmin-postfix webmin-apache webmin-core webmin webmin-mysql sash 
}

function mknetwork () {
	if [ $# -lt 4 ]; then
		echo usage $0 ip1 ip2 netmask ip-gateway
		exit 1;
	fi 
	prepare
	if ! grep -q eepro100 /etc/modules; then
		echo eepro100 >> /etc/modules
	fi
	lynx -source $URL/network.interfaces | sed "s/_IP1_/$1/" | sed "s/_IP2_/$2/" | sed "s/_NETMASK_/$3/" | sed "s/_IPGW_/$4/" > /etc/network/interfaces
	cat /etc/hosts | sed "s/127.0.0.1\(.*\)/127.0.0.1 localhost/" | sort -u > hosts
	echo "$1	$HOSTNAME" >> hosts
	cat hosts | sort -u > /etc/hosts
	echo "nameserver 194.149.160.1 194.149.160.9" > /etc/resolv.conf
	cp /etc/hosts.deny .
	cat /etc/hosts.deny | grep -v "ALL: PARA" > /etc/hosts.deny
	restore
}

function mkext3 () {
	prepare
	if ! grep -q ext3 /etc/modules; then
		echo ext3 >> /etc/modules
	fi
	cat /etc/fstab | sed "s/\(.*\)ext2\(.*2[:space:]*\)/\1ext3\2/g" > fstab
	cat fstab > /etc/fstab
	for t in `cat /etc/fstab | grep ext3 | sed "s/\(^[^[:space:]]*\).*/\1/g"`; do
		tune2fs -m 0 -c 100 -j -J size=16 $t
	done
	restore 
}

function mksshconfig () {
	prepare
	if ! [ -d /root/.ssh ]; then mkdir /root/.ssh; fi
	lynx -source $URL/keys.pub >> /root/.ssh/authorized_keys
	restore
} 

function mknetsaint () {
	prepare
	apt-get install netsaint-nrpe-server netsaint-plugins
	lynx -source $URL/nrpe.cfg > /etc/nrpe.cfg
	if ! grep -q nrpe /etc/init.d/local; then
		echo /usr/sbin/nrpe -d /etc/nrpe.cfg >> /etc/init.d/local
	fi
	/usr/sbin/nrpe -d /etc/nrpe.cfg
	restore	
}

function do_all () {
	mkrclocal;
	mkhdparm;
	mkfsckfix;
	mkstat;
}
