#! /bin/sh

cleanup () {
 rm -rf /tmp/pkg2tgz
}
trap cleanup 0 1 2 3 4 5 6


if [ $# -eq 0 ]
 then
  echo "Usage $0 <packagename>"
  echo ""
  echo "This program reads /var/adm/packages/<packagename>"
  echo "and strips all files listed in the package"
  exit 1
fi

PackagePath="/var/adm/packages/$1"
Package="`pwd`/$1"


if [ ! -f ${PackagePath} ]
 then
  echo "Package ${PackagePath} does not exist"
  exit 1
fi

let Tail=`grep -n "FILE LIST:" ${PackagePath} | cut -f1 -d":"`+1

echo "Creating: ${Package}.tgz"
cd /
files=`tail +${Tail} ${PackagePath}`
list=${PackagePath}
for file in ${files}
 do
  ValidExt=`echo "${file}" | gawk '{ printf("%d\n", match($0,"\\.(a|so|so\\.[0-9,\\.]*)$")); }'`
  if [ -d ${file} ]			# Directory ?
   then
     echo -n "Directory    : ${file}"
   else
     if [ -L ${file} ]
      then
        echo -n "Symbolic link: ${file}"
      else
       if [ -x ${file} ]			# Executable ?
        then
          echo -n "Strip exec.  : ${file}"
          strip ${file} >& /dev/null
          if [ ! $? = 0 ]; then echo -n " --> Strip failed"; fi
        else
          if [ ! ${ValidExt} = 0 ]	# Library ?
            then
              echo -n "Strip library: ${file}"
              strip ${file} >& /dev/null
              if [ ! $? = 0 ]; then echo -n " --> Strip failed"; fi
            else
              echo -n "No exec./lib.: ${file}"
          fi
       fi
     fi
  fi
  echo ""
done