#!/bin/sh

use_bin_dialog=0

if [ "$1" = "list-arch" ] ; then
	cd arch-conf/
	ls -d */ | grep -v share | paste - - - - | expand -20
	exit 0
elif [ "$1" = "menu" ] ; then
	use_bin_dialog=1
elif [ "$1" != "text" ] ; then
	echo "Usage: $0  < list-arch | text | menu >" ; exit 1
fi

arch=none
if expr "`uname -m`" : "i.86"    > /dev/null ; then arch=intel   ; fi
if expr "`uname -m`" : "alpha"   > /dev/null ; then arch=alpha   ; fi
if expr "`uname -m`" : "ppc"     > /dev/null ; then arch=powerpc ; fi
if expr "`uname -m`" : "powerpc" > /dev/null ; then arch=powerpc ; fi

crossnative=native ; build=''

egrep '^(rockver|develsnapshot|stripbins|makerebuild|buildoptpkgs)=' \
	scripts/parse-config > /tmp/$$.dat
egrep '^(buildextpkgs|abortonerror|retryfailed|dontprintlogs|useflwrapper)=' \
	scripts/parse-config >> /tmp/$$.dat
. /tmp/$$.dat ; [ $develsnapshot = 1 ] && rockver="$rockver-DEV"
rm -f /tmp/$$.dat ; unset develsnapshot

[ -f Config ] && . ./Config

title="ROCK Linux $rockver Build Configuration Tool"

x_dialog() {
	if [ "$use_bin_dialog" = 1 ] ; then
	  if dialog --backtitle "$title" "$@" 2> /tmp/$$-2.dat ; then
		cat /tmp/$$-2.dat >&2 ; rm -f /tmp/$$-2.dat ; return
	  elif [ $? = 1 ] ; then
		rm -f /tmp/$$-2.dat ; return
	  else
		clear 2>&1 ; reset 2>&1 ; clear 2>&1
		use_bin_dialog=0 ; rm -f /tmp/$$-2.dat
	  fi
	fi

	clear 2>&1
	echo -e "\n === $2 ===\n"
	shift 5 ; x=0 ; tags="x"

	if [ $# -ne 4 ] ; then
		while [ $# -gt 1 ] ; do
			tags="$tags $1"
			printf "   %-3s %s\n" $x. "$2"
			x=$(( $x + 1 ))
			shift 2
		done
	else
		tags="x 0 1"
		printf "   %-3s %s\n\n" 1. "$2"
		printf "   %-3s %s\n" 0. "$4"
	fi

	echo -en "\nEnter: " ; read in
	if expr "$in" : '[0-9]\+' > /dev/null ; then
		echo "$tags" | cut -f $(( $in + 2 )) -d ' ' >&2
	fi
}

while true ; do
	x_dialog --menu 'ROCK Linux Build Configuration' 17 70 10 \
	       arch          "Architecture                       $arch"          \
	       stripbins     "Stripping binaries                 $stripbins"     \
	       makerebuild   "Make rebuild stage (stage 5)       $makerebuild"   \
	       buildoptpkgs  "Build optional packages            $buildoptpkgs"  \
	       buildextpkgs  "Build extension packages           $buildextpkgs"  \
	       abortonerror  "Abort when a package-build fails   $abortonerror"  \
	       retryfailed   "Retry building 'broken' packages   $retryfailed"   \
	       dontprintlogs "Build-Output only to log file      $dontprintlogs" \
	       useflwrapper  "Use the flist wrapper lib          $useflwrapper"  \
	       exit          "Exit and Save"  2> /tmp/$$.dat
	sel=`cat /tmp/$$.dat`
	case "$sel" in
	    arch)
		x_dialog --menu 'Select an architecture' 17 70 10   \
		       none    "No Architecture (build disabled)"   \
		       intel   "Generic IBM PCs and compatible"     \
		       i386    "  Optimized for Intel 386"          \
		       i486    "  Optimized for Intel 486"          \
		       i586    "  Optimized for Intel Pentium"      \
		       i686    "  Optimized for Intel Pentium-Pro"  \
		       k6      "  Optimized for AMD K-6"            \
		       alpha   "Generic Alpha Workstations"         \
		       powerpc "Generic PowerPC Workstations"       \
		       sparc   "Generic Sparc Workstations" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && arch=`cat /tmp/$$.dat` ;;
	    stripbins)
		x_dialog --menu "Stripping binaries" 10 75 2 \
			1 "The created binaries should be stripped" \
			0 "The created binaries should not be stripped" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && stripbins=`cat /tmp/$$.dat` ;;
	    makerebuild)
		x_dialog --menu "Make rebuild stage (stage 5)" 10 75 2 \
			1 "Rebuild all packages in stage 5" \
			0 "Don't rebuild all packages in stage 5" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && makerebuild=`cat /tmp/$$.dat` ;;
	    buildoptpkgs)
		x_dialog --menu "Build optional packages" 10 75 2 \
			1 "Build optional packages" \
			0 "Don't build optional packages" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && buildoptpkgs=`cat /tmp/$$.dat` ;;
	    buildextpkgs)
		x_dialog --menu "Build extension (3rd party) packages" 10 75 2 \
			1 "Build extension packages" \
			0 "Don't build extension packages" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && buildextpkgs=`cat /tmp/$$.dat` ;;
	    abortonerror)
		x_dialog --menu "Abort when a package-build fails" 10 75 2 \
			1 "Abort when a package-build fails" \
			0 "Don't abort when a package-build fails" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && abortonerror=`cat /tmp/$$.dat` ;;
	    retryfailed)
		x_dialog --menu "Retry building 'broken' packages" 10 75 2 \
			1 "Retry building 'broken' packages" \
			0 "Don't retry building 'broken' packages" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && retryfailed=`cat /tmp/$$.dat` ;;
	    dontprintlogs)
		x_dialog --menu "Build-Output only to log file" 10 75 2 \
			1 "Only write the build output to the log files" \
			0 "Write the build output to the terminal and the log files" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && dontprintlogs=`cat /tmp/$$.dat` ;;
	    useflwrapper)
		x_dialog --menu "Use the flist wrapper lib" 10 75 2 \
			1 "Use the flist wrapper lib" \
			0 "Use the old compatibility code" 2> /tmp/$$.dat
		[ -s /tmp/$$.dat ] && useflwrapper=`cat /tmp/$$.dat` ;;
	    exit) break ;;
	esac
done

cat > Config <<- EOT
	#
	# Created by ./scripts/Config
	#
	# Type './scripts/Help Config' for details.
	#
	
	arch=$arch
	
	stripbins=$stripbins     # def=1, Should we strip all binaries
	makerebuild=$makerebuild   # def=1, Should we make a stage 5 (rebuild)
	buildoptpkgs=$buildoptpkgs  # def=1, Should we build the optional pkgs
	buildextpkgs=$buildextpkgs  # def=1, Should we build the extension pkgs
	abortonerror=$abortonerror  # def=1, Should we abort if a package fails to build
	retryfailed=$retryfailed   # def=1, Should we retry building 'broken' packages
	dontprintlogs=$dontprintlogs # def=1, Should we suppress the build logs to stdout
	useflwrapper=$useflwrapper  # def=1, Should we use the flist wrapper library
EOT

echo >> Config

if [ $crossnative != native ] ; then
	echo "crossnative=$crossnative" >> Config
	echo
	echo "WARNING: The ./Config file contains a crossnative setting. Only use"
	echo "this configuration variable if you know exactly what you are doing."
else
	echo "# crossnative=native" >> Config
fi

if [ "$build" ] ; then
	echo "build=$build" >> Config
	echo
	echo "WARNING: The ./Config file contains an explicit build setting. Only use"
	echo "this configuration variable if you know exactly what you are doing."
else
	echo "# build=CPU-MACHINE-linux-gnu" >> Config
fi

rm -f /tmp/$$.dat
echo
