#!/bin/bash
#
# $Id: nsb-nightly,v 1.7 2004/07/03 20:37:14 rstory Exp $
# Author: Robert Story <rstory@freesnmp.com>
#
########################################################################
########################################################################

usage()
{
   echo "Usage: $0  [optional params] VERSION"
   echo ""
   echo " optional params: [-x] [-s SRCD] [-b BUILDD] [-i INSTALLD]"
   echo "                  [-h host] [-p path]"
   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 : don't source $HOME/.snmp/nsb-nightly"
   echo ""
   echo " -h : host to upload results to"
   echo " -p : path to upload results to"
   exit 1
}

#set -x

#
# find nsb-platform based on the path to this script
#
EXE_PATH=${0%nsb-nightly}
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-nightly ];then
        source $HOME/.snmp/nsb-nightly
    fi
fi

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

# -r : remove build dir before
# -R : remove build dir after
PARMS="$PARMS -r"
NIGHTLY_VERBOSE=0

while getopts b:i:s:xh:p:v opt
do
    case "$opt" in
      b)  NSB_BUILD_DIR=$OPTARG;;
      i)  PARMS="-i $OPTARG $PARMS";;
      s)  NSB_SRC_DIR=$OPTARG;;
      h)  NSB_SCP_HOST=$OPTARG
          PARMS="-h $NSB_SCP_HOST $PARMS"
          ;;
      p)  NSB_SCP_DEST=$OPTARG
          PARMS="-p $NSB_SCP_DEST $PARMS"
          ;;
      x)  PARMS="-x $PARMS";;
      v)  NIGHTLY_VERBOSE=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

PARMS="$PARMS -b $NSB_BUILD_DIR -s $NSB_SRC_DIR"

########################################################################
########################################################################
echo "Start: `date`"
echo "Running nsb-package $PARMS $NSB_VERSION"
if [ $NIGHTLY_VERBOSE = 1 ]; then
    $EXE_PATH/nsb-package $PARMS $NSB_VERSION
else
    $EXE_PATH/nsb-package $PARMS $NSB_VERSION > /dev/null
fi
RC=$?
echo "Finish: `date`"
echo -n "Result: "
case $RC in
    0)  echo "Success!"
        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
        ;;
    *) echo "Error!"
       if [ ! -z $NSB_SCP_HOST ]; then
          nsb-upload $NSB_BUILD_DIR $NSB_SCP_HOST:$NSB_SCP_DEST/$NSB_VERSION/$NSB_SUFFIX
       fi
        ;;
esac

exit $RC
