#!/bin/sh

# $Id: makepkg.sh.html,v 1.1 2005/02/11 13:23:44 john Exp $
#
# JW.

# makepkg whoson 2.03 "WHOSON implementation" -bin whoson -sbin whosond 

name="$1"; shift
version="$1"; shift
comment="$1"; shift

from=$(pwd)
tmp="tmp"
mkdir "$tmp"
cd "$tmp"


echo "$comment" > comment
echo "$comment" > description
echo "Created with makepkg.sh" >> description
echo "PWD: $from" >> description
echo "Args: $*" >> description

echo "@name $name-$version" >> packlist
echo "@arch $(uname -m)" >> packlist

for arg; do
   case "$arg" in
   -bin)
      dest="usr/local/bin"
      mode="555"
      owner="bin"
      group="bin"
      shift;;
   -sbin)
      dest="usr/local/sbin"
      mode="555"
      owner="bin"
      group="bin"
      shift;;
   -lib)
      dest="usr/local/lib"
      so_mode="555"
      a_mode="444"
      owner="bin"
      group="bin"
      shift;;
   -doc)
      dest="usr/local/share/doc/$name"
      mode="444"
      owner="root"
      group="wheel"
      shift;;
   -etc)
      dest="etc/$name"
      mode="444"
      owner="root"
      group="wheel"
      shift;;
   -man)
      dest="usr/local/man"
      mode="444"
      owner="root"
      group="wheel"
      shift;;
   *)
      base=$(basename $arg)
      if [ "$dest" = "usr/local/man" ]; then
         n=$( echo "$arg" | sed "s/^.*\.//")
         output="$dest/man$n/$base"
      elif [ "$dest" = "usr/local/lib" ]; then
         case "$base" in
         *.a)
            mode="$a_mode"
            ;;
         *.so|*.so.*)
            mode="$so_mode"
            ;;
         *)
            mode="$a_mode"
            ;;
         esac
         output="$dest/$base"
      else
         output="$dest/$base"
      fi
      echo "@mode $mode" >> packlist
      echo "@owner $owner" >> packlist
      echo "@group $group" >> packlist
      echo "$output" >> packlist
      dir=$(dirname $output)
      if [ ! -d "$dir" ]; then
         echo "Creating $dir"
         mkdir -p "$dir"
      fi
      echo "$from/$arg" "->" "$output"
      cp "$from/$arg" "$output"
      ;;
   esac
done

pkg_create -v -p / -c comment -d description -f packlist "$name-$version"
mv "$name-$version" ../"$name-$version".tgz

echo "tar tvzf ./$name-$version.tgz"
tar tvzf "../$name-$version.tgz"

# Could clean up the tmp directory too.