#!/bin/sh

#
#	Generate a kickstart file from a live Red Hat 5.0+ machine
#		Alan Cox (c) Copyright 1998 Red Hat Software
#
#	This software is provided under the GNU Public License v2 or later
#

NOX=n
NET=y
nfsrootdevice=""

PATH=/sbin:$PATH
export PATH

VERSION=1.0

usage () {
    cat >&2 <<EOF
usage: `basename $0` [--version] [--nfs host:file] [--nonet] [--dhcp] [--bootp] [--nox]
       (ex: `basename $0` --nfs installbox:/mnt/redhat)
EOF
    exit $1
}

while [ $# -gt 0 ]; do
    case $1 in
	--nfs)
	    shift
	    nfsrootdevice=$1
	    ;;
	--help)
	    usage 0
	    ;;
	--nox)
	    NOX=y
	    ;;

	--nonet)
	    NET=n
	    ;;

	--dhcp)
	    NET=dhcp
	    ;;

	--bootp)
	    NET=bootp
	    ;;

	--version)
	    echo "mkkickstart: version $VERSION"
	    exit 0
	    ;;
	*)
		usage
	    ;;
    esac

    shift
done

sizeify()
{
	I=`echo $I | tr "+" " " `
	echo $I "1024 / p" | dc
}

if [ -f "/etc/sysconfig/i18n" ]; then
	. /etc/sysconfig/i18n
	echo "lang "$LANG
else
	echo "lang en"
fi
if [ "x"$NET = "xy" ]; then
	if [ -f "/etc/sysconfig/network-scripts/ifcfg-eth0" ]; then
		. /etc/sysconfig/network-scripts/ifcfg-eth0
		ETHER=eth0
		if [ "x"$BOOTPROTO = "xdhcp" ]; then
			echo "network --bootproto dhcp"
		else if [ "x"$BOOTPROTO = "xbootp" ]; then
			echo "network --bootproto bootp"
		else 
			echo "#network --static --ip 10.0.0.1 --netmask 255.255.255.0 --gateway 10.0.0.254"
			echo "***WARNING: you will need to set the static IP address in the configuration file" 1>&2
		fi
		fi
	fi
else
	if [ "x"$NET = "xbootp" ]; then
		echo "network --bootp"
	fi
	if [ "x"$NET = "xdhcp" ]; then
		echo "network --dhcp"
	fi
fi

if [  "x"$nfsrootdevice "=" "x" ]; then
	echo "cdrom"
else
	echo $nfsrootdevice | ( IFS=" :" ; read S D ; echo "nfs --server "$S" --dir "$D )
fi
if [ ! x"$ETHER" = "x" ]; then
	echo -n "device ethernet "
	X=`grep "alias eth0" /etc/conf.modules | ( read A B C ; echo $C	)`
	echo -n "$X"
	Y=`grep "options eth0" /etc/conf.modules | ( read A B C ; echo $C )`
	if [ ! x"$Y" = "x" ]; then
		echo -n " --options "$Y
	fi
	echo ""
fi
echo -n "keyboard "
sed < /etc/sysconfig/keyboard -e "s/^.*\///"| sed -e "s/\.map\"$//"
case `arch` in
	i?86 ) echo "zerombr";;
esac
fdisk -l 2>/dev/null | grep "^/dev/[shx]da" | sort >.tmp1$$ 
sort /etc/fstab >.tmp3$$
join -j 1 .tmp3$$ .tmp1$$ >.tmp2$$
rm .tmp1$$ .tmp3$$
cat .tmp2$$ | while read A B C D E F G H I J K L
do
(
	if [ "x"$C = "xswap" ]; then
		echo -n "part swap --size "
		sizeify
	fi
	if [ "x"$C = "xext2" ]; then
		echo -n "part "$B" --size "
		sizeify
	fi
)
done
rm .tmp2$$
echo "install"
echo -n "mouse "
. /etc/sysconfig/mouse
echo -n "$MOUSETYPE"| tr "[A-Z]" "[a-z]"
if [ "x"$XEMU3 "=" "xyes" ]; then
	echo -n " --emulthree"
fi
echo ""
. /etc/sysconfig/clock
echo -n "timezone "
if [ "x"$UTC "=" "xtrue" ]; then
	echo -n "--utc "
fi
file /etc/localtime | sed -e "s/^.*zoneinfo\///"
if [ ! "x"$NOX = "xy" ]; then
	if [ -f /etc/X11/XF86Config ]; then
		MONITOR=`cat /etc/X11/XF86Config |grep Identifier| head -1 | sed -e 's/\"$//'| sed -e 's/^[ \t]*Identifier[ \t]*\"//'| tr [A-Z] [a-z] `
		SERVER=`file /etc/X11/X| sed -e "s/^.*\/XF86_//"`
		echo 'xconfig --server "'$SERVER'" --monitor "'$MONITOR'"'
	fi
fi
echo -n "rootpw --iscrypted "
grep "^root:" /etc/passwd | sed -e "s/^root://" | sed -e "s/:.*$//"
echo "#lilo --append 'anything-needed'"
echo "%packages"
rpm -qa --queryformat "%{NAME}\n"
echo "%post"
