#!/bin/sh

# Package configuration values
PACKAGE="uTorrent"
DNAME="utorrent"
RUNAS="utorrent"

# Common definitions
INSTALL_DIR=/usr/local/$DNAME
UPGRADE_FLAG_FILE=/tmp/${PACKAGE}-upgrade
PATH=${INSTALL_DIR}/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/syno/sbin:/usr/syno/bin

#########################################
# DSM package manager functions
#########################################
preinst() { exit 0 ; }
postinst()
{
    # Link the installed file in the view directory
    ln -s ${SYNOPKG_PKGDEST} ${INSTALL_DIR}
    # Add uTorrent user
    ${INSTALL_DIR}/bin/busybox adduser -h / -g "uTorrent user" -G users -s /bin/sh -S -D ${RUNAS} 2> /dev/null
    # Create symlinks to utils
    mkdir -p /usr/local/bin
    ln -s /var/packages/${PACKAGE}/scripts/start-stop-status /usr/local/bin/utsctl
    # Correct the files ownership
    mkdir -p /volume1/utorrent/download
    mkdir -p /volume1/utorrent/torrent_auto
    mkdir -p /volume1/utorrent/torrent_files
    chown -R ${RUNAS}:users ${SYNOPKG_PKGDEST}/log
    chown -R ${RUNAS}:users ${SYNOPKG_PKGDEST}/pid
    chown -R ${RUNAS}:users ${SYNOPKG_PKGDEST}/settings
    chown -R ${RUNAS}:users /volume1/utorrent
    chmod -R g+w /volume1/utorrent
    exit 0
}

preuninst()
{
    # Remove the user (if not upgrading)
    #if [ -f ${UPGRADE_FLAG_FILE} ]; then ${INSTALL_DIR}/bin/busybox deluser ${RUNAS} ; fi
    exit 0
}

postuninst() { rm -f /usr/local/bin/utsctl ; rm -f ${INSTALL_DIR} ; exit 0 ; }
preupgrade() { exit 0 ; }
postupgrade() { exit 0 ; }
