#! /bin/ksh

#
# Set the PSTAMP string
#
#PSTAMP= -set up by calling script - from pkginfo

function usage {
   print ""
   print "usage: $SCRIPT \"package\" <file> ..."
   print ""
   exit 1
}

function pstamp_bin_or_so {
   FILE=$1
   count=`mcs -p $FILE | wc -l`
   mcs -p $FILE | grep "$PSTAMP" > /dev/null 2>&1
   status=$?
   if [[ $status -ne 0 || $count -ne 4 ]]  # module name, pstamp, 2 blank lines
   then
      echo " pstamping $FILE"
      mcs -d $FILE
      mcs -a "$PSTAMP" $FILE
   else
      echo "   already pstamped: $FILE"
   fi
}

function pstamp_archive {
   FILE=$1
   ar_count=`mcs -p $FILE | grep -c "$FILE"`
   pstamp_count=`mcs -p $FILE | grep -c "$PSTAMP"`
   if [[ $ar_count -ne $pstamp_count || $ar_count -eq 0 ]]
   then
      echo " pstamping $FILE"
      mcs -d $FILE 2> /dev/null
      mcs -a "$PSTAMP" $FILE
      /bin/ar -ts $FILE > /dev/null
   else
      echo "   already pstamped: $FILE"
   fi
}  


SCRIPT=$0
if [[ $# -le 1 ]]
then
   usage
fi
PKG=$1
shift


if [[ -f $PKG || -d $PKG ]]
then
   print "Package name (and version) appear to be a file or directory"
   usage
fi
pwd | grep "$PKG" > /dev/null 2>&1
if [[ $? -ne 0 ]]
then
   print "Do not seem to be in the $PKG directory"
   usage
fi

for file in $@; do
   if [[ ! -f $file || -L $file ]]
   then
      # Not regular file or is symbolic link - skip
      echo "   skipping: $file"
      continue
   fi
   case "`file $file`" in

      *ELF\ 32-bit*executable* |		\
      *ELF\ 32-bit*dynamic* )
          # executable or shared object
          pstamp_bin_or_so $file
          ;;

      *current\ ar\ archive*)
          pstamp_archive $file
          ;;

      *)
         echo "   skipping $file"
         ;;
   esac
done
