#!/bin/bash
ACTION="$1"
shift
PACKAGE_LIST=$@

# START Define variables
# END Define Variables

status() {
    ret=$(dpkg-query -W -f '${Status}' "$@" 2>/dev/null)
}
if [ -n "$ACTION" -a "$ACTION" != "getStatus" ]; then
    echo "ACTION=${ACTION} PACKAGE_LIST=${PACKAGE_LIST}"
fi
case $ACTION in
    getStatus)
        # Warning: Nothing MUST be echoed in the script before this point
        # Type apt MUST echo ONLY "Not found", otherwise "0" (installed) "1" (not-installed) for other types not "apt"
        echo "Not found"
    ;;

    download)
        echo "download phase ..."
    ;;

    preInstall)
        echo "preInstall phase ..."
        for package in $PACKAGE_LIST; do
            if [ "$package" == "class-jam" ]; then
                status "nwjs0.30"
                if [ "$ret" == "install ok installed" ]; then
                    echo "nwjs 0.30 is installed"
                else
                    echo "installing nwjs 0.30 as dependency"
                    apt-get install -y nwjs0.30
                fi
            fi
        done
    ;;

    installPackage)
        # ommited for "apt" type
        echo "installPackage phase ..."
    ;;

    postInstall)
        echo "postInstall phase ..."
    ;;

    remove)
        echo "remove phase ..."
        for pkg in ${PACKAGE_LIST}; do
            apt-get remove -y $pkg
        done
    ;;
esac
exit 0
