#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG=$TMP/package-bsdgames
SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
if [ ! -d $PKG ]; then
  mkdir -p $PKG # place for the package to be built
fi

# Explode the package framework:
cd $PKG
tar xzvf $CWD/_bsdgames.tar.gz

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+===================+"
echo "| bsd-games.src-1.3 |"
echo "+===================+"
cd $TMP
tar xzvf $CWD/bsd-games.src-1.3.tar.gz
cd games
zcat $CWD/bsdgames.diff.gz | patch
make
cd cribbage ; make ; cd ..
cd sail ; make ; cd ..
cd arithmetic
strip arithmetic
cat arithmetic > $PKG/usr/games/arithmetic
cd ../atc
strip atc
cat atc > $PKG/usr/games/atc
cd ../backgammon/backgammon
strip backgammon
cat backgammon > $PKG/usr/games/backgammon
cd ../teachgammon
strip teachgammon
cat teachgammon > $PKG/usr/games/teachgammon
cd ..
#cd ../battlestar
#strip battlestar
#cat battlestar > $PKG/usr/games/battlestar
cd ../bcd
strip bcd
cat bcd > $PKG/usr/games/bcd
cd ../bog
strip bog
cat bog > $PKG/usr/games/bog
cd ../caesar
strip caesar
cat caesar > $PKG/usr/games/caesar
cd ../canfield/canfield
strip canfield
cat canfield > $PKG/usr/games/canfield
cd ../cfscores
strip cfscores
cat cfscores > $PKG/usr/games/cfscores
cd ../../cribbage
strip cribbage
cat cribbage > $PKG/usr/games/cribbage
cd ../factor
strip factor
cat factor > $PKG/usr/games/factor
cd ../fish
strip fish
cat fish > $PKG/usr/games/fish
cd ../fortune/fortune
strip fortune
cat fortune > $PKG/usr/games/fortune
cd ../strfile
strip strfile
cat strfile > $PKG/usr/bin/strfile
cd ../../hangman
strip hangman
cat hangman > $PKG/usr/games/hangman
cd ../hunt
strip hunt huntd
cat hunt > $PKG/usr/games/hunt
cat huntd > $PKG/usr/games/lib/huntd
cd ../mille
strip mille
cat mille > $PKG/usr/games/mille
cd ../monop
strip monop
cat monop > $PKG/usr/games/monop
cd ../morse
strip morse
cat morse > $PKG/usr/games/morse
cd ../number
strip number
cat number > $PKG/usr/games/number
cd ../paranoia
strip paranoia
cat paranoia > $PKG/usr/games/paranoia
cd ../pom
strip pom
cat pom > $PKG/usr/games/pom
cd ../ppt
strip ppt
cat ppt > $PKG/usr/games/ppt
cd ../primes
strip primes
cat primes > $PKG/usr/games/primes
cd ../rain
strip rain
cat rain > $PKG/usr/games/rain
cd ../robots
strip robots
cat robots > $PKG/usr/games/robots
cd ../sail
strip sail
cat sail > $PKG/usr/games/sail
cd ../snake/snake
strip snake
cat snake > $PKG/usr/games/snake
#cd ../snscore
#strip snscore
#cat snscore > $PKG/usr/games/snscore
cd ../../trek
strip trek
cat trek > $PKG/usr/games/trek
cd ../worm
strip worm
cat worm > $PKG/usr/games/worm
cd ../worms
strip worms
cat worms > $PKG/usr/games/worms
cd ../wump
strip wump
cat wump > $PKG/usr/games/wump
cd ..
cp */*.6 $PKG/usr/man/man6
rm -f $PKG/usr/man/man6/battlestar.6 $PKG/usr/man/man6/snscore.6
chown root.root $PKG/usr/man/man6/*
chmod 644 $PKG/usr/man/man6/*
( cd $PKG/usr/man/man6 ; gzip -9 * )

# Build the package:
cd $PKG
tar czvf $TMP/bsdgames.tgz .

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/games
  rm -rf $PKG
fi