#!/bin/sh

# Package configuration values
PACKAGE="rutorrent"
DNAME="ruTorrent"
RUNAS="rutorrent"
#TR_UTILS="transmission-cli transmission-create transmission-edit \
#          transmission-remote transmission-show"

# 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 ()
{
    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}

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

    # Create symlinks to utils
#    mkdir -p /usr/local/bin
#    for exe in ${TR_UTILS}
#    do
#      ln -s ${INSTALL_DIR}/bin/${exe} /usr/local/bin/${exe}
#    done
#    ln -s /var/packages/${PACKAGE}/scripts/start-stop-status /usr/local/bin/${PACKAGE}-ctl 
    ln -s /var/packages/${DNAME}/scripts/start-stop-status /usr/local/bin/${PACKAGE}-ctl 

    # Restore the previous user configuration, if any
#    if [ -f ${UPGRADE_FLAG_FILE} ]
#    then
#        TMP_DIR=`cat ${UPGRADE_FLAG_FILE}`
#        rm ${UPGRADE_FLAG_FILE}
#        if [ -d ${TMP_DIR} ]
#        then
#            rm -fr ${CONFIG_DIR}
#            mv ${TMP_DIR} ${CONFIG_DIR}
#        fi
#    fi

    # Update the configuration file
#    ${INSTALL_DIR}/bin/transmission-daemon -g ${CONFIG_DIR}/ -d 2> ${CONFIG_DIR}/new.settings.json
#    mv ${CONFIG_DIR}/new.settings.json ${CONFIG_DIR}/settings.json
#    chmod 600 ${CONFIG_DIR}/settings.json

    # Create the daemon user if needed
#    if grep "^${RUNAS}:" /etc/passwd >/dev/null
#    then
#        true
#    else
#    su -c "$(BIN_DIR)/adduser -D -H ${RUNAS}"
#    su -c "$(BIN_DIR)/addgroup ${RUNAS} ${RUNAS}"
#    fi

    # Create the WWW user if needed
#    if grep "^www-data:" /etc/passwd >/dev/null
#    then
#        true
#    else
#    su -c "$(BIN_DIR)/adduser -D -H www-data"
#    su -c "$(BIN_DIR)/addgroup www-data www-data"
#    fi

    # 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}/www/rutorrent/share/users
    ln -s /volume1/rutorrent/users ${INSTALL_DIR}/www/rutorrent/share/users
#
    if ! [ -f /volume1/rutorrent/rtorrent.conf ] ; then
#	echo "file exist /volume1/rutorrent/rtorrent.conf"
#	else
	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
#
    if [ -f /usr/syno/bin/ffmpeg ] ; then
        ln -s /usr/syno/bin/ffmpeg ${INSTALL_DIR}/bin/ffmpeg
    fi
#
    chown -R root:root ${SYNOPKG_PKGDEST}
#    chown -R ${RUNAS}:${RUNAS} /${INSTALL_DIR}/home
#    chown -R ${RUNAS}:${RUNAS} /volume1/rutorrent
#    chown -R www-data:www-data ${INSTALL_DIR}/www
    chown -R admin:users /volume1/rutorrent
    chown -R nobody:nobody ${INSTALL_DIR}/www

    # Correct permission and ownership of download directory
#    downloadDir=`grep download-dir ${CONFIG_DIR}/settings.json | cut -d'"' -f4`
#    if [ -n "${downloadDir}" -a -d "${downloadDir}" ]
#    then
#        chown -Rh ${RUNAS}:users ${downloadDir}
#        chmod -R g+w ${downloadDir}
#    fi
#
#    # Correct permission and ownership of incomplete directory
#    incompleteDir=`grep incomplete-dir ${CONFIG_DIR}/settings.json | cut -d'"' -f4`
#    if [ -n "${incompleteDir}" -a -d "${incompleteDir}" ]
#    then
#        chown -Rh ${RUNAS}:users ${incompleteDir}
#    fi

    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
}

