#!/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
make
cd cribbage ; make ; cd ..
cd sail ; make ; cd ..
cd arithmetic
strip arithmetic
cat arithmetic > $PKG/usr/games/arithmetic
man2gz arithmetic.6 $PKG/usr/man/preformat/cat6/arithmetic.6.gz $SRC/usr/man/man6/arithmetic.6
cd ../atc
strip atc
cat atc > $PKG/usr/games/atc
man2gz atc.6 $PKG/usr/man/preformat/cat6/atc.6.gz $SRC/usr/man/man6/atc.6
cd ../backgammon/backgammon
strip backgammon
cat backgammon > $PKG/usr/games/backgammon
man2gz backgammon.6 $PKG/usr/man/preformat/cat6/backgammon.6.gz $SRC/usr/man/man6/backgammon.6
cd ../teachgammon
strip teachgammon
cat teachgammon > $PKG/usr/games/teachgammon
cd ../../battlestar
strip battlestar
cat battlestar > $PKG/usr/games/battlestar
man2gz battlestar.6 $PKG/usr/man/preformat/cat6/battlestar.6.gz $SRC/usr/man/man6/battlestar.6
cd ../bcd
strip bcd
cat bcd > $PKG/usr/games/bcd
man2gz bcd.6 $PKG/usr/man/preformat/cat6/bcd.6.gz $SRC/usr/man/man6/bcd.6
cd ../bog
strip bog
cat bog > $PKG/usr/games/bog
man2gz bog.man $PKG/usr/man/preformat/cat6/bog.6.gz $SRC/usr/man/man6/bog.6
cd ../caesar
strip caesar
cat caesar > $PKG/usr/games/caesar
man2gz caesar.6 $PKG/usr/man/preformat/cat6/caesar.6.gz $SRC/usr/man/man6/caesar.6
cd ../canfield/canfield
strip canfield
cat canfield > $PKG/usr/games/canfield
man2gz canfield.6 $PKG/usr/man/preformat/cat6/canfield.6.gz $SRC/usr/man/man6/canfield.6
cd ../cfscores
strip cfscores
cat cfscores > $PKG/usr/games/cfscores
cd ../../cribbage
strip cribbage
cat cribbage > $PKG/usr/games/cribbage
man2gz cribbage.6 $PKG/usr/man/preformat/cat6/cribbage.6.gz $SRC/usr/man/man6/cribbage.6
cd ../factor
strip factor
cat factor > $PKG/usr/games/factor
man2gz factor.6 $PKG/usr/man/preformat/cat6/factor.6.gz $SRC/usr/man/man6/factor.6
cd ../fish
strip fish
cat fish > $PKG/usr/games/fish
man2gz fish.6 $PKG/usr/man/preformat/cat6/fish.6.gz $SRC/usr/man/man6/fish.6
cd ../fortune/fortune
strip fortune
cat fortune > $PKG/usr/games/fortune
man2gz fortune.6 $PKG/usr/man/preformat/cat6/fortune.6.gz $SRC/usr/man/man6/fortune.6
cd ../strfile
strip strfile
cat strfile > $PKG/usr/bin/strfile
man2gz strfile.8 $PKG/usr/man/preformat/cat8/strfile.8.gz $SRC/usr/man/man8/strfile.8
cd ../../hangman
strip hangman
cat hangman > $PKG/usr/games/hangman
man2gz hangman.6 $PKG/usr/man/preformat/cat6/hangman.6.gz $SRC/usr/man/man6/hangman.6
cd ../hunt
strip hunt huntd
cat hunt > $PKG/usr/games/hunt
cat huntd > $PKG/usr/games/lib/huntd
man2gz hunt.6 $PKG/usr/man/preformat/cat6/hunt.6.gz $SRC/usr/man/man6/hunt.6
man2gz huntd.6 $PKG/usr/man/preformat/cat6/huntd.6.gz $SRC/usr/man/man6/huntd.6
cd ../mille
strip mille
cat mille > $PKG/usr/games/mille
man2gz mille.6 $PKG/usr/man/preformat/cat6/mille.6.gz $SRC/usr/man/man6/mille.6
cd ../monop
strip monop
cat monop > $PKG/usr/games/monop
man2gz monop.6 $PKG/usr/man/preformat/cat6/monop.6.gz $SRC/usr/man/man6/monop.6
cd ../morse
strip morse
cat morse > $PKG/usr/games/morse
cd ../number
strip number
cat number > $PKG/usr/games/number
man2gz number.6 $PKG/usr/man/preformat/cat6/number.6.gz $SRC/usr/man/man6/number.6
cd ../paranoia
strip paranoia
cat paranoia > $PKG/usr/games/paranoia
cd ../pom
strip pom
cat pom > $PKG/usr/games/pom
man2gz pom.6 $PKG/usr/man/preformat/cat6/pom.6.gz $SRC/usr/man/man6/pom.6
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
man2gz rain.6 $PKG/usr/man/preformat/cat6/rain.6.gz $SRC/usr/man/man6/rain.6
cd ../robots
strip robots
cat robots > $PKG/usr/games/robots
man2gz robots.6 $PKG/usr/man/preformat/cat6/robots.6.gz $SRC/usr/man/man6/robots.6
cd ../sail
strip sail
cat sail > $PKG/usr/games/sail
man2gz sail.6 $PKG/usr/man/preformat/cat6/sail.6.gz $SRC/usr/man/man6/sail.6
cd ../snake/snake
strip snake
cat snake > $PKG/usr/games/snake
man2gz snake.6 $PKG/usr/man/preformat/cat6/snake.6.gz $SRC/usr/man/man6/snake.6
cd ../snscore
strip snscore
cat snscore > $PKG/usr/games/snscore
cd ../../trek
strip trek
cat trek > $PKG/usr/games/trek
man2gz trek.6 $PKG/usr/man/preformat/cat6/trek.6.gz $SRC/usr/man/man6/trek.6
cd ../worm
strip worm
cat worm > $PKG/usr/games/worm
man2gz worm.6 $PKG/usr/man/preformat/cat6/worm.6.gz $SRC/usr/man/man6/worm.6
cd ../worms
strip worms
cat worms > $PKG/usr/games/worms
man2gz worms.6 $PKG/usr/man/preformat/cat6/worms.6.gz $SRC/usr/man/man6/worms.6
cd ../wump
strip wump
cat wump > $PKG/usr/games/wump
man2gz wump.6 $PKG/usr/man/preformat/cat6/wump.6.gz $SRC/usr/man/man6/wump.6

# 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