#!/bin/bash

# Script details
SCRIPTNAME=`basename $0`
SCRIPT=$0
SCRIPT_ARGS=${@}

# Initialize some of the variables
if [ "$RIFT_LP_ADDR" = "" ]; then
  RIFT_LP_ADDR="localhost"
fi
PKGS=()
INSTANTIATE=0
DESC_ID=""
NS_NAME=""
LOGGING=0
RIFT_LP_PKG_UPLOAD_URL="https://${RIFT_LP_ADDR}:4567/api/upload"

######################################################################
#  Function:usage                                                    #
#           Prints usage                                             #
######################################################################
function usage() {
  cat <<EOF
  usage $SCRIPTNAME [-h] [-r launchpad-ip][-u upload-package][-i ns-service-name [-d descriptor-id]][-l]
       -h : show this message
       -r : launchpad ip address  -  defaults to RIFT_LP_ADDR enviroment variable
       -u : upload package with the package name specified
       -i : Instantiate a network service with network service name
       -d : Instantiate a network service with the specified descriptor
       -l : Log to file
EOF
}

######################################################################
#  Function:validate_args                                            #
#           Validates the passed arguments                           #
######################################################################
function validate_args () {
  if [ "$RIFT_LP_ADDR" = "" ]; then
    echo "RIFT LP address must be specified - set RIFT_LP_ADDR or specify -l option"
    usage
    exit 1
  fi
  if [ "${#PKGS[@]}" -eq 0 -a "${INSTANTIATE}" -eq 0 ]; then
    echo "One of -u or -i option must be specified"
    usage
    exit 1
  fi
  if [ "${INSTANTIATE}" -eq 1 ]; then
    if [ "${NS_NAME}" = "" -o "${DESC_ID}" = "" ]; then
      echo "Must specify both descriptor id and ns service name when -i is specified"
      usage
      exit 1
    fi
  fi
}

######################################################################
#  Function:upload_pacakage                                          #
#           Uploads a package with the passed argument               #
######################################################################
function upload_package() {
  if [ -z "$1" ]; then
    echo "upload_package: package name should be passed in as an argument"
    usage
    exit 1
  fi
  PACKAGE=$1
  curl --insecure -F "descriptor=@${PACKAGE}" ${RIFT_LP_PKG_UPLOAD_URL}
}

######################################################################
#  Function:instantiate_ns                                           #
#           Instantiates a netork service                            #
######################################################################
function instantiate_ns() {
  echo "instantiate_ns need implementation"
}


while getopts ":hl:r:u:i:n:" OPTION
do
    case $OPTION in
        h)
            usage
            exit 1
            ;;
        r)
            RIFT_LP_ADDR=$OPTARG
            RIFT_LP_PKG_UPLOAD_URL="https://${RIFT_LP_ADDR}:4567/api/upload"
            ;;
        u)
            PKGS+=($OPTARG)
            ;;
        i)
            INSTANTIATE=1
            NS_NAME=$OPTARG
            ;;
        n)
            DESC_ID=$OPTARG
            ;;
        l)
            LOGGING=1
            ;;
        *)
            usage
            exit 1
            ;;
    esac
done

shift $((OPTIND-1))

validate_args

if [ $LOGGING -eq 1 ]; then
    LOGDIR="/tmp"
    LOGFILE="$LOGDIR/$SCRIPTNAME-$DATE.log"
    echo "Logging to file $LOGFILE"

    # Redirect stdout ( > ) and stderr to file
    # and store the STDOUT and STDERR for later use
    exec 3>&1
    exec 4>&2
    exec >$LOGFILE
    exec 2>&1
fi

echo "Started at $DATE"

# Iterate through the packages and upload them
for PKG in "${PKGS[@]}"
do
  echo "Uploading package $PKG"
  upload_package $PKG
  echo ""
done

if [ "${INSTANTIATE}" -eq 1 ]; then
  instantiate_ns $DESC_ID
fi

echo "Ended at $DATE"
