#!/bin/bash

# This Install script is used by the app-manager program to install and uninstall
# the app's dependencies.  

# app-manager requires this script to implement the install, remove, and 
# postinstall command line arguments.
#
# This script can be fully customized if you need to install non-ipk 
# dependancies or no dependancies at all.


function setup_firewall {
  msg="Opening firewall port 3000..."
  echo -n $msg
  logger -t Install $msg

  response=$(curl -skX POST -H "Content-Type: application/json" -d '{
      "__v": 2, 
      "chain": "INPUT",
      "dstPort": "3000", 
      "enabled": true, 
      "name": "allow-express-hello-world", 
      "protocol": "TCP",
      "target": "ACCEPT"
    }' http://127.0.0.1/api/v2/filters)

  # Only save and restart if the firewall rule was successfully added.
  # On updates, the response will be error because rule already exists
  if echo "$response" | grep -q success ; then
    msg="Issuing a save & restart command to the API..."
    echo -n $msg
    logger -t Install $msg
    # Send notification to the Conduit WEB UI
    notify_system EXPRESS WARNING "Rebooting after install of <strong>express-hello-world</strong>..." clean

    curl -skX POST -d '' http://127.0.0.1/api/command/save_restart
  else 
    msg="Firewall port 3000 was already open. Skipping reboot."
    echo -n $msg
    logger -t Install $msg
  fi
}

function revert_firewall {
  msg="Closing firewall port 3000..."
  echo -n $msg
  logger -t Install $msg

  response=$(curl -skX DELETE http://127.0.0.1/api/v2/filters/allow-express-hello-world)

  # Only save and restart if the firewall rule was successfully deleted.
  # If already deleted, the response will be error
  if echo "$response" | grep -q success ; then
    msg="Issuing a save command to the API..."
    echo -n $msg
    logger -t Install $msg

    curl -skX POST -d '' http://127.0.0.1/api/command/save

    # Wait 1 minute for app-manager to finish uninstall, then reboot
    msg="Triggering reboot in 1 minute..."
    echo -n $msg
    logger -t Install $msg
    # Send notification to the Conduit WEB UI
    notify_system EXPRESS WARNING "Rebooting in under 1 minute after uninstall of <strong>express-hello-world</strong>..." clean

    shutdown -r +1
  else
    msg="Firewall port 3000 was already closed. Skipping reboot."
    echo -n $msg
    logger -t Install $msg
  fi
}

case "$1" in
  # install is invoked by app-manager while installing a custom app. 
  install)
      msg="No dependencies to install"
      echo -n $msg
      logger -t Install $msg
      ;;
  # postinstall is invoked by app-manager immediately after installing a custom app. 
  postinstall)
      msg="Running post install "
      echo -n $msg
      logger -t Install $msg
      setup_firewall
      echo "$NAME."
      ;;
  # remove is invoked by app-manager while uninstalling a custom app. 
  remove)
      msg="Removing Dependencies: "
      echo -n $msg
      logger -t Install $msg
      revert_firewall
      ;;
  *)
      echo "Usage: $0 {install|postinstall|remove}" >&2
      exit 1
      ;;
esac

exit 0
