#!/bin/sh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lpp/bos/mksysck.sh 1.6 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1990,2005 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)65	1.6  src/bos/usr/lpp/bos/mksysck.sh, cmdsadm, bos720 6/6/05 17:09:29
# COMPONENT_NAME: (TCBADM) add device entries to SYSCK database
#
# FUNCTIONS: mksysck
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 1990
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

#
# Clean up if I get killed
#
trap "rm -f /tmp/stanzas /tmp/devices; exit -1" 1 2 3 15

#
# Find all device entries in the /dev directory and see about finding
# the destinations of the symbolic links in there as well.
#
find /dev -print | grep -v -E '^/dev/ipldevice$|^/dev/ipl_blv$' \
	| xargs ls -lid > /tmp/devices
find /dev -type l -exec ls -l {} \; | \
	sed -e 's/.* -> \([^ ]*\)$/\1/' | \
	xargs ls -lid >> /tmp/devices

#
# Mung on the ls output and get the major and minor numbers all
# fixed up without commas and such
#
sed -e 's/^ *//' -e 's/\([0-9][0-9]*\), *\([0-9][0-9]*\)/\1,\2/' \
	-e 's/  */ /g' < /tmp/devices | \
	sort -u -o /tmp/devices +0n -1 +9d -10

#
# Feed the result to awk and let awk create the stanzas from the
# edited ls output.  TCBCK then adds those stanzas to its database.
#
awk -f /usr/lpp/bos/mksysck.awk /tmp/devices > /tmp/stanzas
tcbck -a -f /tmp/stanzas

#
# Determine if tcbck failed and exit now rather than fixup the
# IPL device link.
#
if [ $? -ne 0 ]; then
	rm -f /tmp/stanzas /tmp/devices
	exit 1
fi

#
# Determine the real name of /dev/ipldevice by searching the /dev
# directory for its hard link.
#
status=0
if [ -c /dev/ipldevice ]; then
	INUM=$(ls -i /dev/ipldevice | awk '{ print $1 }')
	REAL=$(find /dev -inum $INUM -print | grep -v /dev/ipldevice | head -1)
fi
if [ "${REAL}" != "" ]; then
	tcbck -a ${REAL} links=/dev/ipldevice
	[[ $? -ne 0 ]] && status=1
fi

#
# Determine the real name of /dev/ipl_blv by searching the /dev
# directory for its hard link.
#
if [ -c /dev/ipl_blv ]; then
	INUM=$(ls -i /dev/ipl_blv | awk '{ print $1 }')
	REAL=$(find /dev -inum $INUM -print | grep -v /dev/ipl_blv | head -1)
fi
if [ "${REAL}" != "" ]; then
	tcbck -a ${REAL} links=/dev/ipl_blv
	[[ $? -ne 0 ]] && status=1
fi

#
# Cleanup and exit with whatever the exit status from TCBCK was
#
rm -f /tmp/stanzas /tmp/devices
exit $status