#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos72Q src/bos/usr/sbin/mkinsttape/mkinsttape.sh 1.2.8.3 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1989,2019 
# 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 
# @(#)13  1.2.8.3  src/bos/usr/sbin/mkinsttape/mkinsttape.sh, bosinst, bos72Q, q2019_06A2 1/31/19 15:32:04
#
# COMPONENT_NAME: (BOSINST) Base Operating System Installation
#
# FUNCTIONS: mkinsttape
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 1989, 1997
# 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.
#
#
# NAME: mkinsttape.sh
#
# FUNCTION:
#	Create the BOS install/maintenance tape file image by copying required
#	files to a relative position from root, using inslists to 
#	backup the files to /tmp/bosinst.tape, and then to remove the file
#	copies.
#
#	Requirements:	root authority required.
#			/tmp file system large enough for output image.
#			backup/restore commands
#
# EXECUTION ENVIRONMENT:
#
#	SYNTAX:		mkinsttape [/file] [/tmp_dir]
#				/file defaults to /tmp/bosinst.tape
#                               /tmp_dir file system for working space
#                               when creating tape image from existing
#                               mksysb image
#
# DATA STRUCTURES:
#
######################################################
cleanup()
{
	cd /
	ec=$1
	[ "$ec" -ne 0 ] && echo "mkinsttape failed"
	/usr/bin/rm -fr $temp	
	cd $cwd
	exit $ec
}
######################################################
#
# MAIN
#
DEVICE=${1:-'/tmp/bosinst.tape'}
MKSYSB_TEMP=${2:-''}

trap "trap '' 0 1 2 15; cleanup 1" 0 1 2 15

root='/'
temp=/tmp/bosinst.$$
libs=$temp/usr/lib
ccslibs=$temp/usr/ccs/lib
diaglibs=$temp/usr/lpp/diagnostics/lib
filelist=$temp/filelist
bosinstdir=/usr/lpp/bosinst/tape
bosbootdir=/usr/lib/boot/tape
tapeblksz=/tapeblksz
cwd=`pwd`
needed=5000

[ `expr "$DEVICE" : "[/]"` -eq 0 ] && {
	echo "\nmkinsttape: Absloute path must be used on target device: $DEVICE\n" 
	trap '' 0 1 2 15
	cleanup 2
	}
/usr/bin/rm -fr $temp 2> /dev/null
	#
	# Make sure there's some working space in /tmp
	#
tmpfree=`/usr/bin/df -k /tmp | /usr/bin/awk 'NR == 2 { print $3 }'`
[ "$tmpfree" -lt "$needed" ] && {
	echo "/tmp needs $needed KB free space."
	trap '' 0 1 2 15
	cleanup 3
}

/usr/bin/mkdir -p $libs || { trap '' 0 1 2 15; cleanup 5; }
/usr/bin/mkdir -p $ccslibs || { trap '' 0 1 2 15; cleanup 5; }
/usr/bin/mkdir -p $diaglibs || { trap '' 0 1 2 15; cleanup 5; }

	#
	# Strip libraries before they go in the install image.
	#
cd $temp || { trap '' 0 1 2 15; cleanup 6; }
# If diagnostics have been installed, strip libraries needed for pretest also.
if [ -s $MKSYSB_TEMP/usr/lpp/diagnostics/bin/dskpt.dep ]
then
	depfile=$MKSYSB_TEMP/usr/lpp/diagnostics/bin/dskpt.dep
else
	depfile=
fi
PLATFORM=`/usr/sbin/bootinfo -a`
if [ "$PLATFORM" -ne 4 ]	# Check for IA64 platform
then
	/usr/bin/grep -h -e /usr/lib/lib -e /usr/ccs/lib/lib -e /usr/lpp/diagnostics/lib/lib \
	$MKSYSB_TEMP$bosinstdir/tapefiles1 $depfile \
	| while read libname rest
		do
			OBJECT_MODE=32_64 ar x $MKSYSB_TEMP$libname shr.o
			strip shr.o 2>/dev/null
			OBJECT_MODE=32_64 ar cq .$libname shr.o  || { trap '' 0 1 2 15; cleanup 7; }
			/usr/bin/rm -f shr.o
		done
fi
# Symbolically link directories into /tmp/bosinst/usr/lib so $temp/usr/lib
# looks like /usr/lib.
/usr/bin/ln -s $MKSYSB_TEMP/usr/lib/boot $MKSYSB_TEMP/usr/lib/drivers $MKSYSB_TEMP/usr/lib/methods $MKSYSB_TEMP/usr/lib/nls \
      $MKSYSB_TEMP/usr/lib/objrepos $temp/usr/lib

# Also, link in other unmodified directories to minimize the number of
# symlinks and create a cleaner bosinst.tape image
/usr/bin/ln -s $MKSYSB_TEMP/usr/sbin $MKSYSB_TEMP/usr/bin $temp/usr
/usr/bin/ln -s $MKSYSB_TEMP/usr/lpp/bosinst $temp/usr/lpp
/usr/bin/ln -s $MKSYSB_TEMP/etc $MKSYSB_TEMP/sbin $temp
/usr/bin/ln -s $MKSYSB_TEMP/usr/ccs/bin $temp/usr/ccs

cd $root || { trap '' 0 1 2 15; cleanup 10; }

> $filelist

if [ -s $tapeblksz ]
then
     echo $tapeblksz > $filelist
     /usr/bin/cp -p $tapeblksz $temp
fi

# Now include the map files.
        /usr/bin/mkdir $temp/tmp 2>/dev/null
        ln -s $MKSYSB_TEMP/tmp/vgdata $temp/tmp/vgdata
        cur_dir=$cwd
        cd $temp
        for i in `/usr/bin/ls -1 tmp/vgdata/*/*.map 2>/dev/null`
        do
                echo /$i
        done >> $filelist
        /usr/bin/rm -rf $temp/tmp/vgdata
        cd $cur_dir

# Only put device drivers in list if they exist on the machine.
for i in `/usr/bin/grep -E "/usr/lib/drivers|/usr/lib/methods|/usr/sbin|/usr/ccs/bin" $MKSYSB_TEMP$bosinstdir/tapefiles1`
do
	if [ -f $MKSYSB_TEMP$i ]
	then
		echo $i
	fi
done >> $filelist

# There may be additional tape device support 
# specified in /usr/lib/boot/tape.tapefiles1.[tape type].
# Only put additional device drivers in list if they exist
# on the machine.
for i in `/usr/bin/cat $MKSYSB_TEMP$bosbootdir/* 2>/dev/null`
do
	if [ -f $MKSYSB_TEMP$i ]
	then
		echo $i
	fi
done >> $filelist

# The following will append to filelist everything in tapefiles1 except
# the BosMenus.cat files and the device drivers.  Since I got the drivers
# files in the above "for" loop, I will filter the file list through grep to
# take out the drivers.  The BosMenus.cat files will be handled later.
/usr/bin/cat $MKSYSB_TEMP$bosinstdir/tapefiles1 | /usr/bin/grep -v BosMenus.cat | \
	/usr/bin/grep -v /usr/lib/drivers | /usr/bin/grep -v /usr/lib/methods | \
        /usr/bin/grep -v /usr/sbin | /usr/bin/grep -v /usr/ccs/bin >> $filelist

if [ -f $MKSYSB_TEMP/usr/lib/nls/msg/$LANG/BosMenus.cat ]
then
	echo /usr/lib/nls/msg/$LANG/BosMenus.cat >> $filelist
fi

# If the diagnostics package has been installed, include in the bosinst.tape
# image the files needed to support pretest.
if [ -x $MKSYSB_TEMP/usr/lpp/diagnostics/bin/diagdskpt -a \
	-x $MKSYSB_TEMP/usr/lpp/diagnostics/bin/get_diag_stanzas ]
then
	$MKSYSB_TEMP/usr/lpp/diagnostics/bin/get_diag_stanzas $libs/objrepos
	# only include the pretest files which exist.
	for i in `/usr/bin/cat $MKSYSB_TEMP/usr/lpp/diagnostics/bin/dskpt.dep`
	do
		if [ -f "$MKSYSB_TEMP$i" ]
		then
			echo $i
		fi
	done >> $filelist

fi


	#
	# Make backup image.
	#
cd $temp || { trap '' 0 1 2 15; cleanup 8; }

# image.data and bosinst.data are restored directly from the bosinst.data
# file on the tape and cannot have a trailing '/' in the filename, therefore
# the file cannot be symlinked in but rather must be copied in to the
# working directory
/usr/bin/rm -f $temp/image.data $temp/bosinst/bosinst.data 
/usr/bin/cp -p $MKSYSB_TEMP/image.data $MKSYSB_TEMP/bosinst.data $temp

# At this point, it is likely that there are no files in the filelist
# which are not visible due to the links to the intermediate directories,
# but if there is something hanging out, link it in to the appropriate
# directory and back it up with a trailing '/' so that the actual file
# gets backed up instead of the symlink.
/usr/bin/cat $filelist | while read fn
do
if [[ ! -f .$fn ]] 
then
    dn=${fn%%/*([!/])*(/)}
    [[ ! -d ./$dn ]] && /usr/bin/mkdir -p ./$dn
    /usr/bin/ln -s $MKSYSB_TEMP$fn .$fn
    echo .$fn/ 
else
    echo .$fn
fi
done | /usr/sbin/backup -iprf$DEVICE

rc=$?
[ "$rc" -ne 0 ] && { trap '' 0 1 2 15; cleanup 9; }

	#
	# image created exit w/ return code 0
	#
cd $root
trap '' 0 1 2 15
cleanup 0
