#!/bin/sh

#edit this to your homedir (no trailing slash)

#add in 'cont' support for NOT using make clean.

TCLDIR="/home/bryan"

#change this to 0 to not clean previously compiled files (faster/for dev)
clean=1


#### DO NOT EDIT BELOW THIS LINE #####

# Awk out the version from source
ver=`cat src/main.c 2>&1 | grep "char" | awk '/egg_version/ {print $4}' | sed -e 's/\"//g' | sed -e 's/\;//g'`
numver=`cat src/main.c 2>&1 | grep "int" | awk '/egg_numver =/ {print $4}' |  sed -e 's/\;//g'`
if test -z "$ver"
then
  ver="?.?.?"
fi
echo "*** wraith v${ver}/${numver} builder ***"
echo ""

debug=0
all=0
hub=0
leaf=0
#if test $# -lt 1; then
if test -z "$1"; then
 echo "Usage: $0 -d all|leaf|hub pack.cfg -c"
 echo "   -d: Builds a debug package."
# echo "   -c: Does not erase previously compiled files unless *necessary* (faster)"
 echo ""
 exit 1
elif [ $1 = "all" ]; then
 all=1
elif [ $1 = "leaf" ]; then
 leaf=1
elif [ $1 = "hub" ]; then
 hub=1
elif [ $1 = "-d" ]; then
 debug=1
 test=" "
 if ! test $# -lt 2; then
  if [ $2 = "all" ]; then
   all=1
  elif [ $2 = "leaf" ]; then
   leaf=1
  elif [ $2 = "hub" ]; then
   hub=1
  else
   all=1
   pack=$2
  fi
 else
  all=1
 fi
else
 all=1
 pack=$1
fi

d=""
if [ $debug = "1" ]; then
 d="d"
fi

if ! test $pack
 then
 if [ $debug = "1" ]; then
  pack=$3
 else
  pack=$2
 fi
fi

if [ $all = "1" ]; then
 leaf=1
 hub=1
fi

if test $pack
then
  packname=$pack
else
  packname=default
fi

# Verify we got the config file
origpack=$packname
if ! test -f $packname; then
  packname=$packname.cfg
fi
if ! test -f $packname; then
  echo "[!] Can't find pack configuration file $origpack"
  exit 1
else
  echo "[*] Using pack configuration file: $packname"
fi

# Figure what bins we're making
case `uname` in
  Linux) os=Linux;;
  FreeBSD) case `uname -r` in
    5*) os=FreeBSD5;;
    4*) os=FreeBSD4;;
    3*) os=FreeBSD3;;
  esac;;
  OpenBSD) os=OpenBSD;;
esac
if test -z $os
then
  echo "[!] Automated packaging disabled, `uname` isn't recognized"
fi

echo "[*] Building pack for $os"

# Run ./configure, then verify it's ok
echo "[*] Configuring..."
umask 077 >/dev/null
if test -z ${TCLDIR}
then
  ./configure --disable-tcl-threads > /dev/null 2>configure.temp
else
  ./configure --disable-tcl-threads --with-tcllib=${TCLDIR}/lib/libtcl8.4.a --with-tclinc=${TCLDIR}/include/tcl.h > /dev/null 2>configure.temp
fi
#if test "`cat configure.temp`"
#then
#  echo "Configure error'd"
#  cat configure.temp
#
#  exit 1
#fi
#rm -f configure.temp

# make clean, just in case
if [ $clean = "1" ]; then
 echo "[*] Cleaning up old binaries/files..."
 make clean > /dev/null
fi

# Read the config
echo -n "[*] Compiling with:"
for cnf in `cat $packname | grep -v "^#"`
do
  defines="$defines -DS_$cnf"
  echo -n "$cnf "
done
echo ""

# Build utils and check we got the bins
echo "[*] Building utils..."
make utils CFLGS="$defines" > /dev/null
if ! test -f stringfix
then
  echo "[!] Util build failed"
  exit 1
fi

if [ $leaf = "1" ]; then
 # Build leaf and check
 echo "[*] Building ${d}leaf..."
 make ${d}leaf CFLGS="$defines" > /dev/null
 if ! test -f leaf
 then
   echo "[!] ${d}leaf build failed"
   exit 1
 fi
fi
if [ $hub = "1" ]; then
 # Build hub and check
 echo "[*] Building ${d}hub..."
 make ${d}hub CFLGS="$defines" > /dev/null
 if ! test -f hub
 then
   echo "[!] ${d}hub build failed"
   exit 1
 fi
fi


# Wrap it nicely up into an archive
echo "[*] Packaging..."
packname=`basename $packname .cfg`
if [ $all = "1" ]; then
 fls="leaf hub"
elif [ $leaf = "1" ]; then
 fls="leaf"
elif [ $hub = "1" ]; then
 fls="hub"
fi

for bin in $fls
do
  mv -f $bin $bin.$os.$numver
done
tar -zcf $packname.$os.$numver.tgz *.$os.$numver
rm -f *.$os.$numver
echo "Binaries are now in '$packname.$os.$numver.tgz'."
exit 0

