#!/bin/bash
#
# $Id: nsb-package,v 1.1.1.1 2004/01/27 00:30:19 dlehman Exp $
# Author: Robert Story <rstory@freesnmp.com>
#
########################################################################
########################################################################

usage()
{
   echo "Usage: $0  [-c] [-d] [-p] [-s SRCD] [-b BUILDD] [-i INSTALLD] VERSION"
   echo ""
   echo " VERSION    : relase number (eg 5.0.3)"
   echo " -s SRCDIR  : soure directory [$HOME/src/net-snmp-VERSION]"
   echo " -b BUILDD  : build directory [$HOME/build/]"
   echo "              NOTE-platform will be appended to build directory"
   echo " -i INSTALLD: install directory [$HOME/build/\$PLATFORM/usr/local]"
   echo ""
   echo " -x : configure extra features for pre-release testing"
   echo " -c : skip configure"
   echo " -d : dirty build (don't make distclean)"
   echo " -m : skip 'make all'"
   echo " -p : don't pause at prompts in between stages"
   echo " -r : remove build dir (rm -fR) before build"
   echo " -R : remove build dir (rm -fR) after successful build"
   echo " -t : skip 'make test'"
   exit 1
}

#trap exit SIGINT

#set -x

#
# find nsb-platform based on te path to this script
#
EXE_PATH=${0%nsb-package}
EXE_PATH=${EXE_PATH%/}
if [ -f $EXE_PATH/nsb-functions ];then
   source $EXE_PATH/nsb-functions
elif [ -f $HOME/bin/nsb-functions ]; then
   source $HOME/bin/nsb-functions
elif [ -f nsb-functions ];then
   source nsb-functions
else
   echo "Cannot find nsb-functions in $EXE_PATH, $HOME/bin or $PWD"
   exit 1
fi


########################################################################
########################################################################

REMOVE_SUCCESS=0

#      x)  x=$OPTARG ;;
while getopts b:cdi:mprRs:tx opt
do
    case "$opt" in
      b)  BUILDD=$OPTARG ;;
      c)  NSB_SKIP_CONFIG=1 ;;
      d)  NSB_CLEAN=0 ;;
      i)  INSTALLD=$OPTARG ;;
      m)  NSB_SKIP_BUILD=1 ;;
      p)  NSB_PROMPT=0 ;;
      r)  NSB_CLEAN=2 ;;
      R)  REMOVE_SUCCESS=1 ;;
      s)  SRCD=$OPTARG ;;
      t)  NSB_SKIP_TEST=1 ;;
      x)  NSB_CONFIG_ALL=1;;
      \?)# unknown flag
        usage;;
    esac
done
shift `expr $OPTIND - 1`

if [ $# -ne 1 ]; then
   echo "expecting 1 argument, got $# ($@)"
   usage
fi

if [ $NSB_CLEAN -eq 1 ]; then
   if [ $NSB_SKIP_CONFIG -eq 1 ]; then
      echo "A clean build also requires configuration (-d and -c"
      echo "cannot both be specified)."
      usage
   fi
fi

VERSION=$1
if [ -z "$SRCD" ]; then
   SRCD=$HOME/src/net-snmp-$VERSION
fi
if [ -z "$BUILDD" ]; then
   BUILDD=$HOME/build
fi
if [ $SRCD != $BUILDD ];then
    BUILDD=$BUILDD/$VERSION-`nsb-sysname`
fi
if [ -z "$INSTALLD" ]; then
   INSTALLD=$BUILDD/usr/local
fi


########################################################################
########################################################################
nsb-prompt "press enter to build $SRCD in $BUILDD, and install in $INSTALLD"
nsb-build $VERSION $SRCD $BUILDD $INSTALLD $NSB_CONFIG_ALL
rc=$?
if [ $rc -eq 0 ] && [ $REMOVE_SUCCESS -eq 1 ];then
    nsb-prompt "press enter to remove $BUILDD"
    /bin/rm -fR $BUILDD > /dev/null 2>&1
fi

exit $rc
