#!/bin/sh
# preinst: runs before DSM copies the package files, at install and at the
# install half of an upgrade (SYNOPKG_PKG_STATUS=INSTALL or UPGRADE).
#
# Order: the SDK's preinst steps first, then this script. If the SDK steps
# fail, this script does not run.
#
# 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
#
# Install wizard answers arrive as environment variables named by each item's
# key ("true", "false" or text). The SDK's own wizard keys are not for you.
#
# Exit code: 0 continues; non-zero aborts the install or upgrade (DSM's own
# rule). Keep this to checks: nothing undoes a change made here.
#
# 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 DSM older than 7.2.
# if [ "${SYNOPKG_DSM_VERSION_MAJOR:-7}" -eq 7 ] && [ "${SYNOPKG_DSM_VERSION_MINOR:-0}" -lt 2 ]; then
#   echo "This App needs DSM 7.2 or later." > "$SYNOPKG_TEMP_LOGFILE"
#   exit 1
# fi

exit 0
