#!/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 15.0" > /etc/zenwalk-version

# activate new configs
dotnewlist="$(find /etc -name "*.new")"
if [ "$dotnewlist" ] ; then 
	for file in $dotnewlist ; do
		origfile="$(echo $file|sed -e 's/.new//')"
		cp -f $file $origfile
		rm -f $file
	done
fi

# In case 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

(
# 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

