#! /bin/sh
# The setting file parser script for GUARDIAN series installer Ver. 1.0.02
# Copyright (c) 2008 Canon IT Solutions Inc.
#

# check shell and set PATH environment variable
#
PATH=/bin:/usr/bin
export PATH || (echo "You must use sh to run this script"; kill $$)
if [ ! -t 0 ]; then
	echo "Say 'sh `basename $0`', not 'sh < `basename $0`'"
	exit 1
fi
LC_COLLATE=C

# script usage message print function
#
Usage()
{
	cat << END_OF_USAGE
usage:
  `basename $0` -F <filename> -s <section> [-c|-v <variable>] [-h|--help]

    -F <filename> :    specify the setting file name
    -s <section>  :    specify the section name
    -c            :    print custom variables in specified section
    -v <variable> :    print value of <variable> in specified section
    -h, --help    :    print this help message and script exit
END_OF_USAGE
}

# make regex string
#
cnv_regex()
{
	CNV=''
	for i in `echo $1 | sed -e 's/\(.\)/\1 /g'`
	do
		case $i in
		[a-z])
			_U=`echo $i | tr '[a-z]' '[A-Z]'`
			CNV="${CNV}[${i}${_U}]"
			;;
		[A-Z])
			_L=`echo $i | tr '[A-Z]' '[a-z]'`
			CNV="${CNV}[${_L}${i}]"
			;;
		*)
			CNV="${CNV}${i}"
			;;
		esac
	done
	echo ${CNV}
}

# parse command line options
#
INFL=""
SECT=""
VARL=""
ALLV=1
CUST=0
while [ $# -ne 0 ]
do
	case $1 in
	'')
		break;
		;;
	'-F')
		INFL=$2
		if [ "X${INFL}" = "X" ]; then
			echo "ERROR: specify file name." 1>&2
			exit 1
		fi
		if [ ! -r ${INFL} ]; then
			echo "ERROR: ${INFL} is not readable." 1>&2
			exit 1
		fi
		shift
		;;
	'-s')
		SECT=$2
		if [ "X${SECT}" = "X" ]; then
			echo "ERROR: specify section name." 1>&2
			exit 1
		fi
		shift
		;;
	'-c')
		CUST=1
		ALLV=0
		if [ "X${VARL}" != "X" ]; then
			echo "ERROR: you cannot specify both '-c' and '-v' options." 1>&2
			exit 1
		fi
		;;
	'-v')
		VARL=$2
		ALLV=0
		if [ "X${VARL}" = "X" ]; then
			echo "ERROR: specify variable name." 1>&2
			exit 1
		fi
		if [ ${CUST} -eq 1 ]; then
			echo "ERROR: you cannot specify both '-c' and '-v' options." 1>&2
			exit 1
		fi
		shift
		;;
	'-h'|'--help')
		Usage
		exit 0
		;;
	*)
		echo "$1: unknown option." 1>&2
		Usage
		exit 1
		;;
	esac
	shift
done
if [ "X${INFL}" = "X" ]; then
	echo "ERROR: you must be specify '-F' option and <filename>." 1>&2
	exit 1
fi
if [ "X${SECT}" = "X" ]; then
	echo "ERROR: you must be specify '-s' option and <section>." 1>&2
	exit 1
fi
if grep -i "^\[${SECT}\]$" ${INFL} >/dev/null; then
	true
else
	echo "ERROR: section[${SECT}] not found." 1>&2
	exit 1
fi

if [ ${ALLV} -eq 1 ]; then
	SED_CMD=p
elif [ ${CUST} -eq 1 ]; then
	SED_CMD="/^X_/p"
elif [ "X${VARL}" != "X" ]; then
	REG_VARL=`cnv_regex ${VARL}`
	SED_CMD="/^${REG_VARL}=/s///p"
else
	echo "ERROR: the meaning of this command is not understood." 1>&2
	exit 1
fi

REG_SECT=`cnv_regex $SECT`

RESULT=`sed -n -e "/^\[${REG_SECT}\]$/,/\[.*\]$/{
/^\[${REG_SECT}\]$/d
/\[.*\]$/q
/^[ 	]*$/d
/^#/d
s/[ 	]*=[ 	]*/=/
${SED_CMD}
}" ${INFL}`

if [ "X${RESULT}" = "X" ]; then
	if [ ${ALLV} -eq 1 ]; then
		echo "ERROR: no variables in a section[${SECT}]." 1>&2
	elif [ ${CUST} -eq 1 ]; then
		echo "ERROR: no custom variables in a section[${SECT}]." 1>&2
	elif [ "X${VARL}" != "X" ]; then
		echo "ERROR: a variable is not found." 1>&2
	fi
	exit 1
fi
echo "${RESULT}"

#
# End of this script
exit 0
