#!/bin/sh

# Package
PACKAGE="RoonServer"
# Others
SSS="/var/packages/${PACKAGE}/scripts/start-stop-status"
ROON_PKG_URL="https://download.roonlabs.net/builds/production1x/RoonServerLegacy_linuxx64.tar.bz2"
ROON_ARCHIVE="${ROON_PKG_URL##*/}"
R=0
preinst ()
{
  	ROON_DATA_DIR=`${SYNOPKG_PKGINST_TEMP_DIR}/helper-scripts/getsharelocation RoonServer`
  	ROON_PUBLIC_DIR=`${SYNOPKG_PKGINST_TEMP_DIR}/helper-scripts/getsharelocation public`
  	ROON_OFFLINE_ARCHIVE="${ROON_PUBLIC_DIR}/${ROON_ARCHIVE}"

    if [ -f ${ROON_DATA_DIR}/ROON_DEBUG_INSTALL_URL.txt ]; then
        CUSTOM_INSTALL_URL=$(<${ROON_DATA_DIR}/ROON_DEBUG_INSTALL_URL.txt)
        
        if [[ ${CUSTOM_INSTALL_URL:0:4} == "http" ]]  && [[ $(basename ${CUSTOM_INSTALL_URL}) == $(basename ${ROON_PKG_URL}) ]]; then
            ROON_PKG_URL="${CUSTOM_INSTALL_URL}"
        fi
    fi

    if [ ! -d "$ROON_DATA_DIR" ]; then
        R=1
        ERRORMESSAGE="\"<b>RoonServer</b>\" (case-sensitive!) needs to be manually created before installation.<br>You can also rename an external USB volume to \"RoonServer\".<br>(eSata drives cannot be renamed, use the description field here)."
    fi

    if [ $R -eq 0 ]; then
        if [ "${SYNOPKG_PKG_STATUS}" != "UPGRADE" ]; then 
            if [ -f ${ROON_OFFLINE_ARCHIVE} ]; then
                pushd /tmp > /dev/null
                rm -f ${ROON_ARCHIVE}
                cp "${ROON_OFFLINE_ARCHIVE}" /tmp/ > /dev/null
                R=$?
                if [ $R -ne 0 ]; then
                    ERRORMESSAGE="Local installation archive in public-Share could not been copied to the tmp directory."

                fi
            else
                pushd /tmp > /dev/null
                rm -f ${ROON_ARCHIVE}
                curl -sOL "${ROON_PKG_URL}"
                R=$?
                if [ $R -ne 0 ]; then
                    PKGDOMAIN=`echo "$ROON_PKG_URL" | awk -F/ '{print $3}'`
                    DOMAINCHECK=`/bin/curl -sL -w "%{http_code} %{url_effective}\\n" "$PKGDOMAIN" -o /dev/null | awk '{print $1}'`
                    TMPSPACE=`/bin/df -Ph /tmp | tail -1 | awk '{print $4}'`
                    wait 5;
                    ERRORMESSAGE="Could not download installation archive from Roon Labs website. Please check your internet connection.<br><b>URL:</b> $ROON_PKG_URL<br><b>HTTP status code:</b> $DOMAINCHECK<br><b>Available space in /tmp:</b> $TMPSPACE"
                fi
            fi
            if [ $R -eq 0 ]; then
                tar xjf ${ROON_ARCHIVE} -C "${SYNOPKG_PKGINST_TEMP_DIR}" > /dev/null
                R=$?
                if [ $R -ne 0 ]; then
                    ERRORMESSAGE="An error occured while extracting the installation archive. The archive might be corrupt or the tmp directory has no space left."
                fi

            fi
            rm -f ${ROON_ARCHIVE}
            popd > /dev/null
        fi
    fi
    if [ $R -ne 0 ]; then
	    echo "$ERRORMESSAGE"
	fi
	return $R
}

postinst ()
{
  if [ ! -d ${SYNOPKG_PKGDEST}/id ]; then
    mkdir  ${SYNOPKG_PKGDEST}/id
  fi
  
	exit 0
}

preuninst ()
{
    # Stop the package
    ${SSS} stop > /dev/null
    
}

postuninst ()
{
    # Delete Package directory if it contains nothing.
    if [ ! -d "$(ls -A /usr/syno/etc/packages/${PACKAGE})" ]; then
        rm -Rf /usr/syno/etc/packages/${PACKAGE}
    fi
    exit 0
}

preupgrade ()
{
    mv ${SYNOPKG_PKGDEST}/RoonServer ${SYNOPKG_PKGINST_TEMP_DIR}/RoonServer
    mv ${SYNOPKG_PKGDEST}/id ${SYNOPKG_PKGINST_TEMP_DIR}/id
    exit 0
}

postupgrade ()
{
    exit 0
}
