#!/bin/sh
# postinstall		This file is executed at end of setup
#
# Version:	8.0	Sunday , 03/2016
#
# Author:	Jean-Philippe Guillemin, <jp.guillemin@free.fr>


# perform post-install config

# in case everything is not mounted yet
(
/sbin/mount /proc
/sbin/mount -a 
) 1>/dev/null 2>/dev/null

# Still Zenwalk
echo "Zenwalk 8.0" > /etc/zenwalk-version

# As long as Pat doesn't want to start networkmanager, we need to take care of it
(
if [ ! -x /etc/rc.d/rc.networkmanager ]; then
	chmod 755 /etc/rc.d/rc.networkmanager
fi
) 1>/dev/null 2>/dev/null

# Improve lilo.conf 
(
if [ ! -d /sys/firmware/efi ]; then  
	if [ ! "$(grep "loglevel" /etc/lilo.conf)" ] ; then 
	  sed -i "s|^[\# \t]*append[ \t]*=[ \t]*\"\([^\"]*\)\"|append=\"\1 loglevel=1 \"|g" /etc/lilo.conf
	fi
	if [ ! "$(grep "timeout=\"30\"" /etc/lilo.conf)" ] ; then 	
	  sed -i 's/^[\# \t]*timeout[ \t]*=.*/timeout=\"30\"/g' /etc/lilo.conf
	fi
	if [ ! "$(grep "zen.bmp" /etc/lilo.conf)" ] ; then 
	  sed -i 's/^[\# \t]*bitmap[ \t]*=.*/  bitmap = \/boot\/zen.bmp/g' /etc/lilo.conf
	fi
	/sbin/lilo
fi
) 1>/dev/null 2>/dev/null

# Improve rc.M
(
if [ ! "$(grep "Zenwalk" /etc/rc.d/rc.M)" ] ; then 
	for cmd in rc.samba rc.gpm rc.openldap rc.syslog rc.pulseaudio rc.hald rc.httpd rc.mysqld rc.ntpd rc.autofs rc.sendmail rc.atalk rc.cups rc.lprng rc.saslauthd rc.snmpd rc.dnsmasq rc.pcmcia ; do
		sed -i 's|\(.*'"$cmd"'.*start\)[ \t]*$|\1 \&|g' /etc/rc.d/rc.M
	done
	for cmd in rc.inet2 rc.scanluns rc.sysvinit ; do
		sed -i 's|\(.*'"$cmd"'\)[ \t]*$|\1 \&|g' /etc/rc.d/rc.M
	done

	echo "# Zenwalk mod" >> /etc/rc.d/rc.M
fi

) 1>/dev/null 2>/dev/null

(
# password control sucks
sed -i 's/^PASS_MIN_LEN.*/PASS_MIN_LEN  5/' /etc/login.defs
sed -i 's/^OBSCURE_CHECKS_ENAB.*/OBSCURE_CHECKS_ENAB  no/' /etc/login.defs
sed -i 's/^PASS_ALWAYS_WARN.*/PASS_ALWAYS_WARN  no/' /etc/login.defs
) 1>/dev/null 2>/dev/null

# Touch Alsa
touch /etc/modprobe.d/sound.conf

# Configure Xorg
(
if [ -x /usr/sbin/videoconfig -a -e /usr/bin/X  ]; then
  /usr/sbin/videoconfig silent  
  sed -i "s/^id:.*:initdefault:$/id:4:initdefault:/" /etc/inittab
else
  sed -i "s/^id:.*:initdefault:$/id:3:initdefault:/" /etc/inittab
fi
) 1>/dev/null 2>/dev/null


# Let's try to optimize the way we use swap on a desktop
mkdir -p /etc/sysctl.d 2>/dev/null
sysctlconf="/etc/sysctl.d/zenwalk.conf"
if [ ! -f $sysctlconf ] ; then 
	touch $sysctlconf /etc/login.defs
	let memsize="$(sed -n 's/^MemTotal:[ \t]*\([^ \t]*\)[ \t]*.B$/\1/p' /proc/meminfo)"
	if [ $memsize -gt 4096000 ] ; then
		echo "vm.swappiness=0" >> $sysctlconf 2>/dev/null
		echo "vm.dirty_background_ratio=50" >> $sysctlconf 2>/dev/null
		echo "vm.dirty_ratio=80" >> $sysctlconf 2>/dev/null
		sed -i 's/^ENV_HZ.*/ENV_HZ  HZ=1000/' /etc/login.defs
	elif [ $memsize -gt 2048000 ] ; then
		echo "vm.swappiness=10" >> $sysctlconf 2>/dev/null
		echo "vm.dirty_background_ratio=50" >> $sysctlconf 2>/dev/null
		echo "vm.dirty_ratio=80" >> $sysctlconf 2>/dev/null
		sed -i 's/^ENV_HZ.*/ENV_HZ  HZ=1000/' /etc/login.defs	
	elif [ $memsize -gt 1024000 ] ; then
		echo "vm.swappiness=50" >> $sysctlconf 2>/dev/null
	fi
fi

# We close some inetd services
(
if [ -x /etc/rc.d/rc.inetd ]; then
   /etc/rc.d/rc.inetd stop
   chmod 644 /etc/rc.d/rc.inetd
  sed -i 's/^\([ \t]*time[ \t]*.*\)/\# \1/' /etc/inetd.conf
  sed -i 's/^\([ \t]*comsat[ \t]*.*\)/\# \1/' /etc/inetd.conf
  sed -i 's/^\([ \t]*auth[ \t]*.*\)/\# \1/' /etc/inetd.conf
fi
) 1>/dev/null 2>/dev/null

# Let's add a user to the system
sleep 1

if [ -x /usr/sbin/userconfig ]; then
	/usr/sbin/userconfig adduser
	/usr/sbin/userconfig passwd root
fi

# Activate some more post-install setup, this will be executed only once (from rc.local) !
chmod 755 /etc/rc.d/rc.postinstall

