#! /bin/sh
#
#      extract_files     %Z% %M% %I% %E%
#
#  extracting_files devname category [-a arch] [-r release]  [-f files]
#
# extract/list file(s) on any media, given the name of the category
#	and (optinally) the kernel architecture, release version(CD only),
#   and filenames.
#
# NOTE: does not support floppy disk device
#
#       Copyright (c) 1990 by Sun Microsystems, Inc.

cmdname=$0
cmdname=`basename ${cmdname}`
devname=$1
catname=$2

key_dev=`expr "${devname}" : '\(sr\)'`

usr_arch=`arch`
kernel_arch=`arch -k`
def_arch=`arch -k`

tmpxdr="/tmp/exlst_files.$$.xdr"
tmpent="/tmp/exlst_files.$$.ent"

LISTMSG="Listing files from release media."
EXTRACTMSG="Extracting files from release media."
LOG="/usr/etc/install/files/suninstall.log"
USAGE_DEVNAME="  devname is one of: sr0, st0, st1, ... mt0 ...."
USAGE_CATEGORY="  category is a suninstall category name, which can be"
USAGE_CATEGORY2="    found by running /usr/etc/install/toc_xlat or"
USAGE_CATEGORY3="    /usr/etc/install/xdrtoc (for CD)"
USAGE_CATEGORY4="  arch is a kernel architecture such as sun4c."
USAGE_CATEGORY5="  release specifies the release version on a CDROM"
USAGE_CATEGORY6="    such as 4_1, 4_1_1...etc" 

installer=`whoami`
if [ "$installer" != "root" ]
then
	echo "You must be superuser to perform this operation."
	echo
	exit 1
fi

case ${cmdname} in
e*)	# extract
        files=""
	if [ $# -lt 2 -o $# -eq 3 ]; then
		echo "Usage: ${cmdname} devname category [-a arch] [-r release] [-f files]"
		echo "  ${USAGE_DEVNAME}"
		echo "  ${USAGE_CATEGORY}"
		echo "     ${USAGE_CATEGORY2}"
		echo "     ${USAGE_CATEGORY3}"
		echo "  ${USAGE_CATEGORY4}"
		echo "  ${USAGE_CATEGORY5}"
		echo "     ${USAGE_CATEGORY6}"
		echo "    files is optional list of files to extract"
		exit 1
	fi
	if [ $# -ge 4 ]; then
		shift
		shift
        while [ $# -ne 0 ]
        do
        	case "$1" in
        	-a)
		   		shift
				kernel_arch=$1
				if [ "${key_dev}" != "sr" -a "${kernel_arch}" != "${def_arch}" ]; then
					echo "${cmdname}: -a option with ${kernel_arch} is not allowed in this operation."
					exit 1
				fi
				shift
				;;
            -r) 
                shift
                rel=$1
                shift
                ;;
			-f)
				shift
				files="$*"
                break
				;;
			*)
				echo "${cmdname}: illegal option found."
				exit 1
				;;
			esac
       done
	fi
	what="xpf"
	;;
l*)	# list
	if [ $# -ne 2 -a $# -ne 4 -a $# -ne 6 ]; then
		echo "Usage: ${cmdname} devname category [-a arch] [-r release]"
		echo "  ${USAGE_DEVNAME}"
		echo "  ${USAGE_CATEGORY}"
		echo "     ${USAGE_CATEGORY2}"
		echo "     ${USAGE_CATEGORY3}"
		echo "  ${USAGE_CATEGORY4}"
		echo "  ${USAGE_CATEGORY5}"
		echo "     ${USAGE_CATEGORY6}"
		exit 1
	fi
	if [ $# -ge 4 ]; then
		shift
		shift
		while [ $# -ne 0 ]
		do
			case "$1" in
			-a)
				shift
				kernel_arch=$1
				if [ "${key_dev}" != "sr" -a "${kernel_arch}" != "${def_arch}" ]; then
					echo "${cmdname}: -a option with ${kernel_arch} is not allowed in this operation."
					exit 1
				fi
				shift
				;;
			-r)
				shift
				rel=$1
				shift
				;;
			*)
				echo "${cmdname}: illegal option found."
				exit 1
				;;
			esac
		done
	fi
	what="tvf"
	files=""
        ;;
*)	# unknown
	echo "unknown command name \"${cmdname}\" does not start with e or l"
	echo "  cannot figure out who i am supposed to be."
	exit 1
	;;
esac

if [ "${key_dev}" = "sr" ]; then
   mount -rt hsfs /dev/${devname} /usr/etc/install/tar
   count=`ls -d /usr/etc/install/tar/export/exec/${usr_arch}_sunos* | wc -w`
   if [ ${count} -ge 2 -a "${rel}" = "" ]; then
       echo "Multiple software releases found."
       echo "You must specifiy the -r option."  
       exit 1
   fi
   if [ "${rel}" = "" ]; then
	   release=`ls -d /usr/etc/install/tar/export/exec/${usr_arch}_sunos*`
	   rel=`expr "${release}" : '.*_sunos_\(.*\)'`
   fi

   first_path="/usr/etc/install/tar/export/exec/${usr_arch}_sunos_${rel}"
   second_path="/usr/etc/install/tar/export/exec/kvm/${kernel_arch}_sunos_${rel}"
   third_path="/usr/etc/install/tar/export/share/sunos_${rel}"
   fourth_path="/usr/etc/install/tar/export/exec/proto_root_sunos_${rel}"

   if /usr/etc/install/xdrtoc ${second_path}/xdrtoc > ${tmpxdr}
   then
       :
   else
       echo "${cmdname}: can\'t extract table of contents from media"
       rm -f ${tmpxdr}
       exit 1
   fi
else
# use toc_xlat to snarf toc
   if /usr/etc/install/toc_xlat ${devname} > ${tmpxdr}   
   then
        :
   else
	echo "${cmdname}: can\'t extract table of contents from media"
	rm -f ${tmpxdr}
	exit 1
   fi
#
# given the "catname" value (perhaps we should just have the
# user use the catagory name) - we check the type
   grep "[1-3][\ 	]*[0-9][0-9]*[\ 	]*${catname}[\ 	]" ${tmpxdr} > ${tmpent}
   if grep -s "tarZ" ${tmpent} ; then
	uncompress="/usr/ucb/uncompress"
   else
	uncompress="/bin/cat"
   fi
#
# now see if this is the correct volume
   mediavol=`grep "VOLUME " ${tmpxdr}`
   if [ "X${mediavol}" = "X" ]; then
# this "cannot happen"
	echo "whoa there, no \"VOLUME #\" found in table of contents"
	rm -f ${tmpxdr} ${tmpent}
	exit 1
   fi
   mediavol=`expr "${mediavol}" : 'VOLUME \([1-9][0-9]*\)'`
   vol=`cat ${tmpent}`
#vol=`expr "${vol}" : '\([1-9][0-9]*\)\ ' `
   vol=`expr "${vol}" : '[ 	]*\([1-9][0-9]*\)'`
   if [ "${vol}" = "" ]; then
	echo "no such category found."
	exit 1
   fi
   if [ "${vol}" -ne "${mediavol}" ]; then
	echo "wrong volume(${mediavol})in drive, need volume # \"${vol}\""
	rm -f ${tmpxdr} ${tmpent}
	exit 1
   fi
fi
#
# ok - we know the media name (from toc_xlat) and volume was good,
# so now list/extract the files
#
case $devname in
st*|mt*) # tapes
	fsf=`cat ${tmpent}`
	fsf=`expr "${fsf}" : '[ 	]*[1-3][ 	]* \([0-9][0-9]*\)' `
	mt -f /dev/nr${devname} asf ${fsf}
	dd if=/dev/nr${devname} bs=40b | ${uncompress} | tar ${what} - ${files}
	status=$?
	;;

fd*)	# floppy
	# floppies are hard - they are spread over multiple volumes...
	# for now, we punt
	# the best thing would be to modify extract_diskette.c for
	# a directory mode
	echo "sorry, we don\'t do floppies"
	status=1
	;;
sr*)	# CDROM
		if [ -f ${first_path}/${catname} ]; then
			tarpath="${first_path}/${catname}";
		elif [ -f ${second_path}/${catname} ]; then
			tarpath="${second_path}/${catname}";
		elif [ -f ${third_path}/${catname} ]; then
			tarpath="${third_path}/${catname}";
		elif [ ${catname} = "root" ]; then
			tarpath="${fourth_path}";
		fi
		if [ "${tarpath}" = "" ]; then
			echo "No such category found."
		else
			tar ${what} ${tarpath} ${files}
			status=$?
		fi
	;;

*)	# "never happen"
	echo "unknown device name \"${devname}\", don\'t know how to read it"
	status=1
	;;
esac
#
rm -f ${tmpxdr} ${tmpent}
exit $status

