#! /bin/sh
# We want to use BASH, not whatever /bin/sh points to.
if test -z "$BASH"; then
  bash $0 ${1+"$@"}
  exit 0
fi


# Awk out the version from source
AWK="`which awk`"
if test -z "${AWK}"; then
 AWK="`which gawk`"
fi

ver="?.?.?"
ver=`grep "char" src/main.c | $AWK '/egg_version/ {print $5}' | sed -e 's/\"//g' | sed -e 's/\;//g'`
BUILDTS=`cat src/build.h | $AWK '{print $3}'`
builddate=`perl -e "use POSIX 'strftime';print strftime(\"%m.%d.%Y\n\",gmtime(${BUILDTS}));"`

clear
#Display a banner
bt=`grep -A 3 "char \*wbanner() {" src/misc.c | grep "switch" | sed -e "s/.*switch (randint(\(.\))) {/\1/"`
bn=$[($RANDOM % ${bt})]
banner=`grep -A 15 "char \*wbanner() {" src/misc.c | grep "case ${bn}:" | sed -e "s/.*case ${bn}: return STR(\"\(.*\)\");/\1/"`
echo -e "${banner}"
echo -e "Version:   ${ver}\nBuild:     ${builddate}"

usage() 
{
    echo
    echo "Usage: $0 [-bcCdn] [-p FILE] [all|hub|leaf]"
    echo
    echo "    The options are as follows:"
    echo "    -b        Use bzip2 instead of gzip when packaging."
    echo "    -c        Cleans up old binaries/files before compile."
    echo "    -C        Preforms a distclean before making."
    echo "    -d        Builds a debug package."
    echo "    -n        Do not package the binaries."
    echo "    -p  FILE  Used for development."
}

debug=0
all=0
hub=0
leaf=0
clean=0
type=
nopkg=0
bzip=0
pack=pack/pack.cfg
if test -f Makefile; then
  pack="`grep "CFG =" Makefile | sed -e 's/CFG = \(.*\)/\1/'`"
fi

while getopts bCcdhnp: opt ; do
        case "$opt" in
        b) bzip=1 ;;
	c) clean=1 ;;
	C) clean=2 ;;
        d) debug=1 ;;
	h) usage; exit 0 ;;
        n) nopkg=1 ;;
	p) pack="$OPTARG" ;;
        *) usage; exit 1 ;;

        esac
done

shift $(($OPTIND - 1))

PACKNAME=`grep "PACKNAME " ${pack} | $AWK '/PACKNAME/ {print $2}'`
echo "Packname:  ${PACKNAME} (${pack})"
echo -e `grep -r "http://" src/misc.c | sed -e "s/dprintf(idx, STR(\"\(.*\)\"));/\1/"`
echo

if test -z "$1"; then
 usage
 exit 1
else
 if [ $1 = "all" ]; then
  all=1
 elif [ $1 = "leaf" ]; then
  leaf=1
  type="leaf"
 elif [ $1 = "hub" ]; then
  hub=1
  type="hub"
 else
  all=1
 fi
fi

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

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

# FIXME: We need to check for the proper pack/ files no? 

# Figure what bins we're making
case `uname` in
  Linux) os=Linux;;
  FreeBSD) os=FreeBSD;;

#  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 ${PACKNAME}::${type} for $os"

MAKE="`which gmake`"
if test -z "${MAKE}"; then
 MAKE="`which make`"
fi

if [ $clean = "2" ]; then
 if test -f Makefile; then
  echo "[*] DistCleaning old files..."
  ${MAKE} distclean > /dev/null
 fi
fi

# Run ./configure, then verify it's ok
echo "[*] Configuring..."
umask 077 >/dev/null

./configure --silent --with-cfg=${pack}

if [ $clean = "1" ]; then
 echo "[*] Cleaning up old binaries/files..."
 ${MAKE} clean > /dev/null
fi

_build()
{
 if [ $[${tb}] = "1" ]; then
  echo "[*] Building ${d}${tb}..."
  ${MAKE} ${d}${tb}
  if ! test -f ${tb}; then
    echo "[!] ${d}${tb} build failed"
    exit 1
  fi
 fi
}

if [ -f stamp.hub ]; then
 tb="hub"
 _build
 tb="leaf"
else
 tb="leaf"
 _build
 tb="hub"
fi
_build


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

if test -n "$d"; then
 d="-debug"
fi

for bin in $fls
do
  mv -f $bin $bin.$os-$ver${d}
done

# Wrap it nicely up into an archive
if [ $nopkg = "0" ]; then
  echo "[*] Packaging..."
  if [ $bzip = "1" ]; then
    zip="j"
    ext="bz2"
  else
    zip="z"
    ext="gz"
  fi
  #This MALLOC business is to not Abort 'tar' due to some bug it has.
  tmp=${MALLOC_CHECK_}
  unset MALLOC_CHECK_
  tar -c${zip}f ${PACKNAME}.$os-$ver${d}.tar.${ext} *.$os-$ver${d}
  if test -n "$tmp"; then
    declare -x MALLOC_CHECK_=$tmp
  fi
  rm -f *$os.$ver${d}
  echo "Binaries are now in '${PACKNAME}.$os-$ver${d}.tar.${ext}'."
fi
  exit 0

