#!/usr/bin/ksh

typeset -r ROOTDIR=$1
typeset -r PKGSDIR=$2

PrintErr() {
	echo $@ >&2
}


if [ "$(uname -p)" == 'sparc' ] ; then

	typeset -r lb_plat='Sparc Platform Edition'

	typeset -r MNTPT_1_OF_1=sol_8_sparc
	typeset -r MNTPT_1_OF_2=sol_8_sparc_2
	typeset -r MNTPT_LANGCD=sol_8_lang_sparc

 	typeset -r prod_slice=s0	

elif [ "$(uname -p)" == 'i386' ] ; then

	typeset -r lb_plat='Intel Platform Edition'

	typeset -r MNTPT_1_OF_1=sol_8_ia
	typeset -r MNTPT_1_OF_2=sol_8_ia_2
	typeset -r MNTPT_LANGCD=sol_8_lang_ia

 	typeset -r prod_slice=s2	

else
	PrintErr "Error: Unknown platform: $(uname -p)"
	return 1
fi


typeset -r LB_1_OF_1="SOLARIS 8 SOFTWARE 1 of 2.  $lb_plat"
typeset -r LB_1_OF_2="SOLARIS 8 SOFTWARE 2 of 2.  $lb_plat"
typeset -r LB_LANGCD="SOLARIS 8 LANGUAGES.  $lb_plat"


_CopyDir() {
	typeset -r src=$1
	typeset -r dest=$2

	cd $src				|| return $?

	if [ ! -d "$dest" ] ; then
		mkdir $dest		|| return $?
	fi

	find . | cpio -pdum $dest	|| return $?

	return 0
}


copy_sol_8_1of2() {

	typeset -r rootdir=$1
	typeset -r mntpt=$2
	typeset -r dest=$3

	if [ "$rootdir" == 'cdrom' ] ; then
 		(_CopyDir /cdrom/$mntpt/$prod_slice/Solaris_8/Product $dest)
	else
		(_CopyDir /$rootdir/$mntpt/Solaris_8/Product $dest)
	fi

	return $?
}


copy_sol_8_2of2() {

	typeset -r rootdir=$1
	typeset -r mntpt=$2
	typeset -r dest=$3

	(_CopyDir /$rootdir/$mntpt/Solaris_8/Product $dest)	|| return $?

	typeset -r ea=/$rootdir/$mntpt/Solaris_8/EA/products

	typeset arch=$(uname -p)

	(_CopyDir $ea/Appcert_2.1/$arch/Packages $dest)		|| return $?
	(_CopyDir $ea/DiskSuite_4.2.1/$arch/Packages $dest)	|| return $?
	(_CopyDir $ea/Live_Upgrade_1.0/Packages/common $dest)	|| return $?
	(_CopyDir $ea/Live_Upgrade_1.0/Packages/$arch $dest)	|| return $?
	(_CopyDir $ea/Solaris_Management_Console_2.0/Packages/$arch $dest) \
		|| return $?

	return 0
}


copy_sol_8_lang() {

	typeset -r rootdir=$1
	typeset -r mntpt=$2
	typeset -r dest=$3

	typeset arch=$(uname -p)
	
	cd /$rootdir/$mntpt/components			|| return $?
	
	(_CopyDir $PWD/French/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/German/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/Italian/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/Japanese/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/Korean/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/SimplifiedChinese/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/Spanish/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/Swedish/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/TraditionalChinese/$arch/Packages $dest) ||  return $?
	(_CopyDir $PWD/shared/$arch/Packages $dest) ||  return $?
	
	return 0

}


PromptCD() {

	PrintLabel() {
		typeset -r cdlabel=$@
		PrintErr "\n\t\"$cdlabel\"\n"
	}

	typeset -r rootdir=$1
	typeset -r mntpt=$2
	shift 1
	typeset -r cdlabel=$@

	rightcd=0
	while [ "${rightcd}" = 0 ]; do
		PrintErr "\nPlease insert CD labeled:"
		PrintLabel "$cdlabel"
		PrintErr "\n(WARNING: If there is a installer window popup " \
			"after you insert the CD," 
		PrintErr "you can simply type <CTLR-C> to quit the window " \
			"at the passwd prompt.)\n"
		PrintErr "Press <return> key when ready ..."

		read char < /dev/tty

		echo "Checking ..." 

		if [ ! -d "/$rootdir/$mntpt" ] ; then
			PrintErr "/$rootdir/mntpt\n"
			PrintErr "This tool cannot locate CD with the label:"
			PrintLabel "$cdlabel"
			PrintErr "Please insert the correct CD and try again.\n"
			PrintErr "\n"
		else
			rightcd=1
		fi
	done

	return 0
}


PromptNetimage() {

	PrintLabel() {
		typeset -r cdlabel=$@
		PrintErr "\n\t\"$cdlabel\"\n"
	}

	typeset -r rootdir=$1
	typeset -r mntpt=$2
	shift 1
	typeset -r cdlabel=$@

	rightcd=0
	while [ "${rightcd}" = 0 ]; do
		PrintErr "\nMake sure you have the netimage under the"
		PrintLabel "/$rootdir/$mntpt directory"
		PrintErr "Press <return> key when ready ..."

		read char < /dev/tty

		echo "Checking ..." 

		if [ ! -d "/$rootdir/$mntpt" ] ; then
			PrintErr "This tool can't locate the netimage directory."
			PrintErr "Please check your netimage directory.\n"
			PrintErr "\n"
		else
			rightcd=1
		fi
	done

	return 0
}

Main() {

	typeset rootdir= mntpt= label=

	while read rootdir mntpt label ; do
		while (( 1 )) ; do
			# Check that $ROOTDIR has a legal value
			case $rootdir in
			cdrom)
				PromptCD "$rootdir" "$mntpt" "$label"  && break
				;;
			netimage)
				PromptNetimage "$rootdir" "$mntpt" "$label" && break
				;;
			*)
				PrintErr "extract_pkgs: {cdrom | netimage} <packages_directory>\n"
				exit
				;;
			esac
		done

		case $mntpt in
			sol_8_sparc|sol_8_ia)	
				copy_sol_8_1of2 $rootdir $mntpt $PKGSDIR || return $?
				;;
			sol_8_sparc_2|sol_8_ia_2)
				copy_sol_8_2of2 $rootdir $mntpt $PKGSDIR || return $?
				;;
			sol_8_lang_sparc|sol_8_lang_ia)
				copy_sol_8_lang $rootdir $mntpt $PKGSDIR || return $?
				;;
		esac
	done <<-EOF
                $ROOTDIR  $MNTPT_1_OF_1    $LB_1_OF_1
                $ROOTDIR  $MNTPT_1_OF_2    $LB_1_OF_2
		$ROOTDIR  $MNTPT_LANGCD    $LB_LANGCD
EOF
}

Main
