
# Filename: mergecfg
# 	Merge device configuration and H/W configuration
# 	to generate an ascii file usable by Paragon OS
#
# Usage:
#	mergecfg <hw_cfg_file> <dev_cfg_file>
#	mergecfg -h hw_cfg_file -d dev_cfg_file -p script_path -o out_file"
#		OR
#	mergecfg -u 
#
#	hw_cfg_file - 	ascii file containing h/w config info excluding
#			i/o devices
#	dev_cfg_file -	ascii file containing the device configuration of
#			SCSI, TAPE and DISK devices
#	script_path  -  Path name for the awk scripts used by this script
#
#	out_file     - merged output configuration  file
#	
# Modification History:
#
# $Log: mergecfg,v $
# Revision 1.5  1993/03/24  21:14:23  mouli
# Cleaned up error log.
#
# Revision 1.4  1993/03/24  01:23:55  mouli
# Added default path and file names.
#
# Revision 1.3  1993/03/22  01:43:20  mouli
# 1. Arguments passing style is getopt style.
# 2. Added a two new arguments.
#    (a) Path name of awk scripts
#    (b) merged output configuration file.
# 3. Errors in parsing device info (devinfo.awk) are reported.
#
# Revision 1.2  1993/03/12  01:44:17  mouli
# Added cleanup for getting rid of temporary files.
# Renamed getdevinfo.awk to devinfo.awk for short file name.
#
# Revision 1.1  1993/03/12  01:30:24  mouli
# Initial revision
#
# 	03/09/93	mouli	Initial version
#
p=$0
devinfo=devtmp.$$
errlog=mergecfg.err
def_hwcfg=/usr/paragon/boot/HWCONFIG.TXT
def_dvcfg=/usr/paragon/boot/DEVCONF.TXT
def_ocfg=/usr/paragon/boot/SYSCONFIG.TXT
def_spat=/u/paragon/diag
awk_scripts="devinfo.awk cat.awk merge.awk"
TRUE=0
FALSE=1
usage()
{
	echo "$0 [ -h hw_cfg_file ] [ -d dev_file ] [-p script_path] [-o out_file]"
	echo "		OR"
	echo "$0 -u to get this usage message"
	echo 
	echo "hw_cfg_file  is the file generated by gencfg, default = ${def_hwcfg}"
	echo "dev_cfg_file is the device configuration file, default = ${def_dvcfg}"
	echo "script_path  is the path name for the scripts, default = ${def_spat}"
	echo "out_file     is the merged output configuration file, default = ${def_ocfg}"
	echo
	exit 1
}

nofile()
{
	echo "$p: hw_cfg_file or dev_cfg_file missing"
	exit 1
}

cleanup()
{
	rm -f ${devinfo}
}

#
# deverr()
# 	$1 - Device configuration file
#	$2 - Errlog file
#
deverr()
{
	echo "$p: Error in parsing device file $1, see ${errlog} for details"
	echo "$p: Exiting"
	cleanup
	exit 1
}
BinPrompt()
{
	echo "${1}? <y/n> \c"
	read rsp
	case $rsp in
		[Yy]*)
			return ${TRUE}
			;;
		*)
			return ${FALSE}
			;;
	esac
}

trap 'cleanup' 2 15
# cleanup the errlog if there was any from the previous run
rm -f ${errlog}
#
# Process and validate arguments 
set - - `getopt h:d:p:o:u $*`
if [ $? != 0 ]
then
	usage
fi
for i in $*
do
       case $i in
		-h)
			hwcfg=$3; shift; shift;;
		-d)
			dvcfg=$3; shift; shift;;
		-p)
			scpath=$3; shift; shift;;
		-o)
			ocfg=$3; shift; shift;;
		-u)
			usage;;
	       --)      
			shift;    break;;
       esac
done
# Check for existence of files
err=0
[ "${hwcfg}" = "" ] && hwcfg=${def_hwcfg}
[ "${dvcfg}" = "" ] && dvcfg=${def_dvcfg}
[ "${scpath}" = "" ] && scpath=${def_spat}
[ "${ocfg}" = "" ] && ocfg=${def_ocfg}
if test ! -f "${hwcfg}" ; then
	echo "$p: H/W config file -> ${hwcfg} <- is missing"
	err=1
fi
if test ! -f "${dvcfg}" ; then
	echo "$p: Device config file -> ${dvcfg} <- is missing"
	err=1
fi
if test ! -d "${scpath}" ; then
	echo "$p: The path -> ${scpath} <- for scripts is missing"
	err=1
fi
if test -f ${ocfg} ; then
	if BinPrompt "${ocfg} exists, overwrite it" ; then
		echo overwrite it
	else
		echo "$p: ${ocfg} not overwritten, exiting"
		exit
	fi
fi	
for f in ${awk_scripts}
do
	[ ! -f ${scpath}/$f ] && echo "${scpath}/$f not found" && err=1
done
	
if test ${err} -ne 0 ; then
	exit 1
fi

# Now do the merge

awk -f ${scpath}/devinfo.awk errfile=${errlog} ${dvcfg} > ${devinfo}
[ -s ${errlog} ] && deverr ${dvcfg} ${errlog}
sort ${devinfo} | awk -f ${scpath}/cat.awk | awk -f ${scpath}/merge.awk master=${hwcfg} > ${ocfg}
cleanup
