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

#ident  "@(#)testadd  1.15  01/04/16  SMI"


# ---------------------------------------------------------------------------
# testadd script places SunVTS conformed testname_probe.so probe shared library
# into probe dir.
#
# The testprobe_testname function inside the testname_probe.so will be called
# by VTS kernel probe routine to register the specified test to kernel.
# 
# Usage: testadd [-f] <testname> [<probe_target_dir>]
# where -f: force to add; if shared lib exists, overwrite.
#
# Semantics when probe dir is specified:
#
# e.g.  testadd -f cputest /opt/SUNWvts/lib/probe/sparcv9
#
#      This will copy over cputest_probe.so shared lib in current directory to
#      the probe directory specified. If there is no ./cputest_probe.so in
#      current dir, give error message.
#
# Semantics when probe dir is NOT specified:
#
# e.g. testadd -f cputest 
#      If ./cputest_probe.so is present, do nothing.
#      Otherwise retrieve ./deleted_probe/cputest_probe.so to current dir.
#      This should be executed from the probe directory itself.
#
# Note: The <testname> arg could be test pathname e.g.:
#      testadd -f <testdirpath>/cputest /opt/SUNWvts/lib/probe/sparcv9
#    In that case, it will cd to <testdirpath> first.
#
#    If <probe_target_directory> not specified, it is assumed that
# the test had existed and removed by "testrm" earlier. The testrm just
# moves the test shared lib to <probedir>/deleted_probe directory. This could
# be retrieved by testadd anytime later. It is assumed that the command
# is executed from probe dir in this case.
#
# Warning!! testadd does not attempt to find destination directory depending
# on whether the shared lib is 32 bit or 64 bit. It blindly copies over
# the shared lib to the destination probe shared lib directory.
#
# ---------------------------------------------------------------------------

usage_error()
{
  echo Usage: $0 [-f] "<testname> [<target_dir>]" ;
  echo '-f          :  Force update of existing test probe setup'
  echo '<testname>  :  Name of the test or pathname of the test. '
  echo '<target_dir>:  Target directory where the probe shared lib copied over'
  echo 'Note: testadd copies <testname>_probe.so to probe <target_dir>'

  exit 1
}

#set -x

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

force=0

if [ $1 = '-f' ]
then
  force=1
  shift
fi

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

# Next arg is test name

tname=$1
tbasename=`basename $1`
#
# For most purpose, testdir=. 
# However a nontrivial pathname could be passed: 
# e.g. testadd sparcv9/mytest <probe_lib_dir>
#
testdir=`dirname $tname`              # Test directory name
tprobelib=`basename $tname`_probe.so  # Test probe shared lib name
tprobelibpath=${tname}_probe.so
shift



# Only <probe-target-dir> arg to be processed remaining.

# If last arg not specified, shared lib should be retrieved from ./deleted_probe
if [ $# -eq 0 ]
then
 probelibdir=
else
 probelibdir=$1
 shift
fi

# All args must have been consumed by now. If not, that is an error.
if [ $# -ne 0 ]; then usage_error; fi

if [ -n "$probelibdir" ]
then  
    # If the probe dir is specified, then $tprobelib should be present in tstdir
    if [ ! -f $tprobelibpath ] 
    then
	echo  "Error: Source probe lib $testdir/$tprobelib does not exist." 
        if [ -f $probelibdir/deleted_probe/$tprobelib ]
        then
        cat <<EOF
Warning: Found earlier testrm'ed probe shared lib in
   $probelibdir/deleted_probe/$tprobelib directory.
   To testadd the same probe, cd to $probelibdir, then run: testadd $tbasename
EOF
        fi
	exit 1
    fi
    # The probe lib dir must exist if specified.
    if [ ! -d $probelibdir ]
    then
        echo "Error: $probelibdir directory does not exit."
        exit 1
    fi
    # If the source and destination files for shared lib are same, give error
    # This could happen in case of: testadd testname . 
    if ( /usr/bin/test $tprobelibpath -ef $probelibdir/$tprobelib )
    then
       echo "Error: Source file $tprobelibpath and destination $probelibdir/$tprobelib are same"
       exit 1
    fi
    # If the destination file exists and -f flag not specified give error
    if [ -f  $probelibdir/$tprobelib -a $force -ne 1 ]
    then
     echo "Error: Specifying -f option required to overwrite already" 
     echo "       existing test probe shared library $probelibdir/$tprobelib"
     exit 1
    fi
    # Do the real copy
    if ( cp -f $tprobelibpath $probelibdir/$tprobelib >/dev/null 2>&1 )
    then
      echo "The probe shared lib $tprobelibpath has been copied over"
      echo "    to $probelibdir/$tprobelib"
      echo "Test probe for $tname is added to SunVTS successfully."
      exit 0
    else
      echo "Error: Could not copy $tprobelibpath to $probelibdir/$tprobelib"
      echo "       Test probe add for $tname failed."
      exit 1
    fi
fi

# Now handle the case where probe target dir not specified.
# In this case, the current directory should be probe target directory.

# Give error if the shared lib already present in the cur probe dir.

if [ -f $tprobelibpath ] 
then
  echo "Error: Probe lib $tprobelibpath already exists"
  exit 1
fi

# Check to see if ./deleted_probe/$tprobelib exists.

if [ ! -f $testdir/deleted_probe/$tprobelib ]
then
 echo "Error: $testdir/deleted_probe/$tprobelib not found."
 exit 1
fi

# Retrieve the earlier testrm'ed probe shared lib
if ( mv -f $testdir/deleted_probe/$tprobelib $testdir >/dev/null 2>&1 )
then
  echo "The probe shared lib $testdir/deleted_probe/$tprobelib"
  echo "    has been copied over to current directory."
  echo "Test probe for $tname is added to SunVTS successfully."
  exit 0
else
  echo "Error: Could not copy $testdir/deleted_probe/$tprobelib to $testdir dir"
  echo "       Test probe add for $tname failed."
  exit 1
fi

