#!/bin/sh
# synopkgland: the SDK steps run in synopkgland_sdk; the App's dsm-scripts/postinst runs from scripts/app/postinst.
synopkgland_sdk() (
if [ "${SYNOPKG_PKG_STATUS:-}" != UPGRADE ]; then
  case "${delegate_home:-false}" in
    true|1|yes|on) selected_scope=home ;;
    *)
      selected_share="${delegate_share:-}"
      case "$selected_share" in
        ''|.|*..*|*/*|*'\'*|*:*|*'"'*|*'%'*|*[[:cntrl:]]*|[hH][oO][mM][eE]|[hH][oO][mM][eE][sS]|[[:space:]]*|*[[:space:]]) echo "Select a shared folder or choose home." >&2; exit 1 ;;
      esac
      selected_scope="shared:$selected_share" ;;
  esac
  umask 077
  draft_dir="${SYNOPKG_PKGVAR:-/var/packages/SynoPkgGopeed/var}"
  mkdir -p "$draft_dir"
  draft_tmp="$draft_dir/.delegate-install.$$"
  printf '{"schema":1,"scope":"%s"}\n' "$selected_scope" > "$draft_tmp"
  mv "$draft_tmp" "$draft_dir/delegate-install.json"
fi
"${SYNOPKG_PKGDEST:-/var/packages/SynoPkgGopeed/target}/executor/lifecycle" start --package SynoPkgGopeed || exit $?
exit 0
)
synopkgland_app="$(dirname "$0")/app/postinst"
synopkgland_sdk "$@" || exit $?
exec "$synopkgland_app" "$@"
