#!/bin/sh
# preupgrade: runs at the start of an upgrade, after DSM stopped the old
# version (SYNOPKG_PKG_STATUS=UPGRADE). It is the NEW version's script, but
# SYNOPKG_PKGDEST still holds the OLD version's files.
#
# Upgrade order: old start-stop-status stop, new preupgrade, old preuninst,
# old files deleted, old postuninst, new preinst, new files, new postinst,
# new postupgrade, new start-stop-status start.
#
# Order in this script: this script first, then the SDK's preupgrade steps.
#
# 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
#
# Upgrade wizard answers arrive as environment variables named by each item's
# key. A silent upgrade shows no wizard, so always have a default.
#
# Exit code: 0 continues; non-zero aborts the upgrade before the SDK steps run
# (DSM's own rule). Keep this to checks and to saving what postupgrade needs.
#
# 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: refuse a downgrade the App's data format cannot take.
# case "${SYNOPKG_OLD_PKGVER:-}" in
#   2.*) echo "Downgrading from 2.x is not supported." > "$SYNOPKG_TEMP_LOGFILE"; exit 1 ;;
# esac

exit 0
