#!/bin/sh
# preuninst: runs before DSM deletes the package files, after DSM stopped the
# package. It runs at uninstall (SYNOPKG_PKG_STATUS=UNINSTALL) AND during an
# upgrade (UPGRADE), where it is the OLD version's script.
#
# Order: this script first, then the SDK's preuninst steps (stop, the App's
# uninstall.purge, executor cleanup).
#
# Runs as the package account, never as root. Root work belongs in root/*.sh
# through the SDK executor (synopkgland add root-ops).
#
# Environment (DSM sets these; the SDK passes them through unchanged):
#   SYNOPKG_PKGNAME, SYNOPKG_PKGVER   package id and the version being installed
#   SYNOPKG_OLD_PKGVER                the previous version (upgrade only)
#   SYNOPKG_PKG_STATUS                INSTALL, UPGRADE, UNINSTALL, START, STOP,
#                                     or empty at boot and shutdown
#   SYNOPKG_PKGDEST                   installed files (/var/packages/<id>/target)
#   SYNOPKG_PKGVAR, SYNOPKG_PKGHOME   package data and home; kept across upgrades
#   SYNOPKG_TEMP_LOGFILE              write a message for the user here
#   SYNOPKG_DSM_LANGUAGE              DSM language code (enu, cht, chs, jpn, ...)
#   SYNOPKG_DSM_VERSION_MAJOR, _MINOR, _BUILD, SYNOPKG_DSM_ARCH
#   SYNOPKG_USERNAME                  who started the action; empty when DSM did
#
# Uninstall wizard answers arrive as environment variables named by each
# item's key. The SDK's uninstall wizard already asks about deleting app data.
#
# Exit code: 0 continues; non-zero aborts the uninstall or upgrade before the
# SDK steps run (DSM's own rule).
#
# To tell the user why something failed, write plain text to the message file
# and exit non-zero; DSM shows it after the action:
#   echo "Port 8080 is already in use." > "$SYNOPKG_TEMP_LOGFILE"
#   exit 1
#
# Do not:
#   - start, stop or clean up what the SDK owns (daemon, background worker,
#     executor, portal, Shared Folder, app data deletion, uninstall.purge);
#   - need root or try to gain it;
#   - block for long: neither DSM nor the SDK documents a timeout, so a script
#     that hangs blocks Package Center.
#
# More: synopkgland skill show lifecycle

# Example: on a real uninstall only, export the App's settings to a file the
# user keeps.
# if [ "${SYNOPKG_PKG_STATUS:-}" = UNINSTALL ]; then
#   cp "$SYNOPKG_PKGVAR/settings.json" "$SYNOPKG_PKGHOME/settings-backup.json" 2>/dev/null || true
# fi

exit 0
