#!/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
#
#   Modified for compliance with release 6.2
#      Jay Turner (c) Copyright 2000 Red Hat, Inc.
# 

NOX=n
NET=y
nfsrootdevice=""

PATH=/sbin:$PATH
export PATH

VERSION=2.1

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()
{
	if [ "$G" = "*" ]; then
	    size=$J;
	else 
	    size=$I;
	fi

	size=`echo $size | tr "+" " " `
	echo $size "1024 / 1 + p" | dc
}
#
# LANG setup
#
if [ -f "/etc/sysconfig/i18n" ]; then
	. /etc/sysconfig/i18n
	echo "lang "$LANG
else
	echo "lang en_US"
fi
#
# Networking setup
# NOTE: currently script only configured to write configuration information for
# a single NIC; kickstart itself will need to be modified to support multiple
# card configuration
#
echo ""
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 --bootproto bootp"
		ETHER=eth0
	fi
	if [ "x"$NET = "xdhcp" ]; then
		echo "network --bootproto dhcp"
		ETHER=eth0
	fi
fi

#
# Determine whether this will be a cdrom kickstart or a kickstart from NFS
# server
#
echo ""
if [  "x"$nfsrootdevice "=" "x" ]; then
	echo "cdrom"
else
	echo $nfsrootdevice | ( IFS=" :" ; read S D ; echo "nfs --server "$S" --dir "$D )
fi
#
# Configure ethernet device, adding any options which might be necessary
# for the card
#
echo ""
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
#
# Keyboard configuration
# NOTE: keyboard will be set to 'us' unless there is an /etc/sysconfig/keyboard
# file
#
echo ""
echo -n "keyboard "
if [ -f /etc/sysconfig/keyboard ]; then
#	sed < /etc/sysconfig/keyboard -e "s/^.*\///"| sed -e "s/\.map\$//" | sed -e "s/^KEYTABLE=//" | sed -e "s/\.map.gz\$//"
	cat /etc/sysconfig/keyboard | grep 'KEYTABLE' | sed -e "s/^.*\///" | sed -e "s/\.map\$//" | sed -e "s/^KEYTABLE=//" | sed -e "s/\.map.gz\$//"
else
	echo "us"
fi
#
# Determine whether to clear the MBR of the machine and what partitions to 
# format
# NOTE: currently set to clear the MBR for all intel machines, as well as to 
# format all linux partitions which might be on the machine at install time
#
echo ""
case `arch` in
	i?86 ) echo "zerombr yes"
	       echo "clearpart --linux" ;;
esac
#
# Get current disk partitioning information
#
fdisk -l /dev/hd[a-h] 2>/dev/null | grep "^/dev/[shx]da" | sort  >.tmp1$$ 
fdisk -l /dev/sd[a-p] 2>/dev/null | grep "^/dev/[shx]da" | sort >>.tmp1$$ 
fdisk -l /dev/xd[ab]  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$$
#
# Currently set for installation; need to change to 'upgrade' in the case
# of performing kickstart upgrades
#
echo "install"
# Mouse configuration
# NOTE: currently this is using "dumb" logic, recognizing only whether the user
# has a ps/2, serial or MS Intellimouse; additional configuration will need to 
# be done by the user is desired
#
echo ""
if [ -f /etc/sysconfig/mouse ]; then
	echo -n "mouse "
	. /etc/sysconfig/mouse
	if [ "x"$MOUSETYPE = "xps/2" ]; then
		echo -n "genericps/2"
	else if [ "x"$MOUSETYPE = "ximps2" ]; then
		echo -n "msintellips/2"
	else
		echo -n "generic"
	fi
	fi 		
	if [ "x"$XEMU3 "=" "xyes" ]; then
		echo -n " --emulthree"
	fi
	mousetype=`ls -la /dev/mouse | grep "/dev/mouse" | ( read  A B C D E F G H I J K ; echo $K ) | sed -e "s/\/dev\///"`
	if [ ! x"$mousetype" = "x" ]; then
		if [ ! x"$mousetype" = "xpsaux" ]; then
			echo -n " --device "$mousetype
		fi
	fi
fi

echo ""
# 
# Timezone configuration
# NOTE: timezone will be set to 'US/Eastern' if ZONE is missing from the 
# /etc/sysconfig/clock file or if that file does not exist at all
#
echo ""
if [ -f /etc/sysconfig/clock ]; then
	. /etc/sysconfig/clock
	echo -n "timezone "
	if [ "x"$UTC "=" "xtrue" ]; then
	echo -n "--utc "
	fi
	if [ "x"$ZONE "=" "x" ]; then
		echo "US/Eastern"
	else
		echo $ZONE
	fi
fi
#
# X configuration
#
if [ ! "x"$NOX = "xy" ]; then
	echo ""
	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
#
# Root password
# NOTE: as shipped, release 6.1 does not supprt this '--iscrypted' argument
# the user will need to get the updates from the errata website in order for
# this argument to have an effect; user can also enter password in plain text
# e.g. 'rootpw ThisIsThePassword' 
#
echo "" 
echo -n "rootpw --iscrypted "
PASSWD=/etc/passwd
if [ -f /etc/shadow ] ; then
    PASSWD=/etc/shadow
fi
grep "^root:" $PASSWD | sed -e "s/^root://" | sed -e "s/:.*$//"
#
# Autherization configuration
#
echo ""
echo -n "auth"
DOMAIN=`domainname`
if [ -n "$DOMAIN" -a "$DOMAIN" != "(none)" ]; then
    echo -n " --nisdomain" $DOMAIN
    if [ -f /etc/yp.conf ]; then
	line=`grep "domain  *$DOMAIN" /etc/yp.conf | head -1 | awk '{ print $4 }'`
	if [ -n "$line" ]; then
	    echo -n " --nisserver $line"
	fi
    fi
fi
if [ -f /etc/shadow ]; then
    echo -n " --useshadow"
    if grep 'password.*pam_pwdb.*md5' /etc/pam.d/login > /dev/null; then
	echo -n " --enablemd5"
    fi
fi
echo ""
#
# Lilo configuration
# NOTE: current version does not support picking up kernel options which might 
# need to be specified in order for machine to operate; these should be entered
# manually by the user in the ks.cfg file
#
echo ""
if [ -f /etc/lilo.conf ]; then
	loc=`grep "boot=" /etc/lilo.conf | sed -e "s/boot=\/dev\///"`
	if [ -n "$loc" ]; then
		case $loc in	 
			[shx]da   ) echo  "lilo --location mbr" ;;
			[shx]da*  ) echo  "lilo --location partition" ;;
		esac
	else
		echo "#lilo --append 'anything-needed' --localtion [mbr,partition]"
	fi		
fi
#
# Package selection
#
echo ""

echo "%packages"
rpm -qa --queryformat "%{NAME}\n"
#
# Post install configuation
#
echo ""
echo "%post"
