#! /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 creates a file in the current directory called"
  echo "<packagename>.tgz"
  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=""
for file in ${files}
 do
  if [ ! -d ${file} ]
   then
    list="${list} ${file}"
  fi
done

cd /
mkdir /tmp/pkg2tgz
cp -pdRP ${list} /tmp/pkg2tgz
cd /tmp/pkg2tgz
makepkg ${Package}.tgz