#!/usr/bin/env bash
#
# $Id: nsb-package,v 1.11.2.1 2005/11/12 14:44:48 tanders Exp $
# Author: Robert Story <rstory@freesnmp.com>
#
########################################################################
########################################################################

usage()
{
   echo "Usage: $0  [params] VERSION"
   echo ""
   echo "  params: [-c] [-d] [-P] [-s NSB_SRC_DIR] [-b BUILDD] [-i NSB_INSTALL_DIR]"
   echo "          [-h host] [-p path]"
   echo ""
   echo " VERSION    : release number (eg 5.2.2)"
   echo " -s NSB_SRC_DIRIR  : 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 NSB_INSTALL_DIR: 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 : 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'"
   echo " -C : don't source $HOME/.snmp/nsb-package"
   echo ""
   echo " -h : host to upload results to"
   echo " -p : path to upload results to"
   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
   if [ -f $HOME/bin/nsb-functions ]; then
      EXE_PATH=$HOME/bin
   elif [ -f nsb-functions ];then
      EXE_PATH=$PWD
   else
      echo "Cannot find nsb-functions in $EXE_PATH, $HOME/bin or $PWD"
      exit 1
   fi
fi

if [ "x$1" = "x-C" ]; then
    shift
else
    if [ -f $HOME/.snmp/nsb-package ];then
        source $HOME/.snmp/nsb-package
    fi
fi

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

REMOVE_SUCCESS=0

#      x)  x=$OPTARG ;;
while getopts b:cdh:i:mPp:rRs:tx opt
do
    case "$opt" in
      b)  NSB_BUILD_DIR=$OPTARG ;;
      c)  NSB_SKIP_CONFIG=1 ;;
      d)  NSB_CLEAN=0 ;;
      h)  NSB_SCP_HOST=$OPTARG ;;
      i)  NSB_INSTALL_DIR=$OPTARG ;;
      m)  NSB_SKIP_BUILD=1 ;;
      P)  NSB_PROMPT=1 ;;
      p)  NSB_SCP_DEST=$OPTARG ;;
      r)  NSB_CLEAN=2 ;;
      R)  REMOVE_SUCCESS=1 ;;
      s)  NSB_SRC_DIR=$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

NSB_VERSION=$1

source $EXE_PATH/nsb-functions

nsb-default-paths

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


########################################################################
########################################################################
nsb-prompt "press enter to build $NSB_SRC_DIR in $NSB_BUILD_DIR, and install in $NSB_INSTALL_DIR"
nsb-build $NSB_VERSION $NSB_SRC_DIR $NSB_BUILD_DIR $NSB_INSTALL_DIR $NSB_CONFIG_ALL
rc=$?
if [ ! -z $NSB_SCP_HOST ]; then
   nsb-upload $NSB_BUILD_DIR $NSB_SCP_HOST:$NSB_SCP_DEST/$NSB_VERSION/$NSB_HOST/
fi
if [ $rc -eq 0 ] && [ $REMOVE_SUCCESS -eq 1 ];then
    nsb-prompt "press enter to remove $NSB_BUILD_DIR"
    if [ "x$NSB_BUILD_DIR" = "/" ]; then
       nsb-abort "Not running rm -fR from /"
    else
       /bin/rm -fR $NSB_BUILD_DIR > /dev/null 2>&1
    fi
fi

nsb-prompt "press enter to exit with rc $RC"
exit $rc
