#!/bin/sh
#
# find-features - Extract features from a kernel configuration and gather
#		  related information
#
# Written 2005, 2006 by Werner Almesberger
#

IN=config/kernel-config
PROC_CONFIG=/proc/config.gz
TMP=${TMPDIR:-/tmp}/.kboot-features.$$
OUT_DIR=root/etc
OUT_FILE=$OUT_DIR/kboot-features
CACHE_DIR=root-meta/cache
CACHE_FILE=$CACHE_DIR/features
DEPENDENCIES="make clean"	# perhaps a little radical

trap "rm -f $TMP" 0

. scripts/common-config

if [ ! -r $IN -a -r $PROC_CONFIG ]; then
    echo "No kernel configuration in $IN" 1>&2
    if yesno "Use $PROC_CONFIG ?"; then
	zcat <$PROC_CONFIG >$IN
    fi
fi

if [ ! -r $IN ]; then
    echo "Please put a kernel .config into $IN first." 1>&2
    exit 1
fi


stat_c()
{
    set -- `stat -t $1`
    echo "$7"
}


while read l; do
    [ -z "${l###*}" ] && continue
    __var_set_key item "$l" x
done <$IN

>$TMP

if __var_get_key item CONFIG_SMP=y >/dev/null; then
    echo "Warning: booting an SMP kernel probably doesn't work" 1>&2
else
    echo "Kernel does not use SMP, good" 1>&2
fi

if question booted_config "Access configuration files on root file system ?" y
  then
    echo __booted_config=true >>$TMP
else
    echo __booted_config=false >>$TMP
fi

if __var_get_key item CONFIG_INET=y >/dev/null; then
    if question inetworking "Enable TCP/IP networking ?" y; then
	echo __inetworking=true >>$TMP
	if question use_dhcp "Configure IP addresses with DHCP ?"; then
	    echo __use_dhcp=true >>$TMP
	    require_static=false
	else
	    echo __use_dhcp=false >>$TMP
	    echo __static_ip=true >>$TMP
	    require_static=true
	fi
	if $require_static ||
	  question static_ip \
	    "Also configure a static IP address, as fallback ?" n; then
	    # @@@ make this less naive
	    echo "__static_ip=true" >>$TMP
	    echo "__ipv4_addr=`ask ipv4_addr "IPv4 address:"`" >>$TMP
	    echo "__ipv4_netmask=`ask ipv4_netmask "IPv4 netmask:"`" >>$TMP
	    echo "__ipv4_default=`ask ipv4_default "IPv4 default route:"`" \
	      >>$TMP
	    echo "__ipv4_nameserver=`ask ipv4_nameserver "IPv4 name server:"`" \
	      >>$TMP
	    echo "__dns_domain=`ask dns_domain "DNS domain name:"`" \
	      >>$TMP
	fi
	if question ssh_client "Allow outbound SSH ?" y; then
	    echo __ssh_client=true >>$TMP
	else
	    echo __ssh_client=false >>$TMP
	fi
	if question ssh_server "Allow inbound SSH ?"; then
	    echo __ssh_server=true >>$TMP
	    if $booted_config; then
		answer ssh_update_keys true
	    fi
	    need_keys=true
	    if [ -r config/dropbear_rsa_host_key -o \
	      -r config/dropbear_dss_host_key ]; then
		need_keys=false
	    elif [ -r config/ssh_host_rsa_key -o \
	      -r config/ssh_host_dsa_key ]; then
		answer ssh_convert_keys true
		need_keys=false
	    elif [ -r /etc/dropbear/dropbear_rsa_host_key -o \
	      -r /etc/dropbear/dropbear_rsa_host_key ]; then
		if yesno "Copy host key(s) from /etc/dropbear/ ?" n; then
		    [ -r /etc/dropbear/dropbear_rsa_host_key ] && \
		      cp /etc/dropbear/dropbear_rsa_host_key config/
		    [ -r /etc/dropbear/dropbear_dss_host_key ] && \
		      cp /etc/dropbear/dropbear_dss_host_key config/
		    need_keys=false
		fi
	    fi
	    if $need_keys && [ -r /etc/ssh/ssh_host_rsa_key -o \
	      -r /etc/ssh/ssh_host_dsa_key ]; then
		if yesno "Copy host key(s) from /etc/ssh/ ?" n; then
		    [ -r /etc/ssh/ssh_host_rsa_key ] && \
		      cp /etc/ssh/ssh_host_rsa_key config/
		    [ -r /etc/ssh/ssh_host_dsa_key ] && \
		      cp /etc/ssh/ssh_host_dsa_key config/
		    answer ssh_convert_keys true
		    need_keys=false
		fi
	    fi
	    if $need_keys; then
		if $booted_config; then
		    question ssh_generate_keys \
		      "Generate new SSH keys to use as a default ?" y
		else
		    answer ssh_generate_keys true
		fi
	    fi
	else
	    echo __ssh_server=false >>$TMP
	fi
	if __var_get_key item CONFIG_NFS_FS=y >/dev/null; then
	    if question nfs_client "Support NFS-mounting ?" y; then
		echo __nfs_client=true >>$TMP
	    else
		echo __nfs_client=false >>$TMP
	    fi
	else
	    echo "No NFS support" 1>&2
	    echo __nfs_client=false >>$TMP
	fi
    else
	echo __inetworking=false >>$TMP
    fi
else
    echo "No TCP/IP networking" 1>&2
    echo __inetworking=false >>$TMP
fi

root="`awk '$2 == "/mnt/root" { print $1 }' <root/etc/fstab`"
if [ -z "$root" ]; then
    root="`stat_c /`"
    ask root "Default root device number ?" $root >/dev/null
else
    echo "Default root is $root" 1>&2
fi
echo root=$root >>$TMP

if [ -e $CACHE_FILE ]; then
    if diff -N $TMP $CACHE_FILE >/dev/null; then
	mkdir -p $OUT_DIR
	mv $TMP $OUT_FILE
	exit 0   
    fi
fi

#
# Make sure that we'll end up with something that triggers a rebuild if we get
# interrupted anywhere.
#
rm -f $CACHE_FILE
$DEPENDENCIES
mkdir -p $OUT_DIR
mv $TMP $OUT_FILE
cp $OUT_FILE $TMP
mkdir -p $CACHE_DIR
mv $TMP $CACHE_FILE

"$@"
