#!/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 accept the following command
# line arguments:
#
#   install - installs any dependancies
#   postinstall - performs any needed post install actions
#   remove - uninstalls any dependancies
#
# This script can be fully customized if you need to install non-ipk
# dependancies or no dependancies at all.
#
# This default template will read the p_manifest.json file that must be
# located in the provisioning directory of the application.
# It will process the p_manifest.json file and install any .ipk files listed
# that exist in the provisioning directory.  The format of the dependency
# manifiest is:
#
# {
#   pkgs: [
#	    { “FileName” : “file1.ipk”, “type” : “ipk”, “PkgName” : “name” },
#	    { “FileName” : “file2.ipk”, “type” : “ipk”, “PkgName : “name” }
#   ]
# }
#
# FileName : the name of the .ipk file to install
# type : for now just "ipk" is supported
# PkgName : name of the ipk package that appears when opkg list-packages is run


# The provisioning (dependency manifest)
MANIFEST="./provisioning/p_manifest.json"

# opkg paths for installing and removing ipk dependencies
OPKG_CMD_PREFIX="opkg install --force-depends ./provisioning/"
OPKG_CMD_PREFIX_R="opkg remove --force-depends "

# This function is called via the install argument during app installation. It
# reads the p_manifest.json file in the provisioning directory of the
# application, and executes an "opkg install" on each .ipk package file listed
# in the p_manifest.

function install_packages {
    if [ ! -f "$MANIFEST" ]; then
        return
    fi
    JSONTXT=$(<./provisioning/p_manifest.json)
    PKGCNT=$(echo $JSONTXT | jsparser --count -p /pkgs)

    for ((i=0; i < PKGCNT; i++))
    do
        PKG=$(echo $JSONTXT | jsparser --jsobj -p /pkgs/$i)
        PKGNM=$(echo $PKG | jsparser -p /FileName)
        PKGTYPE=$(echo $PKG | jsparser -p /type)

        if [ "$PKGTYPE" == "ipk" ]; then
            PKGCMD=$OPKG_CMD_PREFIX$PKGNM
            echo "Executing: $PKGCMD"
            eval $PKGCMD
            if [ $? != 0 ]; then
                msg="Command [$PKGCMD] failed with status $?"
                echo -n $msg
                logger -t Install $msg
                exit $?
            fi
        fi
        msg="Command [$PKGCMD] succeeded"
        echo -n $msg
        logger -t Install $msg
    done
}


# This function is called via the postinstall argument immediately after
# installation. For example, this could be used to reboot the Conduit

function post_install {
    echo "post_install"
}


# This function is called via the remove argument during app uninstall. It
# reads the p_manifest.json file in the provisioning direcgtory of the
# application and executes an "opkg remove" on each package listed
# in the p_manifest.json.

function remove_packages {
    if [ ! -f "$MANIFEST" ]; then
        echo "provisioning manifest file not found"
        return
    fi
    JSONTXT=$(<./provisioning/p_manifest.json)
    PKGCNT=$(echo $JSONTXT | jsparser --count -p /pkg)

    for ((i=0; i < PKGCNT; i++))
    do
        PKG=$(echo $JSONTXT | jsparser --jsobj -p /pkg/$i)
        PKGNM=$(echo $PKG | jsparser -p /PkgName)
        PKGTYPE=$(echo $PKG | jsparser -p /type)

        if [ "$PKGTYPE" == "ipk" ]; then
            PKGCMD=$OPKG_CMD_PREFIX_R$PKGNM
            echo "Executing: $PKGCMD"
            eval $PKGCMD
            if [ $? != 0 ]; then
                msg="Command [$PKGCMD] failed with status $?"
                echo -n $msg
                logger -t Install $msg
                exit $?
            fi
        fi
        msg="Command [$PKGCMD] succeeded"
        echo -n $msg
        logger -t Install $msg
    done
}


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

exit 0
