From 49e4e990abd0cb3cdacd3907711be8262339eb8b Mon Sep 17 00:00:00 2001
From: Alejandro Mery <amery@opensde.org>
Date: Thu, 27 Mar 2008 14:25:57 -0400
Subject: [PATCH] [sde-config] Introduced lib/sde-config/pkgseldir_compile.sh,
 to generate Config code to handle pkgsel.d directories

---
 lib/sde-config/pkgseldir_compile.sh | 153 ++++++++++++++++++++++++++++
 1 file changed, 153 insertions(+)
 create mode 100755 lib/sde-config/pkgseldir_compile.sh

diff --git a/lib/sde-config/pkgseldir_compile.sh b/lib/sde-config/pkgseldir_compile.sh
new file mode 100755
index 0000000..b73e6c3
--- /dev/null
+++ b/lib/sde-config/pkgseldir_compile.sh
@@ -0,0 +1,153 @@
+#!/bin/bash
+# --- SDE-COPYRIGHT-NOTE-BEGIN ---
+# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
+#
+# Filename: lib/sde-config/pkgseldir_compile.sh
+# Copyright (C) 2008 The OpenSDE Project
+#
+# More information can be found in the files COPYING and README.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License. A copy of the
+# GNU General Public License can be found in the file COPYING.
+# --- SDE-COPYRIGHT-NOTE-END ---
+
+PKGSEL_LIST_VAR="$1"
+VAR_PREFIX="$2"
+OUT_PREFIX="$3"
+
+shift 3
+
+OUT_CONFIG="${OUT_PREFIX}config.in"
+OUT_PKGSEL="${OUT_PREFIX}pkgsel.in"
+OUT_PKGSELAWK="${OUT_PREFIX}pkgsel.awk"
+
+trap ' ' INT
+
+module_get_var()
+{
+	echo "${VAR_PREFIX}${1}" | tr ' a-z-' '_A-Z_'
+}
+
+module_get_description()
+{
+	sed -n -e 's/^# Description: \(.*\)/\1/p' "$1"
+}
+
+module_get_default()
+{
+	sed -n -e 's/^# Default: \(.*\)/\1/p' "$1"
+}
+
+module_render()
+{
+	local pkgseldir="$1" module_dir="$2" module_filename="$3" module_name="$4" module_type="$5"
+	local file= var= desc= default=
+
+	# recompose the full path to the module file
+	file="$pkgseldir${module_dir:+/$module_dir}/$module_filename"
+	var="$( module_get_var "$module_name" )"
+
+	# print headers
+	cat <<-EOT | tee -a "$OUT_CONFIG.$$" >> "$OUT_PKGSEL.$$"
+	# BEGIN:$var ($module_filename)
+	#
+	EOT
+
+	case "$module_type" in
+	auto)
+		cat <<-EOT >> "$OUT_PKGSEL.$$"
+		$PKGSEL_LIST_VAR="\$$PKGSEL_LIST_VAR $file"
+		EOT
+		;;
+	boolean)
+		desc="$( module_get_description "$file" )"
+		default="$( module_get_default "$file" )"
+
+		cat <<-EOT >> "$OUT_CONFIG.$$"
+		bool '${desc:-${module_name//_/ }}' $var ${default:-0}
+		EOT
+
+		cat <<-EOT >> "$OUT_PKGSEL.$$"
+		if [ "\$$var" = 1 ]; then
+		    $PKGSEL_LIST_VAR="\$$PKGSEL_LIST_VAR $file"
+		fi
+		EOT
+		;;
+	esac
+
+	# print footers
+	cat <<-EOT | tee -a "$OUT_CONFIG.$$" >> "$OUT_PKGSEL.$$"
+	# END:$var
+
+	EOT
+}
+
+cat <<-EOT | tee "$OUT_CONFIG.$$" > "$OUT_PKGSEL.$$"
+# Generated by $0
+# ($( date ))
+# using: $@
+
+EOT
+
+for pkgseldir; do
+	if [ ! -d "$pkgseldir" ]; then
+		echo "$pkgseldir: Invalid pkgseldir." >&2
+		continue
+	else
+		pkgseldir=$( cd "$pkgseldir"; pwd -P )
+	fi
+
+	bin/find "$pkgseldir" -name '*.all' -o -name '*.ask' | sort | while read module; do
+		# simple parsing
+		#
+		module_dir="${module%/*}"
+		module_filename="${module##*/}"
+		module_file_ext="${module##*.}"
+
+		case "$module_file_ext" in
+		all)	module_type=auto
+			;;
+		ask)	module_type=boolean
+			;;
+		*)	# not a module
+			continue
+			;;
+		esac
+
+		# make $module_dir $pkgseldir relative
+		if [ "$module_dir" = "$pkgseldir" ]; then
+			module_dir=
+		else
+			module_dir="${module_dir#$pkgseldir/}"
+		fi
+
+		module_name="$( echo "${module_filename%.*}" | sed -e 's/^[0-9][0-9]-//' )"
+
+		module_render "$pkgseldir" "$module_dir" "$module_filename" "$module_name" "$module_type"
+	done
+done
+
+cat <<EOT >> "$OUT_PKGSEL.$$"
+
+# Update .awk file only if needed
+#
+if [ ! -s "$OUT_PKGSELAWK" -o "\$( cat "$OUT_PKGSELAWK.cache" 2> /dev/null)" != "\$$PKGSEL_LIST_VAR" ]; then
+	echo "\$$PKGSEL_LIST_VAR" > $OUT_PKGSELAWK.cache
+
+	cat <<-EOF > "$OUT_PKGSELAWK.\$\$"
+	# Generated by \$0
+	# \$( date )
+	# using: \$$PKGSEL_LIST_VAR
+
+	EOF
+
+	lib/sde-config/pkgsel2awk.sh \$$PKGSEL_LIST_VAR >> "$OUT_PKGSELAWK.\$\$"
+	mv "$OUT_PKGSELAWK.\$\$" "$OUT_PKGSELAWK"
+fi
+EOT
+
+for x in $OUT_CONFIG $OUT_PKGSEL; do
+	mv "$x.$$" "$x"
+done