#!/bin/sh
# synopkgland: the SDK steps run in synopkgland_sdk; the App's dsm-scripts/preuninst runs from scripts/app/preuninst.
synopkgland_sdk() (
synopkgland_purge_args=
synopkgland_uninstall_hook() {
  delete_state=0
  remove_data=0
  for flag in "${pkgwizard_delete_state:-}" "${wizard_pkgwizard_delete_state:-}"; do
    case "$flag" in
      true|True|TRUE|yes|1) delete_state=1 ;;
    esac
  done
  for flag in "${pkgwizard_remove_additional:-}" "${wizard_pkgwizard_remove_additional:-}" "${delete_app_data:-}" "${wizard_delete_app_data:-}"; do
    case "$flag" in
      true|True|TRUE|yes|1) remove_data=1 ;;
    esac
  done
  [ "$delete_state" -eq 0 ] || synopkgland_purge_args=--delete-state
  hook_dest="${SYNOPKG_PKGDEST:-/var/packages/SynoReclaim/target}"
  hook_var="${SYNOPKG_PKGVAR:-/var/packages/SynoReclaim/var}"
  mkdir -p "$hook_var/tmp" 2>/dev/null || true
  # Its own process group, so a timeout stops the hook's children too; its
  # output goes to a file, so a lingering child cannot hold DSM's pipe open.
  set -m
  (cd "$hook_var" 2>/dev/null || cd /; exec /usr/bin/env -i PATH=/usr/bin:/bin HOME="$hook_var" TMPDIR="$hook_var/tmp" SYNOPKGLAND_DELETE_STATE="$delete_state" SYNOPKGLAND_REMOVE_DATA="$remove_data" "$hook_dest"/'app/bin/synoreclaim-app' 'uninstall') </dev/null >"$hook_var/uninstall-hook.log" 2>&1 &
  hook_pid=$!
  set +m
  # DSM's bash logs a failed kill to syslog even with 2>/dev/null, so a
  # finished hook is looked up in /proc wherever there is one.
  hook_running() {
    if [ -d /proc/self ]; then [ -d "/proc/$hook_pid" ]; else kill -0 "$hook_pid" 2>/dev/null; fi
  }
  # A process in /proc whose group is the hook's ("pid (comm) state ppid pgrp ...").
  hook_group_running() {
    cat /proc/[0-9]*/stat 2>/dev/null | awk -v group="$hook_pid" '{ sub(/.*) /, ""); if ($3 == group) found = 1 } END { exit !found }'
  }
  hook_signal() {
    if [ ! -d /proc/self ]; then
      kill -"$1" -"$hook_pid" 2>/dev/null || kill -"$1" "$hook_pid" 2>/dev/null
    elif hook_group_running; then
      kill -"$1" -"$hook_pid"
    elif hook_running; then
      kill -"$1" "$hook_pid"
    fi
  }
  hook_waited=0
  while hook_running && [ "$hook_waited" -lt 30 ]; do
    sleep 1
    hook_waited=$((hook_waited + 1))
  done
  hook_failure=
  if hook_running; then
    hook_signal TERM
    sleep 1
    hook_signal KILL
    wait "$hook_pid" 2>/dev/null
    hook_failure="timeout after 30 s"
  else
    wait "$hook_pid"
    hook_status=$?
    [ "$hook_status" -eq 0 ] || hook_failure="exit $hook_status"
  fi
  if [ -n "$hook_failure" ]; then
    echo "SynoReclaim uninstall hook failed: $hook_failure; output in $hook_var/uninstall-hook.log" >&2
    logger -t synopkgland-uninstall "SynoReclaim uninstall hook failed: $hook_failure; output in $hook_var/uninstall-hook.log" 2>/dev/null || true
  fi
  return 0
}
action=purge
[ "${SYNOPKG_PKG_STATUS:-}" != UPGRADE ] || action=stop
[ "$action" = stop ] || synopkgland_uninstall_hook
"${SYNOPKG_PKGDEST:-/var/packages/SynoReclaim/target}/executor/lifecycle" "$action" $synopkgland_purge_args --package SynoReclaim || exit $?
exit 0
)
synopkgland_app="$(dirname "$0")/app/preuninst"
"$synopkgland_app" "$@" || exit $?
synopkgland_sdk "$@"
