#!/bin/sh
#
# ROCK Linux: /etc/setup.d/00-general
#
# This is a ROCK Linux setup script. ROCK Linux set scripts are started
# after a package is installed or manually by the user to (re-)setup a
# package.
#
# Usage: <srcipt-name> [ setup | update | ... ]
#
#   setup:   Run this script interactively and configure the package. But
#            ask if the user would like to configure the package first.
#
#
#   update:  Don't run interactively. This is called whenever a package has
#            been installed, removed or updated. E.g. findutils is using this
#            for updating the locatedb.
#
#   x-*:     If a script has other non-standard actions, they have to start
#            with 'x-'. E.g. '00-general x-set-tmzone' .
#
#   [a-z]*:  Everything else starting with a lower case letter will be ignored
#            (for future extensions).
#
#   *:       Help. Everything else will print out a usage note.

mk_fstab_entry() {
	if [ -b $1 ]; then
	  name=`basename $1`
	  name=$2`echo $name | sed "s,0,,"`
	  echo "$1 /mnt/$name $3 $4" >> $5
	  mkdir -p /mnt/$name
	fi
}

make_fstab() {
	echo '==> Create /etc/fstab <=='
	echo "" > /tmp/$$.2.tmp
	
	# create some defaults
	cat > /tmp/$$.2.tmp <<- EOT
none /dev devfs defaults 0 0
none /proc proc defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
none /proc/bus/usb usbdevfs defaults,noauto
EOT
	# get all the user configured during the installation
	egrep -v "/dev/rd.*|,*/src.*|none.*" /proc/mounts | \
		sed "s,rw,defaults," >> /tmp/$$.2.tmp
	
	# get the user's swap
	if grep "/dev" /proc/swaps ; then
	  grep "^/dev/" /proc/swaps | cut -f1 -d' ' | \
	    sed 's,$, swap swap defaults,' >> /tmp/$$.2.tmp
	else
	  echo "#a-dev swap swap defaults" >> /tmp/$$.2.tmp
	fi
	
	# detect all cdroms and floppy drives
	for x in `ls /dev/cdroms/* 2> /dev/null`; do
	  mk_fstab_entry $x "" "iso9660" "noauto,user,exec,ro" /tmp/$$.2.tmp

	done
	
	for x in `ls /dev/floppy/? 2> /dev/null`; do
	  mk_fstab_entry $x "floppy" "auto" "noauto,user,exec" /tmp/$$.2.tmp
	done
	
	# nice sorting
	awk '{print $2}' < /tmp/$$.2.tmp | sort -u | while read dn ; do
		[ ! -z "$dn" ] && grep " $dn " /tmp/$$.2.tmp | head -1
	done > /tmp/$$.1.tmp
	
	# a nice indenting _trick_
	cat << EOT > /tmp/$$.2.tmp
ARGIND == 1 {
    for (c=1; c<=NF; c++) if (ss[c] < length(\$c)) ss[c]=length(\$c);
}
ARGIND == 2 {
    for (c=1; c<NF; c++) printf "%-*s",ss[c]+2,\$c;
    printf "%s\n",\$NF;
}
EOT
	# run the indenting
	gawk -f /tmp/$$.2.tmp /tmp/$$.1.tmp /tmp/$$.1.tmp > /tmp/$$.3.tmp
	
	# insert the fsck field for ext2 filesystems
	cat /tmp/$$.3.tmp | sed 's,\( ext2 .*\) 0  0$,\1 0  2,' > /tmp/$$.1.tmp
	
	# finally pipe into the fstab and on the screen
	cat /tmp/$$.1.tmp | tee /etc/fstab
	rm -f /tmp/$$.1.tmp /tmp/$$.2.tmp /tmp/$$.3.tmp ; echo
}

set_keymap() {
	echo '==> Set Keyboard mapping <=='

	if [[ `uname -m` == i[3-6]86 ]] ; then
		mapdir="`echo /usr/share/kbd/keymaps/i386`"
	else
   		echo "Unknown architecture." ; return 1
	fi

	while : ; do
		echo -en '\nEnter keyboard-map name ("list" for a list): '
		read in ; echo
		if [ "$in" = "list" ] ; then
			find $mapdir -name '*.map.gz' |
				sed 's,^.*/,,; s,\.map\.gz$,,' |
				sort -u | paste - - - | expand -25
		elif [ "`find $mapdir -name "$in.map.gz"`" ] ; then
			ln -svf "`find $mapdir -name "$in.map.gz"`" \
				/etc/default.keymap.map.gz
				loadkeys /etc/default.keymap.map.gz
			break
		elif [ -z "$in" ] ; then
			echo "No keymap set." ; break
		else
			echo "No such keymap found."
		fi
	done

	echo '==> Set Keyboard behavior <=='
	while : ; do
	    [ -z "$KBDRATE" ] && {
		echo -en '\nEnter keyboard repeat rate (2.0 - 30.0 cps [24.0]): '
		read in
		if [ -z "$in" ]; then
		    echo "choosing default of 24.0 cps"
		    in=24.0
		fi
		cps=$(kbdrate -r $in | awk '{print $5}')
		if [ "$cps" != "$in" ]; then
		    echo -en "Next possible rate is: $cps\nIs this OK? (Yes/no): "
		    read in2; case "$in2" in [nN]*) continue ;; esac
		    KBDRATE=$cps
		else
		    KBDRATE=$in
		fi    
	        echo "keyboard rate set to $KBDRATE"
	    }	
	    [ -z "$KBDDELAY" ] && {
		echo -en '\nEnter keyboard delay rate (250, 500, 750, 1000 ms [500]): '
		read in;
		if [ -z "$in" ]; then
		    echo "choosing default of 500 ms"
		    in=500
		elif [ $[$in%250] -ne 0 -o $in -gt 1000 -o $in -lt 250 ]; then
			echo "wrong rate! choose of 250, 500, 750 or 1000"
			continue
		fi
		KBDDELAY=$in
		echo "keyboard delay rate set to $KBDDELAY"
	    }
	    [ -z "$NUMLEDS" ] && {
		echo -en "Do you want to activate num keys on startup? (YES/no): "
		read in
		case "$in" in 
			[nN]*) NUMLEDS=off ;; 
			*)     NUMLEDS=on ;;
		esac
		echo "num leds at startup set to $NUMLEDS"
	    }
	    break
	done
	echo "writing settings to /etc/conf/keyboard"
	cat > /etc/conf/keyboard << EOF
#
# dRock Linux : /etc/conf/keyboard
#

# This file sets 3 variables for setting keyboard behaviour.
# It is sourced from /etc/init.d/init.local

KBDRATE=$KBDRATE
KBDDELAY=$KBDDELAY
NUMLEDS=$NUMLEDS

EOF
}


set_tmzone() {
	echo '==> Set System Time Zone <=='
	cd /usr/share/zoneinfo
	while : ; do
		echo -en '\nEnter zone name ("list" for a list): '
		read in in2 ; echo
		if [ "$in" = "list" ] ; then
			echo '[ type "list dirname" show subdirectory contents ]'
			echo ; ls -p $in2
		elif [ -f "$in" ] ; then
			ln -svf /usr/share/zoneinfo/"$in" /etc/localtime
			cd ~- ; break
		else
			echo "No such time zone found."
		fi
	done

	in=''
	hw_utc="true" ; hwc_arg='--utc'
	echo -en '\nIs your hardwareclock kept in UTC time? (YES/no) '
	read in
	case "$in" in 
	    [nN]*)
		hw_utc="false"
		hwc_arg=''
		;;
	esac
	
	if [ "$hw_utc" == "true" ] ; then
		echo "hardwareclock is kept in UTC"
	else
		echo "hardwareclock is not kept in UTC"
	fi
	
	echo "UTC=$hw_utc" > /etc/conf/clock
	echo "ARC=false  #see hwclock(8) for more information" >> /etc/conf/clock
	hwclock -s $hwc_arg
	
	in=''
	while : ; do
		echo -en '\nCurrent date and time: '
		date $in ; echo -en '\nDo you like to change this? (yes/NO) '
		read in
		case "$in" in
		  [yY]*)
			echo -en 'Enter current date and time ' \
			  'format MMDDhhmm[[CC]YY][.ss]: '
			read in ; echo ; date $in ; hwclock --systohc "$hwc_arg"
			;;
		  *)
			break;
			;;
		esac
	done

	echo
}

set_locale() {
	echo '==> Set language(s) <=='

	cd /usr/share/locale
	while : ; do
		echo -en '\nEnter colon seperated list of language(s) ("list" for a list): '
		read in in2 ; echo
		if [ "$in" = "list" ] ; then
			echo ; ls -d */
		elif [ "$in" ] ; then
			echo "export LANG='$in'" > /etc/profile.d/locale
			cd ~- ; break
		else
			rm -f /etc/profile.d/locale
			cd ~- ; break
		fi
	done
}

set_rootpw() {
	echo '==> Set root password <=='
	while ! passwd root ; do sleep 1 ; done
	echo
}

update_sys() {
	echo '==> Updating some system files <=='

	echo "Running ldconfig ..." ; ldconfig -q
	echo "Running makewhatis ..." ; makewhatis
	
	echo "Recreate info dir ..."
	rm -f /usr/share/info/dir
	for x in /usr/share/info/*.info ; do
		{ install-info --info-dir=/usr/share/info $x 2>&1 ; } |
		grep -v 'no info dir entry in'
	done
}

case "$1" in

	# Configure the system
	#
	setup)
		make_fstab ; set_keymap ; set_rootpw
		set_tmzone ; set_locale ; update_sys ; echo ;;

	update)
		update_sys ;;

	# Some special actions
	#
	x-make-fstab) make_fstab ;;
	x-set-keymap) set_keymap ;;
	x-set-tmzone) set_tmzone ;;
	x-set-locale) set_locale ;;

	# Any other non-implemented function
	#
	[a-z]*) ;;

	# Help
	#
	*)
echo -e ""
echo -e "Usage: /etc/setup.d/00-general  [ action ]"
echo -e ""
echo -e "\t\tupdate  Update system after package install, remove or update"
echo -e ""
echo -e "\t\tsetup   Confgure the system:"
echo -e ""
echo -e "\t\tx-make-fstab    Create /etc/fstab from /etc/mtab."
echo -e "\t\tx-set-keymap    Select a keyboard mapping."
echo -e "\t\tx-set-tmzone    Select a time zone and set the system clock"
echo -e "\t\tx-set-locale    Select a language."
echo -e ""
echo -e "\t\t--help, -h, -?  Help"
echo -e ""
		;;

esac

exit 0
