#!/bin/sh
# vim:set sw=4 ts=4:
#
# $Id: make_config_files,v 1.9 2005/06/14 14:07:45 fabian Exp $
#
#############################################################################
#
# ALICE
# Automatic Linux Installation and Configuration Environment
#
# Copyright (c) 2000-2002 SuSE Linux Solutions AG, Eschborn, Germany
#               2002-2004 SuSE Linux AG, Eschborn, Germany
#               2005      SUSE GmbH, Nuernberg, Germany
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
#############################################################################
#
# Authors: Jrgen Henge-Ernst 
#          Fabian Herschel
# 
##############################################################################
#
# make_config_files
# create static configuration files
# NEW TAG: MISC_FILE_TABLE
#
# Sections: sys, network, misc, <any section named in the master misc tags>
# Tags:     MISC_CFG_TABLE, MISC_CFG_LINKS, MISC_FILE_TABLE 
# Tags:     MISC_CFG_<name>  (if using MISC_CFG_TABLE)
# Tags:     <section>_<name> (if using MISC_FILE_TABLE)
#
##############################################################################
#

usage()
{
   echo "$_self -h | --help | -?"
   echo "$_self -fqhn"
}

create_generic_file()
{
	typeset varval var val
	typeset my_section my_mode my_user my_group my_path my_name my_prog my_content 
	typeset my_SECTION my_DIR my_expand my_device_parms my_recursive my_type
	typeset my_src
	my_type="file"
	while [ $# -gt 0 ]
	do
	    varval="$1"
	    var="${varval%=*}"
	    val="${varval#*=}"
	    case "$var" in
		section | SECTION )
			my_section="$val"
			;;
		mode    | MODE    )
			my_mode="$val"
			;;
		user    | USER    )
			my_user="$val"
			;;
		group   | GROUP   )
			my_group="$val"
			;;
		path    | PATH    )
			my_path="$val"
			;;
		name    | NAME    )
			my_name="$val"
			;;
		filter    | FILTER    )
			my_prog="$val"
				;;
                expand )
			WARNING FREETEXT 'Using expand in MISC_FILE_TABLE is obsolete. Use <misctag expand="yes">...</misctag> instead.'
			my_expand="yes";
                       ;;
		recursive )
			my_recursive="yes";
			;;
		type )  # file (default) | directory | device | symlink
			my_type="$val";
			;;
		device_parms ) 
			# "TYPE MAJOR MINOR"
			my_device_parms="$val";
			;;
		src )   # for symlinks (type=symlink)
			my_src="$val";
			;;
			
	    esac
	    shift
	done
	#
	# load the config
	#
	if [ -n "$my_section" -a -n "$my_name" ]
	then
	    my_SECTION=$(echo "$my_section" | tr "[:lower:]" "[:upper:]")
	    eval "$my_SECTION"_"$my_name"=""
	    GET_CONFIGURATION $my_section
	fi
	if [ -z "$my_path" ]
	then
	    echo "path not set in MISC_FILE_TABLE"
	    return 1
	fi
	my_DIR=$( dirname "$my_path" )
	if [ ! -d "${DIR}" ]
	then
	    mkdir -p "${my_DIR}"
	fi
	if [ -n "$my_section" -a -n "$my_name" ]
	then
	    eval my_content=\"\$${my_SECTION}_${my_name}\"
	    echo "$my_content" > /tmp/misc_file.$$
	    #
	    # create the file itself
	    #
	    if [ -n "$my_prog" ]
	    then
		cat /tmp/misc_file.$$ | $my_prog > $my_path
		rm /tmp/misc_file.$$
	    else
		mv /tmp/misc_file.$$ $my_path
	    fi
	fi
	#
	#
	#
	#
	if [ ! -e "$my_path" ]
	then
		case "$my_type" in
			file )
				touch $my_path
				;;
			directory )
				mkdir -p $my_path
				;;
			device )
				# mknod [OPTION]... NAME TYPE MAJOR MINOR
				mknod $my_path $my_device_parms
				;;
			symlink )
				ln -s $my_src $my_path
				;;
		esac
	fi
        if [ "$my_recursive" = "yes" ]
	then
		change_options=" -R "
	fi
	if [ -n "$my_mode" ]
	then
		chmod $change_options $my_mode $my_path
	fi
	if [ -n "$my_user" -o -n "$my_group" ]
	then
		chown $change_options "$my_user:$my_group" $my_path
	fi
}

test -n "$alice_dir" || alice_dir="/usr/lib/alice2/"
export alice_dir


while [ $# -gt 0 ]
do
    case $1 in
	-h | --help | -? ) usage
			  exit 1
			  ;;
	-fqhn | --fqhn ) export fqhn=$2; shift
	    ;;
    esac
    shift
done

. $alice_dir/lib/alicerc
WELCOME

#
############################################################################
#
# And now we begin - get the configuration
#
GET_CONFIGURATION sys $CFG_DEBUG
GET_CONFIGURATION network $CFG_DEBUG
GET_CONFIGURATION misc $CFG_DEBUG

if [ -n "${MISC_CFG_TABLE}" ]
then
    echo "${MISC_CFG_TABLE}" | 
    while read line ; do
	cfgsection=${line%%[ 	]*}
	line=${line##*[ 	]}
	cfgtag=${line%%=*}
	filename=${line#*=}
	GET_CONFIGURATION $cfgsection
	DIR=$( dirname "$filename" )
	if [ ! -d "${DIR}" ]
	then
	    mkdir -p "${DIR}"
	fi
	eval echo \""\$MISC_CFG_$cfgtag"\" > $filename
    done
fi

if [ -n "${MISC_CFG_LINKS}" ]
then
    echo "${MISC_CFG_LINKS}" | 
    while read line ; do
	dest=${line%%[ 	]*}
	src=${line##*[	 ]}
	DIR=$( dirname "$src" )
	if [ ! -d "${DIR}" ]
	then
	    mkdir -p "${DIR}"
	fi
	ln -s "$dest" "$src"
    done
fi

#
######### THE NEW MISC_FILE_TABLE ###################
#

if [ -n "${MISC_FILE_TABLE}" ]
then
    echo "${MISC_FILE_TABLE}" |
    while read line ; do
	eval create_generic_file "$line"
    done
fi
#
#
############################################################################
# Thats all
GOOD_BYE
