#!/usr/bin/ksh

# full pathname of the file to be installed
FILE=$1

# full pathname of the installdb
INSTALLDB=$2

# temp file to store install deliverables
TMPDBFILE=/tmp/$$

# Grep the entry out the installdb and store the results in the temp file.
# Use the ^ and : characters to make sure we don't pull more than one
# entry out the the installdb.  As an example, if we had both /etc/README 
# and /usr/newconfig/etc/README.conv in the installdb, this could present
# problems in the absence of either ^ or : 
grep ^$FILE: ${INSTALLDB} > ${TMPDBFILE}

# Check to see if the grep was successful. If it returns a non-zero error code,
# produce a warning message.

if [ $? -ne 0 ] ; then
	echo "WARNING: $FILE does not exist as the first field in ${INSTALLDB}"
fi

# Commands used by this script

CPSET=/usr/bin/cpset
MKDIR=/usr/bin/mkdir
RM=/usr/bin/rm
CHMOD=/usr/bin/chmod
CHOWN=/usr/bin/chown
  
IFS=":"
cat ${TMPDBFILE} | \
 while read INS_TARGET PRODUCT FILESET TYPE STATUS ARCH MODE OWNER GROUP LINK_SRC SDSTATUS MISC_FLD PROJECT; do 
	DIR=`dirname ${INS_TARGET}`
	FILE=`basename ${INS_TARGET}`
	${MKDIR} -p ${ROOT}/${FILESET}${DIR}
	if [ ${TYPE} = "sym_link" ] ; then
		${RM} -f ${ROOT}/${FILESET}${DIR}/${FILE} 
		echo "Creating symlink ${ROOT}/${FILESET}${DIR}/${FILE} to ${LINK_SRC}"
		ln -s ${LINK_SRC} ${ROOT}/${FILESET}${DIR}/${FILE}
		#${CHMOD} ${MODE} ${ROOT}/${FILESET}${DIR}/${FILE}
		${CHOWN} "${OWNER}:${GROUP}" ${ROOT}/${FILESET}${DIR}/${FILE}
		
	elif [ ${TYPE} = "hard_link" ] ; then
		${RM} -f ${ROOT}/${FILESET}${DIR}/${FILE}
		echo "Creating hardlink ${ROOT}/${FILESET}${DIR}/${FILE} to ${ROOT}/${FILESET}${LINK_SRC}"
		ln  ${ROOT}/${FILESET}${LINK_SRC} ${ROOT}/${FILESET}${DIR}/${FILE}
		${CHMOD} ${MODE} ${ROOT}/${FILESET}${DIR}/${FILE}
		${CHOWN} "${OWNER}:${GROUP}" ${ROOT}/${FILESET}${DIR}/${FILE}
	else
	echo "installing ${FILE} into ${ROOT}/${FILESET}${DIR}"
	${CPSET} ${FILE} ${ROOT}/${FILESET}${DIR} ${MODE} ${OWNER} ${GROUP}
	fi
done

# Remove the temporary file to keep things clean
${RM} -f ${TMPDBFILE}

