#!/bin/sh

# Package configuration values
PACKAGE="rutorrent"
DNAME="ruTorrent"
RUNAS="rutorrent"

# Find the CPU architecture
synoinfo=`get_key_value /etc.defaults/synoinfo.conf unique`
arch=`echo $synoinfo | cut -d_ -f2`

# Common definitions
INSTALL_DIR="/usr/local/${PACKAGE}"
BIN_DIR="${INSTALL_DIR}/bin"
CONFIG_DIR="${INSTALL_DIR}/var"
# Assume that ${SYNOPKG_PKGDEST} is a link to /volumeX/@appstore/${PACKAGE}
TMP_BASE=`realpath ${SYNOPKG_PKGDEST} | cut -d/ -f1-2`/@tmp
UPGRADE_FLAG_FILE=/tmp/${PACKAGE}-upgrade
PATH="${INSTALL_DIR}/sbin:${INSTALL_DIR}/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin"
SYNO3APP="/usr/syno/synoman/webman/3rdparty"


#########################################
# DSM package manager functions
preinst ()
{
    # Check if the architecture is supported
    case $arch in
        x86|bromolow|cedarview|evansport|avoton)
            true
            ;;
        *)
            cat << EOM
Your architecture is not supported by this package, sorry.
Architecture  : $arch
Synology info : $synoinfo
EOM
        exit 1
            ;;
    esac
    exit 0
}

postinst ()
{
    # Remove the DSM user (legacy)
#    if /usr/syno/sbin/synouser --enum local | grep "^${RUNAS}$" >/dev/null
#    then
#        /usr/syno/sbin/synouser --del ${RUNAS} 2> /dev/null
#    fi

    # Link the installed file in the view directory
    ln -s ${SYNOPKG_PKGDEST} ${INSTALL_DIR}

    ${INSTALL_DIR}/sbin/htpasswd -cbd ${INSTALL_DIR}/etc/httpd/.htpasswd ${wizard_username:=admin} ${wizard_password:=admin}

    if [ ! -e ${INSTALL_DIR}/etc/httpd/server.key ]; then
    # https server key.
    ${INSTALL_DIR}/bin/openssl genrsa -rand /var/log/dmesg -out ${INSTALL_DIR}/etc/httpd/server.key 1024
    # CSR
    sed -e "6 s/ruTorrent/${wizard_hostname:=diskstation}/" ${INSTALL_DIR}/etc/certparams | ${INSTALL_DIR}/bin/openssl \
    req -new -key ${INSTALL_DIR}/etc/httpd/server.key -out ${INSTALL_DIR}/etc/httpd/server.csr
    # Signing certificate
    ${INSTALL_DIR}/bin/openssl x509 -req -days 999999 -in ${INSTALL_DIR}/etc/httpd/server.csr -signkey ${INSTALL_DIR}/etc/httpd/server.key \
      -out ${INSTALL_DIR}/etc/httpd/server.crt
    fi

    # DSM Desktop icon
#    ln -s ${INSTALL_DIR}/app ${SYNO3APP}/${PACKAGE}

#    ln -s /var/packages/${DNAME}/scripts/start-stop-status /usr/local/bin/${PACKAGE}-ctl 

    # Correct the files ownership
    mkdir -p /volume1/rutorrent/.session
    mkdir -p /volume1/rutorrent/.torrent
    mkdir -p /volume1/rutorrent/download
    rm -rf ${INSTALL_DIR}/home
    ln -s /volume1/rutorrent ${INSTALL_DIR}/home
#
    mkdir -p /volume1/rutorrent/users
    rm -rf ${INSTALL_DIR}/var/www/html/share/users
    ln -s /volume1/rutorrent/users ${INSTALL_DIR}/var/www/html/share/users
#
    if [ ! -e /volume1/rutorrent/rtorrent.conf ] ; then
	cp -f ${INSTALL_DIR}/etc/rtorrent.conf /volume1/rutorrent
    fi
    rm -f ${INSTALL_DIR}/etc/rtorrent.conf
    ln -s /volume1/rutorrent/rtorrent.conf ${INSTALL_DIR}/etc/rtorrent.conf
#
    chown -R root:root ${SYNOPKG_PKGDEST}
    chown -R admin:users /volume1/rutorrent
    chown -R nobody:nobody /volume1/rutorrent/users
    chown -R nobody:nobody ${INSTALL_DIR}/var/www

    exit 0
}

preuninst ()
{
    # Remove the user (if not upgrading)
#    if [ -f ${UPGRADE_FLAG_FILE} ]
#    then
#        deluser ${RUNAS}
#    fi

    # Remove the DSM desktop icon
#    rm ${SYNO3APP}/${PACKAGE}
    
    exit 0
}

postuninst ()
{
    # Remove symlinks to utils
#    for exe in ${TR_UTILS}
#    do
#      rm /usr/local/bin/${exe}
#    done
#     rm -f /usr/local/bin/${PACKAGE}-ctl 
#    # Remove link
     rm -f ${INSTALL_DIR}

    exit 0
}

preupgrade ()
{
#    # Make sure the package is not running while we are upgrading it
#    /usr/local/bin/${PACKAGE}-ctl stop
#
#    TMP_DIR=${TMP_BASE}/${PACKAGE}-$$
#    echo ${TMP_DIR} > ${UPGRADE_FLAG_FILE}
#
#    # Save the users settings before the upgrade
#    # Mind the order here!
#    for config_dir in /usr/local/var/transmission \
#                      ${INSTALL_DIR}/var \
#                      ${SYNOPKG_PKGDEST}/var \
#                      ${SYNOPKG_PKGDEST}/usr/local/var/lib/transmission-daemon
#    do
#        if [ -d ${config_dir} ]
#        then
#            mv ${config_dir} ${TMP_DIR}
#            break
#        fi
#    done

    exit 0
}

postupgrade ()
{
    exit 0
}

