#!/bin/bash
#
# /etc/init.d/serverconfig
#
### BEGIN INIT INFO
# Provides: serverconfig
# Required-Start: $remote_fs $syslog sshd
# Required-Stop:  $remote_fs $syslog
# Default-Start:  3 5
# Default-Stop:   0 1 2 6
# Description:    draht configuration + manipulation
### END INIT INFO
# 
# Note on Required-Start: It does specify the init script ordering,
# not real dependencies. Depencies have to be handled by admin
# resp. the configuration tools (s)he uses.

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num><num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
. /etc/rc.status

# First reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0 - success
# 1 - generic or unspecified error
# 2 - invalid or excess argument(s)
# 3 - unimplemented feature (e.g. "reload")
# 4 - insufficient privilege
# 5 - program is not installed
# 6 - program is not configured
# 7 - program is not running
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signalling is not supported) are
# considered a success.
PATH=/usr/local/sbin:/usr/local/bin:$PATH

case "$1" in
    start)
	echo -n "Starting serverconfig..."
	echo ""
##	echo -n "tcplog "
##	    /usr/local/sbin/tcplog
#	echo -n "icmpinfo "
#	    /usr/sbin/icmpinfo -vl
#	echo -n "hdparm "

# powersave mode. < 127 allows for spin-down.
#B=224
#B=15
#hdparm -q -B $B /dev/sda
#hdparm -q -B $B /dev/sdb
#hdparm -q -B $B /dev/sdc
#hdparm -q -B $B /dev/sde
#B=225
#hdparm -q -B $B /dev/sdd
#	hdparm -q -B $B /dev/sda
#	hdparm -q -B $B /dev/sdb
#	hdparm -q -B $B /dev/sdc

#a=8192
#	hdparm -q -a $a /dev/sda
#	hdparm -q -a $a /dev/sdb
#	hdparm -q -a $a /dev/sdc
# disk sleep:
#S=36
#	hdparm -S $S /dev/sda
#	hdparm -S $S /dev/sdb
#	hdparm -S $S /dev/sdc
#	hdparm -S $S /dev/sde
#S=120
#	hdparm -S $S /dev/sdd
#
#for i in a b c d e; do 
#
#  # hdparm -S and -B may have disabled smart
#  smartctl -s on   /dev/sd$i > /dev/null
#  smartctl -t long /dev/sd$i > /dev/null
#
#done &


##	echo -n "sensors "
##	# I2C adapter drivers
##	modprobe i2c-isa
##	# I2C chip drivers
##	modprobe via686a

#	echo -n "vm/block-layer "
##	echo 60		> /proc/sys/vm/laptop_mode		# def:0
#	echo 1 		> /proc/sys/vm/dirty_background_ratio	# def:10%
#	echo 99 	> /proc/sys/vm/dirty_ratio		# def:40%
#	echo 360000 	> /proc/sys/vm/dirty_expire_centisecs	# def:30s (3000)
#	echo 360000 	> /proc/sys/vm/dirty_writeback_centisecs # def:5s (500)
#
	echo -n "readahead "
	/usr/local/sbin/readahead > /dev/null
#

	echo -n "panic "
	echo "60" > /proc/sys/kernel/panic
#
#	echo -n "ip-filters "
#	iptables -L -nv > /dev/null 2>&1
#	modprobe nf_conntrack_ipv4
#	modprobe nf_nat_sip
#	/usr/local/sbin/ipfilters-silence

if true; then
	echo -n "IP-forwarding "
	echo "1" > /proc/sys/net/ipv4/ip_forward
	echo -n "tcp-slow-start-off "
	echo 0 > /proc/sys/net/ipv4/tcp_slow_start_after_idle
	echo -n "lower tcp latency   "
	echo 1 > /proc/sys/net/ipv4/tcp_low_latency
	echo -n "cron.daily-trigger "
###	ifconfig eth0 txqueuelen 100
###	ifconfig tun0 txqueuelen 10
###	ifconfig tun1 txqueuelen 10
fi

	rm -f /var/spool/cron/lastrun/cron.daily

	(
#	# virtual machines startup:
	# see "stop" section for halt procedure!
	cd /kvm || exit 0
	export STARTUP_DELAY=yes
	modprobe kvm_intel
	screen -dmS kvm
	sleep 1
	for i in *.sh; do
		screen -x kvm -X screen bash ./$i # returns fast
		sleep 1 # there is another sleep in each startup script.
	done
###	screen -dmS kvm bash ./kvm-0.qemu_img.sh
	) &


	sync
	sync

	modprobe zram num_devices=2
	SIZE=1024
	echo $(($SIZE*1024*1024)) > /sys/block/zram0/disksize
	echo $(($SIZE*1024*1024)) > /sys/block/zram1/disksize
	mkswap /dev/zram0
	mkswap /dev/zram1
	swapon -p 42 /dev/zram0
	swapon -p 42 /dev/zram1


	# ksm same page merging:
	echo 1 > /sys/kernel/mm/ksm/run


if false; then # handled by /usr/local/sbin/power-auto, NOT started automatically yet.
	(
	cd /sys/devices/system/cpu
	# CPU at pedal to the metal:
	for i in 0 1 2 3; do
	  for j in 0 1 2 3 4; do 
	    echo 1 > cpu$i/cpuidle/state$j/disable
	  done
	done
	# this is the default already:
	echo 0 > intel_pstate/no_turbo
	)
fi

#	echo "removing ssh-agent directories from /tmp."
	rm -rf /tmp/ssh-???????? /nosuchfileordirectory

	# Remember status and be verbose
#	echo -n "losetup(/nbd0) nbd "
#	losetup /dev/loop0 /nbd0
#	nbd-server 12000 /dev/loop0 -a 21600 -l /etc/nbd_server.allow
	rc_status -v
	;;
    stop)
	echo -n "Shutting down services in serverconfig..."
#	killall -TERM icmpinfo tcplog vdradmind > /dev/null 2>&1
#	killall -KILL icmpinfo tcplog vdradmind > /dev/null 2>&1

	killall openvpn
	pkill -9 -u nexstat
	pkill -9 -u nextst
	pkill -9 -u nex1v1
	pkill -9 -u nexuiz
	pkill -9 -u nexctf
	pkill -9 -u nexdbc

	for i in /tmp/kvm*.socket; do
		echo system_powerdown | nc -U "$i"
	done
	sleep 15	# allow them to settle down...


	# Remember status and be verbose
	rc_status -v
	;;
    try-restart)
	## Stop the service and if this succeeds (i.e. the 
	## service was running before), start it again.
	## Note: try-restart is not (yet) part of LSB (as of 0.7.5)
	$0 status >/dev/null &&  $0 restart

	# Remember status and be quiet
	rc_status
	;;
    restart)
	## Stop the service and regardless of whether it was
	## running or not, start it again.
	$0 stop
	$0 start

	# Remember status and be quiet
	rc_status
	;;
    force-reload)
	## Signal the daemon to reload its config. Most daemons
	## do this on signal 1 (SIGHUP).
	## If it does not support it, restart.

##	echo -n "Reload service FOO"
	## if it supports it:
##	killproc -HUP $FOO_BIN
	#touch /var/run/FOO.pid
	rc_status -v

	## Otherwise:
	#$0 stop  &&  $0 start
	#rc_status
	;;
    reload)
	## Like force-reload, but if daemon does not support
	## signalling, do nothing (!)

	# If it supports signalling:
##	echo -n "Reload service FOO"
##	killproc -HUP $FOO_BIN
	#touch /var/run/FOO.pid
	rc_status -v
	
	## Otherwise if it does not support reload:
	#rc_failed 3
	#rc_status -v
	;;
    status)
	echo -n "Checking for service FOO: "
	## Check status with checkproc(8), if process is running
	## checkproc will return with exit status 0.

	# Return value is slightly different for the status command:
	# 0 - service running
	# 1 - service dead, but /var/run/  pid  file exists
	# 2 - service dead, but /var/lock/ lock file exists
	# 3 - service not running

	# NOTE: checkproc returns LSB compliant status values.
##	checkproc $FOO_BIN
	rc_status -v
	;;
    probe)
	## Optional: Probe for the necessity of a reload,
	## print out the argument which is required for a reload.

	test /etc/FOO/FOO.conf -nt /var/run/FOO.pid && echo reload
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload|probe}"
	exit 1
	;;
esac
rc_exit
