#! /bin/sh

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



print_config () {
    cat <<EOF

config $SYM
	bool "$NAME"
EOF
    [ ! "$ALIAS_SYM" ] && echo -e "\tdefault $DEFAULT"
    [ "$DEPS" ] && 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 cat <<EOF
	prompt "Where to install $NAME" if CUSTOMIZE_DESTS
	default "usr"
EOF
    else cat <<EOF
	default "$DEST"
EOF
    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 `dirname $0`/..
rm -r kbuild/* 2>/dev/null
MANDATORY_SYMS="/tmp/mandatory.$$"
echo 'KERNEL_IMAGE' >$MANDATORY_SYMS
reset_vars

(cat metadata/*; 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:\s\+//'`"
	SYM="`echo \"$NAME\" | tr '[a-z\-]' '[A-Z_]'`"
	;;
    Provides:*)
	ALIAS_SYM="`echo \"$LINE\" | sed 's/^Provides:\s\+//' | tr '[a-z\-]' \
		'[A-Z_]'`"
	;;
    Conflicts:*)
	# FIXME
	:
	;;
    Priority:*)
	case "`echo \"$LINE\" | sed 's/^Priority:\s\+//'`" 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:\s\+//'
		`$SUFFIX/Kconfig"
	mkdir -p `dirname $OUTPUT_FN`
	;;
    Destination:*)
	DEST="`echo \"$LINE\" | sed 's/^Destination:\s\+//'`"
	;;
    Depends:*)
	DEPS="`echo \"$LINE\" | sed -e 's/^Depends:\s\+//' -e 's/([^,]\+)//g' \
		-e 's/,/ \&\&/g' -e 's/\s\+/ /g' | tr '[a-z\-]' '[A-Z_]'`"
	;;
    Description:*)
	SHORTDESC="`echo \"$LINE\" | sed 's/^Description:\s\+//'`."
	;;
    *:*)
	if [ "$DESC_IN_PROGRESS" ]; then
	     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
	echo -e "\t  $LINE" >>${OUTPUT_FN:-kbuild/Kconfig}
	;;
    esac
done

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

config EMBED_KERNEL_IMAGE
	bool "Include kernel image in root filesystem"
	default n
	help
	  Enable this option to include the kernel itself in the userspace
	  build.

	  This is only useful when you are using a bootloader that understands
	  Linux filesystem formats, and can read the kernel image directly
	  from the root filesystem rather than a dedicated disk (or MTD)
	  partition.  This is very likely on x86/x86_64 and powerpc platforms
	  (GRUB, syslinux, yaboot etc), and may or not be possible, depending
	  upon the bootloader, in other situations.

	  If the bootloader is not filesystem aware and appropriately
	  configured, or if the root filesystem is an initrd/initramfs, then
	  turning this option on will just waste disk space and/or RAM.

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 bootstrapping
	  the system to 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).

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 package root will default to "usr" for all relocatable packages
	  regardless of whether this option is selected or not.  Turning this
	  option on just gives you an opportunity to override the default.

config MANDATORY
	bool
	default y
EOF
sed -e '/^$/d' -e 's/^/\tselect /' $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
