#----------------------------------------------------------------------------
# /var/install/include/configlib - library for eisfair scripts
#
# Copyright (c) 2005 Frank Meyer <frank@eisfair(dot)org>
#
# Creation:     24.05.2005  max
# Last Update:  $Id$
#
# 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.
#----------------------------------------------------------------------------

#----------------------------------------------------------------------------
# interface description
#----------------------------------------------------------------------------
#
# printgpl()             print the GPL Header
# printgroup()           print a configuration group header
# printvar()             print a config variable
# printend()             print the footer
#
#----------------------------------------------------------------------------

#============================================================================
# only include this file once
#============================================================================

if [ "$_CONFIGLIB" != "true" ]
then
    _CONFIGLIB='true'

    #
    # printgpl
    # ========
    #
    #  prints the GPL header for a configuration file
    #
    #
    #  Parameters:     $1    package name
    #                        e.g. "base"
    #                  $2    creation date
    #                        e.g. "31.12.2001"
    #                  $3    creator
    #                        e.g. "fm"
    #                  $4    copyright notice
    #                        e.g. "2001-2006 The Eisfair Team, c/o Frank Meyer, frank(at)eisfair(dot)org"
    #
    #  Output:        GPL header for an eisfair config-file
    #

    printgpl()
    {
        echo "#------------------------------------------------------------------------------"
	echo "# /etc/config.d/$1 - configuration file for $1"
	echo "#"
	echo "# Creation:     $2  $3"
	echo "# Last Update:  `date +%d.%m.%Y`  $LOGNAME"
	echo "#"
	echo "# Copyright (c) $4"
	echo "#"
	echo "# This program is free software; you can redistribute it and/or modify"
	echo "# it under the terms of the GNU General Public License as published by"
	echo "# the Free Software Foundation; either version 2 of the License, or"
	echo "# (at your option) any later version."
	echo "#------------------------------------------------------------------------------"
    }



    #
    # printgroup
    # ==========
    #
    #  prints an eisfair configuration group header
    #
    #
    #  Parameters:     $1    group name
    #                        e.g. "General settings"
    #                  $2    group comment [optional]
    #                        e.g. "general settings for eisfair configuration"
    #
    #  Output:         group header for an eisfair config-file
    #

    printgroup()
    {
        echo
        echo "#------------------------------------------------------------------------------"
	echo "# $1"
	if [ "$2" != "" ]
	then
	    echo "# $2"
	fi
	echo "#------------------------------------------------------------------------------"
	echo
    }



    #
    # printvar
    # ========
    #
    #  prints config variables and comments with specific indenting
    #
    #
    #  Parameters:     $1    name of variable
    #                        e.g. "FOO_ARRAY_2_VARIABLE"
    #                  $2    comment
    #                        e.g. "an important option"
    #
    #  Output:         variable entry line for an eisfair config-file
    #                  e.g. "FOO_ARRAY_2_VARIABLE="myvalue"      # an important option"
    #
    
    # fixed spaces string - 60 character long (fast method)
    _configlib_spacestr="                                                            "
    _configlib_commentpos=37
    printvar()
    {
        _printvar_varname=$1
        if [ "$2" == "" ]
        then
            _printvar_comment=''
	else
	    _printvar_comment=" # $2"
	fi
	if [ "$_printvar_varname" == "" ]
	then
	    echo "`echo "$_configlib_spacestr" | head -c$_configlib_commentpos`$_printvar_comment"
	else
	    _printvar_varlen=${#_printvar_varname}
            eval _printvar_value='$'$_printvar_varname
            _printvar_vallen=${#_printvar_value}

	    HK="'"
            if echo $_printvar_value | grep -q "'"
            then
                HK='"'
            fi

	    _printvar_strlen=`expr $_configlib_commentpos - 3 - $_printvar_varlen - $_printvar_vallen`
            if [ $_printvar_strlen -lt 0 ]
            then
                echo "$_printvar_varname=$HK$_printvar_value$HK"
		if [ "$_printvar_comment" != "" ]
		then
                    echo "`echo "$_configlib_spacestr" | head -c$_configlib_commentpos`$_printvar_comment"
		fi
            else
                echo "$_printvar_varname=$HK$_printvar_value$HK`echo "$_configlib_spacestr" | head -c$_printvar_strlen`$_printvar_comment"
            fi
        fi
    }



    #
    # printend
    # ========
    #
    #  prints an eisfair configuration footer
    #
    #
    #  Output:         footer for an eisfair config-file
    #

    printend()
    {
        echo
	echo
        echo "#------------------------------------------------------------------------------"
        echo "# End"
	echo "#------------------------------------------------------------------------------"
    }

fi