#!/usr/bin/sh
#--------------------------------------------------------------------
# Program: r3itotra 
#
# Functionality: Copy the distributed SAP R/3 transports to the
#		 /usr/sap/trans directory
#--------------------------------------------------------------------

check_hardware()
{
	echo ">>>>>> Checking hardware"
	OPCCMD=/opt/OV/bin/OpC/cmds
	OPCCMD4=/var/opt/OV/bin/OpC/cmds

	HARDWARE=`uname -s`
	if [ "$HARDWARE" = "HP-UX" ]
		then
		VERSION=`uname -r | cut -d "." -f 2-`
		if [ $VERSION -lt 10 ]
        		then
        		OPCCMD=/usr/OV/bin/OpC/s700/cmds
		fi
	fi
	
	if [ "$HARDWARE" = "AIX" ]
		then
        	OPCCMD=/usr/lpp/OV/OpC/cmds
        	OPCCMD4=/var/lpp/OV/bin/OpC/cmds
	fi
	
	if [ "$HARDWARE" = "OSF1" ]
		then
        	OPCCMD=/usr/opt/OV/bin/OpC/cmds
	fi

# Check for installed ITO 4.0 agent. Highest priority

	if [ -d $OPCCMD4 ]
		then
		OPCCMD=$OPCCMD4
	fi
}


check_files()
{

	echo ">>>>>> Checking directory /usr/sap/trans"
	if [ ! -d /usr/sap/trans ]
		then
		echo "ERROR: Directory /usr/sap/trans doesn't exist"
		echo ""
		echo "Press RETURN to continue"
		read tmp
		exit 
	fi

	echo ">>>>>> Checking file $OPCCMD/r3itotra.tar"
	if [ ! -f $OPCCMD/r3itotra.tar ]
		then
		echo "ERROR: File $OPCCMD/r3itotra.tar doesn't exist"
		echo ""
		echo "Press RETURN to continue"
		read tmp
		exit
	fi

	echo ">>>>>> Checking directory /tmp/_hpitosap"
	if [ -d /tmp/_hpitosap ]
		then
		echo ">>>>>> Removing directory /tmp/_hpitosap"
		rm -rf /tmp/_hpitosap
	fi

	if [ ! -d /tmp/_hpitosap ]
		then
		if mkdir /tmp/_hpitosap
			then
			echo ">>>>>> Creating directory /tmp/_hpitosap"
		else
			echo "ERROR: File $OPCCMD/r3itotra.tar doesn't exist"
			echo ""
			echo "Press RETURN to continue"
			read tmp
			exit
		fi
	fi
}

move_transport_to_tmp()
{
	echo ">>>>>> Extract $OPCCMD/r3itotra.tar to directory /tmp/_hpitosap"
	(cd /tmp/_hpitosap; tar -xf $OPCCMD/r3itotra.tar)
}

select_uid()
{
	UID=""
   	for i in `cut -d ":" -f 1 /etc/passwd | grep adm | sed -e 's/ //g' -e 's/	//g'`
		do
		len=`echo $i | wc -c`
		if [ $len -eq 7 ]
			then
			UID=$i
		fi
	done
	echo ">>>>>> Please enter user ID for transport files (f.e. $UID) \c"
	read UIDOK
	if [ "$UIDOK" = "" ] 
		then
		UIDOK=$UID
	fi
	if grep -q $UIDOK /etc/passwd
		then
		echo ">>>>>> User ID '$UIDOK' found in /etc/passwd"
	else
		echo "ERROR: User ID '$UIDOK' not found in /etc/passwd"
		echo ""
		echo "Press RETURN to continue"
		read tmp
		exit
	fi
}

copy_transport_files()
{
	#(cd /tmp/_hpitosap; tar -xvf $OPCCMD/r3itotra.tar)
	cd /tmp/_hpitosap
	for i in `find data cofiles -type f`
		do
		echo ">>>>>> Copy file $i to /usr/sap/trans/$i"
		cp $i /usr/sap/trans/$i
                echo ">>>>>> Change permission to $UIDOK:sapsys for file $i"
		chown $UIDOK:sapsys /usr/sap/trans/$i
		BASEALL=`basename $i`
		TRANSPORT=`echo $BASEALL | cut -d "." -f 1`
		SID=`echo $BASEALL | cut -d "." -f 2`
	done
}

echo_note()
{
	echo ""
	echo "NOTE: The transport is moved to the /usr/sap/trans directory"
	echo "      The commands: "
	echo ""
	echo "      cd /usr/sap/trans/bin"
	echo "      tp addtobuffer <transport> <SID> "
	echo "      tp import      <transport> <SID> client<client>"
	echo ""
	echo "      must be entered by <sid>adm on the managed node"
	echo ""
	echo "Example:  Transportfile = $TRANSPORT.$SID"
	echo "          SAP system    = TST"
	echo "          SAP client    = 099"
	echo " "
	echo "          tp addtobuffer $SID$TRANSPORT TST client099"
	echo "          tp import      $SID$TRANSPORT TST client099"
	echo ""
	echo "After import verify the transport with SAP transaction se01 or se09"
	echo ""
	echo "Press RETURN to continue"
	read tmp
}

check_hardware
check_files
move_transport_to_tmp
select_uid
copy_transport_files
echo_note
