#!/bin/sh
#
#  Copyright(c) 1995 Sun Microsystems, Inc.
#  All rights reserved.

#ident  "@(#)testadd  1.14  95/11/16  SMI"


# ---------------------------------------------------------------------------
# testadd script add SunVTS conformed testname_info.o binary to VTS kernel 
# shared object library (.so) for dynamic link during run time.
#
# The testprobe_testname function inside the testprobe_info.o will be called
# by VTS kernel probe routine to register the specified test to kernel.
# 
# Usage: testadd [-f] <testname> [<target_directory>]
# where -f: force to add; if test exits already, then update.
# ---------------------------------------------------------------------------

usage_error()
{
    echo Usage: $0 [-f] "<testname> [<target_directory>]" ;
    exit
}

#set -x

if [ $# = 0 ]; then usage_error; fi

force=0
update=0
libdir=`pwd`
pwddir=`pwd`
lib=vtsinfo
AR=/usr/ccs/bin/ar
NM=/usr/ccs/bin/nm
LD=/usr/ccs/bin/ld


for i
do case $i in
	-f)	force=1 ;
		case $# in
		    1)  usage_error ;;
		    2)  if [ $2 != '-f' ]
			then 
			    tname=$2
			else 
			    usage_error
		        fi ;;
		    3)  if [ $3 != '-f' ]
			then
			    tname=$2 ; libdir=$3
			else  
			    usage_error  
			fi ;; 
		esac ;;
	-*)	echo 'unknown flag '$i''
		usage_error ;;
   esac
done

if [ $force -ne 1 ]
then
    case $# in 
	1) tname=$1 ;;
	2) tname=$1 ; libdir=$2 ;;
	3) usage_error ;;
    esac 
fi

tinfo=${tname}_info.o

if [ ! -f $tinfo ] 
then
	echo  $tinfo "does not exist in current directory."
	exit
fi

if [ ! -d $libdir ]
then
    echo $libdir "directory does not exit."
    exit
fi
        

if ( $NM -p $tinfo | grep ' testprobe_'$tname'' > /dev/null )
then
    flag=`/usr/ccs/bin/nm -p $tinfo | grep ' testprobe_'$tname'' | awk '{print $2}'`
    if [ $flag = U ]
    then
	echo Failed to add: found undefined testprobe_$tname in ${tinfo}.
	exit
    fi
else
    echo Failed to add: testprobe_$tname not found in ${tinfo}.
    echo "The "$lib".a was not changed."
    exit
fi

$AR t ${libdir}/${lib}.a > /dev/null
if [ $? -ne 0 ]
then
    echo "Creating a new SunVTS "$lib".a!"
else 
    if ( $NM -p ${libdir}/${lib}.so | grep testprobe_$tname > /dev/null )
#    if ( $AR t ${libdir}/${lib}.a | grep $tinfo > /dev/null )
    then
	 if [ $force -ne 1 ]
	 then
	     echo $tname was already in SunVTS ${lib}.so.
	     exit
	 else
	     update=1
	 fi
    fi
fi 

rm -rf ${libdir}/tmp
mkdir ${libdir}/tmp

if [ $update -ne 1 ]
then
    cd ${libdir}/tmp
    $AR x ../${lib}.a > /dev/null 2>&1 
				# extract all .o files to tmp from current lib
    cp ${pwddir}/$tinfo .       # get testname_info.o file too 
    $AR rc ${lib}.a *.o		# update and create a new version lib
    cp ${lib}.a ..		# send back to orginal lib place
else
    $AR ru ${libdir}/${lib}.a $tinfo # Update only the testname_info.o  
    cd ${libdir}/tmp
    $AR x ../${lib}.a  		# extract all .o files to tmp from current lib
fi

$LD -dy -G -o ${lib}.so *.o	# build a shared object library

if ( $NM -p ${lib}.so | grep testprobe_$tname > /dev/null )
then
    echo $tinfo is in ${lib}.a, and testprobe_$tname in ${lib}.so. 
else
    echo Failed to add the test to SunVTS: testprobe_$tname was not found.
    echo "The "$lib".so was not changed." 
    exit
fi

cp ${lib}.so ..			# put it back to the right place
cd ..				# back to current test directory
rm -rf tmp			# clean up the mess
rm -f $tinfo			# Also remove _info.o file.

if [ $update -ne 1 ]
then
    echo $tname is added to SunVTS.
else
    echo $tname is updated in SunVTS.
fi
