#!/bin/sh

[ "$SYNOPKG_PKGDEST" ] || SYNOPKG_PKGDEST="/volume1/@appstore/SynoBoxBase"

# Package
PACKAGE="SynoBoxBase"
DNAME="SynoBox Base"

SCRIPTS_DIR="$(dirname $0)"
PACKAGE_DIR=/var/packages/$PACKAGE

# Others
INSTALL_DIR="${SYNOPKG_PKGDEST}"
INSTALL_APP="${SYNOPKG_PKGDEST}/app"
LOG_FILE="${INSTALL_APP}/install.log"

check1 ()
{
    [ $1 -ne 0 ] && R=$((R+1))
    [ $1 -eq 0 ] && echo -n "$2... yes" || echo -n "$2... no"
    [ $1 -ne 0 ] && [ "$3" != "" ] && echo -n " (found $3)"
    echo ""
}

check2 ()
{
    [ $1 -ne 0 ] && W=$((W+1))
    [ $1 -eq 0 ] && echo -n "$2... yes" || echo -n "$2... no"
    [ $1 -ne 0 ] && [ "$3" != "" ] && echo -n " (found $3)"
    echo ""
}

check ()
{
    R=0
    W=0
    echo "-- check errors --"
    ( cd ${INSTALL_DIR} && chown -R root: . &> /dev/null )
    [ -d ${INSTALL_DIR} ] && find ${INSTALL_DIR} -user 1000 -exec chown root: {} \; &> /dev/null
    [ -d ${INSTALL_APP} ] &> /dev/null ; ok=$? ; check1 $ok "checking for app folder"
    x=$( cd ${INSTALL_APP} ; id -un 2>&1 ) ; [ "$x" == "root" ] ; ok=$? ; check1 $ok "checking for root current user" "$x"
    x=$( cd ${INSTALL_APP} ; ./busybox &> /dev/null ) ; ok=$? ; check1 $ok "checking for busybox"
    x=$( cd ${INSTALL_APP} ; ./busybox stat -c %a mypython 2>&1 ) ; [ "$x" == "755" ] ; ok=$? ; check1 $ok "checking for mypython 755 permissions" "$x"
    x=$( cd ${INSTALL_APP} ; ./mypython --version 2>&1 ) ; [ "$x" == "Python 3.3.2" ] ; ok=$?  ; check1 $ok "checking for mypython 3.3.2 version" "$x"
    x=$( cd ${INSTALL_APP} ; echo -e "import concurrent.futures" | ./mypython &> /dev/null ) ; ok=$? ; check1 $ok "checking for mypython module: concurrent.futures"
    x=$( cd ${INSTALL_APP} ; echo -e "import logging" | ./mypython &> /dev/null ) ; ok=$? ; check1 $ok "checking for mypython module: logging"
    x=$( cd ${INSTALL_APP} ; echo -e "import multiprocessing" | ./mypython &> /dev/null ) ; ok=$? ; check1 $? "checking for mypython module: multiprocessing"
    x=$( cd ${INSTALL_APP} ; echo -e "import sqlite3" | ./mypython &> /dev/null ; ok=$? ) ; check1 $ok "checking for mypython module: sqlite3"
    x=$( cd ${INSTALL_APP} ; echo -e "import ssl" | ./mypython &> /dev/null ) ; ok=$? ; check1 $ok "checking for mypython module: ssl"
    x=$( cd ${INSTALL_APP} ; echo -e "import sys\nprint(sys.getdefaultencoding())" | ./mypython 2>&1 ) ; [ "$x" == "utf-8" ] ; ok=$? ; check1 $ok "checking for utf-8 default encoding" "$x"
    x=$( cd ${INSTALL_APP} ; echo -e "import sys\nprint(sys.getfilesystemencoding())" | ./mypython 2>&1 ) ; [ "$x" == "ascii" ] ; ok=$? ; check1 $ok "checking for ascii file system encoding" "$x"
    echo "errors found: $R"
    return $R
}

do_check ()
{
    check &> ${SCRIPTS_DIR}/install.log ; ok=$?
    [ $ok -eq 0 ] && echo "Installation successfull" || ( echo "Installation failed !" ; echo "---------------------" ; cat ${SCRIPTS_DIR}/install.log )
    [ -d ${INSTALL_APP} ] && cp ${SCRIPTS_DIR}/install.log ${LOG_FILE}
    return $ok
}

preinst ()
{
    exit 0
}

postinst ()
{
    # Check system
    if [ "${SYNOPKG_PKG_STATUS}" != "UPGRADE" ]; then
        do_check || exit 1
    fi

    exit 0
}

preuninst ()
{
    exit 0
}

postuninst ()
{
    exit 0
}

preupgrade ()
{
    exit 0
}

postupgrade ()
{
    # Check system
    do_check || exit 1

    exit 0
}

