#!/bin/sh
#
BASEDIR=`dirname $0`
. ${BASEDIR}/package-build.inc.sh

NANOBSD="$NANO_SRC/tools/tools/nanobsd/nanobsd.sh"

usage() {
cat <<EOF
# Usage $0 <arguments>
#
# Wrapper around nanobsd.sh with autodetection of already processed steps to
# provide some failsafe net, which avoids building world and/or kernel by
# default.
#
# Rick van der Zwet <rick@wirelessleiden.nl>
#
# Arguments:
# build                         - Build NanoBSD parts which are not build yet
# build force kernel            - Build NanoBSD and force rebuilding the kernel
# build force world             - Build NanoBSD and force rebuilding world
# rebuild                       - Rebuild NanoBSD (aka force rebuilding all)
# deploy on <node> [and reboot] - Deploy the image on node and reboot if needed
EOF
}


deploy_image() {
  # Find object directory 
  img=${OBJDIR}/_.disk.image

  if [ ! -r "$img" ]; then
    p_err Source $img does not exists
    exit 1
  fi

  prompt_timeout=5
  p_warn "Going to DEPLOY $img to $host"
  $do_reboot && p_warn "AND will REBOOT the $host"
  p_warn "Press CTRL+C in $prompt_timeout seconds to CANCEL"
  sleep $prompt_timeout

  ssh $host mount || exit 1
  cat $img | ssh $host /tools/update || exit 1
  if $do_reboot; then
    ssh $host reboot
  fi
}


build_image() {
  p_info Forcefully building kernel: $FORCE_KERNEL
  p_info Forcefully building world : $FORCE_WORLD
  
  NANOBSD_EXTRA=${NANOBSD_EXTRA:-''}
  
  if [ ! -r "${NANOBSD}" ]; then
    p_err ${NANOBSD} does not exists
    exit 1
  fi
  
  if [ ! -x "${NANOBSD}" ]; then
    NANOBSD="sh ${NANOBSD}"
  fi
  
  # Find object directory 
  OBJDIR="/usr/obj/nanobsd.${NANO_NAME}"
  
  if [ -d "${OBJDIR}" ]; then
    NANOBSD_FLAGS=""
    
    # Detect succesfull buildworld
    tail -10 ${OBJDIR}/_.bw | grep 'World build completed'
    if [ $? -eq 0 -a ${FORCE_WORLD} = "no" ]; then
       p_info NO building of world
       NANOBSD_FLAGS="${NANOBSD_FLAGS} -w"
    fi  
  
    # Detect succesfull buildkernel
    tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed'
    if [ $? -eq 0 -a ${FORCE_KERNEL} = "no" ]; then
       p_info NO building of kernel
       NANOBSD_FLAGS="${NANOBSD_FLAGS} -k"
    fi  
  
  else
    p_warn Nothing yet, starting fresh
    NANOBSD_FLAGS=""
  fi
  
  # Provide verbose output by default
  COMMAND="${NANOBSD} ${NANOBSD_FLAGS} -c ${NANO_CFG_FILE} -v ${NANOBSD_EXTRA}"
  f_time ${COMMAND}
  RETVAL=$?
  
  # Verify on build failures
  tail -10 ${OBJDIR}/_.bw | grep 'World build completed'
  if [ $? -eq 1 ]; then
    p_err Building world FAILED, check ${OBJDIR}/_.bw
  fi
  tail -10 ${OBJDIR}/_.bk | grep 'Kernel build for .* completed'
  if [ $? -eq 1 ]; then
    p_err Building kernel FAILED, check ${OBJDIR}/_.bk
  fi  
  if [ $RETVAL -ne 0 ]; then
    p_err "Errors in building NanoBSD Image ($RETVAL)"
  fi
  p_info End time: `date`
  exit ${RETVAL}
}


#
# Argument parsing
#
FORCE_KERNEL=${FORCE_KERNEL:-"no"}
FORCE_WORLD=${FORCE_WORLD:-"no"}
if [ -z "$1" ]; then
  usage; exit 1
elif [ "$1" = "build" ]; then
  if [ -z "$2" ]; then
  elif [ "$2" = "force" ]; then
    if [ "$3" = "kernel" ]; then
      FORCE_KERNEL="yes"
    elif [ "$3" = "world" ]; then
      FORCE_WORLD="yes"
    else
      echo "Argument Error - '$3'"; exit 128
    fi
  else
    echo "Argument Error - '$2'"; exit 128
  fi
  build_image
elif [ "$1" = "rebuild" ]; then
  FORCE_KERNEL="yes"
  FORCE_WORLD="yes"
  build_image
elif [ "$1" = "deploy" -a "$2" = "on" ]; then
  if [ -z "$3" ]; then
      echo "Argument Error - '$3'"; exit 128
  fi
  host=$3
  do_reboot=false
  if [ -n "$4" -o -n "$5" ]; then
    if [ "$4" = "and" -a "$5" = "reboot" ]; then
      do_reboot=true
    else
      echo "Argument Error - '$4 $5'"; exit 128
    fi
  fi
  deploy_image
else
  echo "Argument Error - '$1'"; exit 128
fi

