#! /bin/sh

umask 022
export PATH='/bin:/usr/bin:/usr/local/bin'

if [ ! -d $HOME/diet-pc/mdcache ]; then
    cat 1>&2 <<EOF
$HOME/diet-pc/mdcache is missing - must run "dpc-mdcache" first!
EOF
   exit 1
fi
if [ ! "`which ed 2>/dev/null`" ]; then
    cat 1>&2 <<EOF
Cannot find "ed" text editor (required).
Your Linux distribution may not have installed "ed" by default.
Please install it and try again.
EOF
    exit 1
fi

reset_vars () {
    NAME=
    SYM=
    ALIAS_SYM=
    MANDATORY=
    DEFAULT=
    DEPS=
    DEST=
    DESC_IN_PROGRESS=
}



print_config () {
    cat <<EOF

config $SYM
	bool "$NAME"
EOF
    [ ! "$ALIAS_SYM" ] && /bin/echo -e "\tdefault $DEFAULT"
    [ "$DEPS" ] && /bin/echo -e "\tdepends on $DEPS"
   cat <<EOF
	help
	  $SHORTDESC

EOF
}



dest_stanza () {
    cat <<EOF

config ${SYM}_DEST
	string
	depends on $SYM
EOF
    if [ ! "$DEST" -o "$DEST" = 'any' ]; then
	if [ "$ALIAS_SYM" = 'KERNEL_IMAGE' -o "$OUTPUT_FN" = \
		'kbuild/System/Bootstrap/Kconfig' ]; then
	    cat <<EOF
	default "root" if ROOT_IS_BOOTABLE
	default "usr" if USR_IS_BOOTABLE
	default "usrlocal" if USRLOCAL_IS_BOOTABLE
	default "none" if !ROOT_IS_BOOTABLE && !USR_IS_BOOTABLE && !USRLOCAL_IS_BOOTABLE
EOF
	else
	    /bin/echo -e "\tprompt \"Where to install $NAME\" if CUSTOMIZE_DESTS"
	    /bin/echo -e '\tdefault "usr"'
	fi
    else
	/bin/echo -e "\tdefault \"$DEST\""
    fi
}



print_trailer () {
    if [ "$ALIAS_SYM" ]; then
	if [ "`grep \"^config ${ALIAS_SYM}\$\" ${OUTPUT_FN:-kbuild/Kconfig}`" \
		]; then
	    ed -s ${OUTPUT_FN:-kbuild/Kconfig} >/dev/null <<EOF
/^config ${ALIAS_SYM}\$/
+1s/\$/ || $SYM/
a
`dest_stanza`
.
w
q
EOF
	else
	    cat <<EOF

config $ALIAS_SYM
	def_bool $SYM
`dest_stanza`
EOF
	fi
    else
	dest_stanza
    fi
}



cd $HOME/diet-pc
rm -r kbuild/* 2>/dev/null
MANDATORY_SYMS="/tmp/mandatory.$$"
echo 'KERNEL_IMAGE' >$MANDATORY_SYMS
reset_vars

(cat mdcache/*; echo 'Package:') | while read LINE; do
    case "$LINE" in
    Package:*)
	if [ "$NAME" ]; then
	    if [ ! "$DESC_IN_PROGRESS" ]; then
		print_config >>${OUTPUT_FN:-kbuild/Kconfig}
	    fi
	    if [ "$ALIAS_SYM" ]; then
		OUTPUT_FN=`echo "$OUTPUT_FN" | sed 's@[^/]\+/Kconfig@Kconfig@'`
	    fi
	    print_trailer >>${OUTPUT_FN:-kbuild/Kconfig}
	    if [ "$MANDATORY" ]; then
		echo ${ALIAS_SYM:-$SYM} >>$MANDATORY_SYMS
	    fi
	    reset_vars
	    OUTPUT_FN=
	fi
	NAME="`echo \"$LINE\" | sed 's/^Package:\W\+//'`"
	SYM="`echo \"$NAME\" | tr '[a-z\-]' '[A-Z_]'`"
	;;
    Provides:*)
	ALIAS_SYM="`echo \"$LINE\" | sed 's/^Provides:\W\+//' | tr '[a-z\-]' \
		'[A-Z_]'`"
	;;
    Conflicts:*)
	# FIXME?
	:
	;;
    Priority:*)
	case "`echo \"$LINE\" | sed 's/^Priority:\W\+//'`" in
	required)
	    DEFAULT='y'
	    MANDATORY=TRUE
	    ;;
	extra)
	    DEFAULT='n'
	    ;;
	*)
	    DEFAULT='y'
	    ;;
	esac
	;;
    Section:*)
	if [ "$ALIAS_SYM" ]; then
	    SUFFIX="/`echo $ALIAS_SYM | tr '[A-Z_]' '[a-z\-]'`"
	else
	    SUFFIX=
	fi
	OUTPUT_FN="kbuild/`echo \"$LINE\" | sed 's/^Section:\W\+//'
		`$SUFFIX/Kconfig"
	mkdir -p `dirname $OUTPUT_FN`
	;;
    Destination:*)
	DEST="`echo \"$LINE\" | sed 's/^Destination:\W\+//'`"
	;;
    Depends:*)
	DEPS="`echo \"$LINE\" | sed -e 's/^Depends:\W\+//' -e 's/([^,]\+)//g' \
		-e 's/,/ \&\&/g' | tr '[a-z\-]' '[A-Z_]'`"
	;;
    Description:*)
	SHORTDESC="`echo \"$LINE\" | sed 's/^Description:\W\+//'`."
	;;
    *:*)
	if [ "$DESC_IN_PROGRESS" ]; then
	     /bin/echo -e "\t  $LINE" >>${OUTPUT_FN:-kbuild/Kconfig}
	fi
	;;
    .)
	if [ "$DESC_IN_PROGRESS" ]; then
	    echo >>${OUTPUT_FN:-kbuild/Kconfig}
	fi
	;;
    *)
	if [ ! "$DESC_IN_PROGRESS" ]; then
	    print_config >>${OUTPUT_FN:-kbuild/Kconfig}
	    DESC_IN_PROGRESS=TRUE
	fi
	/bin/echo -e "\t  $LINE" >>${OUTPUT_FN:-kbuild/Kconfig}
	;;
    esac
done

cat >>kbuild/Kconfig <<EOF
mainmenu "DIET-PC Package Selection"

config SEPARATE_USR
	bool "Create a separate filesystem for /usr"
	default n
	help
	  DIET-PC's design allows /usr to be mounted as a read-only
	  filesystem distinct from the root filesystem.

	  /usr typically contains the majority, by number, of the system's
	  iPKG packages, although none of them are essential for a bare
	  minimum operational state.  Hence separating /usr into a separate
	  filesystem allows the root filesystem to be kept very small (approx
	  5 MB or so), which may be useful in order to conserve RAM,
	  particularly if the root filesystem is an initrd or initramfs, or to
	  fit the kernel and root filesystem into a small (<= 8 MB) internal
	  flash storage device and the rest into a removeable storage device.
	  As it is read-only, /usr could also be an NFS share mounted
	  concurrently on multiple clients, or an inherently read-only
	  filesystem with integral compression (e.g. squashfs).

config SEPARATE_USRLOCAL
	bool "Create a separate filesystem for /usr/local"
	default n
	help
	  DIET-PC's design allows /usr/local to be mounted as a read-only
	  filesystem distinct from the root and/or /usr filesystem(s).  As it
	  is subordinate to /usr, you may have root-with-usr-and-usrlocal,
	  root-with-usr + usrlocal, or root + usr-with-usrlocal, or root + usr
	  + usrlocal, depending on whether or not /usr is also separate.

	  /usr/local is intended to contain the iPKG packages with the largest
	  disk footprints (typically multiple megabytes each).  Where such
	  packages exist, they will account for most of the running system's
	  size.  Hence /usr/local is well suited to high capacity conventional
	  mass storage (magnetic or solid state disk), as opposed to an MTD.
	  As it is read-only, /usr/local could also be an NFS share mounted
	  concurrently on multiple clients, or an inherently read-only
	  filesystem with integral compression (e.g. squashfs).

choice
	optional
	prompt "Filesystem containing bootstrap"
	help
	  Select this option if you wish to embed the kernel image, runtime
	  bootstrap components (if any), and initrd/initramfs image (if
	  applicable) into a userspace filesystem.

	  This is only useful if your bootloader can read the filesystem
	  format of the chosen filesystem.  In any other situation, you will
	  just be wasting disk space.

	config ROOT_IS_BOOTABLE
		bool
		prompt "root"
		help
		  This option is nonsensical if your root filesystem is an
		  initrd or initramfs, as the root filesystem cannot contain
		  itself!  In such situations, use /usr or /usrlocal or a
		  dedicated partition outside of userspace.

	config USR_IS_BOOTABLE
		bool
		prompt "usr" if SEPARATE_USR

	config USRLOCAL_IS_BOOTABLE
		bool
		prompt "usrlocal" if SEPARATE_USRLOCAL
endchoice

config CUSTOMIZE_DESTS
	bool "Prompt for location of relocatable packages"
	default n
	help
	  Select this option to view and optionally edit the installation
	  root of packages that can be installed to a variable location.

	  About 50 percent of packages have this capability, so selecting
	  this option will clutter your menus.  It is therefore off by default.

	  The installation root will default to "usr" for all relocatable
	  packages except kernel images and bootloader runtime packages (whose
	  location is set automatically in accordance with the "Filesystem
	  containing bootstrap" option).

	  Default values are set regardless of whether this option is selected
	  or not.  Selecting this option just gives you an opportunity to
	  override the default.

config MANDATORY
	bool
	default y
EOF
sed -e '/^$/d' -e 's/^/	select /' $MANDATORY_SYMS >>kbuild/Kconfig

find kbuild -type d -print | sort | while read PATHNAME; do
    PARENT=`dirname $PATHNAME`
    [ "$PARENT" = '.' ] && continue
    SECTION=`basename $PATHNAME`
    ALIAS_SYM=`echo $SECTION | tr '[a-z\-]' '[A-Z_]'`
    if grep -q "^config ${ALIAS_SYM}\$" $PARENT/Kconfig; then
	ed -s $PATHNAME/Kconfig >/dev/null <<EOF
1i
choice
	prompt "$SECTION"
	`grep -q "\<$ALIAS_SYM\>" $MANDATORY_SYMS || echo 'optional'`
.
\$a

endchoice
.
w
q
EOF
	ed -s $PARENT/Kconfig >/dev/null <<EOF
/^config ${ALIAS_SYM}\$/
+1a

source "$PATHNAME/Kconfig"
.
w
q
EOF
    else
	ed -s $PATHNAME/Kconfig >/dev/null <<EOF
1i
menu "$SECTION"
.
\$a

endmenu
.
w
q
EOF
	if [ -f $PARENT/Kconfig ]; then
	    ed -s $PARENT/Kconfig >/dev/null <<EOF
/^config/i
source "$PATHNAME/Kconfig"

.
w
q
EOF
	else
	    echo "source \"$PATHNAME/Kconfig\"" >$PARENT/Kconfig
	fi
    fi
done

rm -f $MANDATORY_SYMS
exit 0
