#!/bin/sh
# synopkgland: the SDK steps run in synopkgland_sdk; the App's dsm-scripts/postinst runs from scripts/app/postinst.
synopkgland_sdk() (
synopkgland_ingress_error=$("$(dirname "$0")/ingress-check" ingress-check 'SynoPkgOpenList' '/synopkgland/SynoPkgOpenList/' 2>&1) || {
  printf '%s\n' "$synopkgland_ingress_error" >&2
  if [ -n "${SYNOPKG_TEMP_LOGFILE:-}" ]; then printf '%s\n' "$synopkgland_ingress_error" > "$SYNOPKG_TEMP_LOGFILE"; fi
  exit 1
}
"${SYNOPKG_PKGDEST:-/var/packages/SynoPkgOpenList/target}/install/control" remember --root "${SYNOPKG_PKGDEST:-/var/packages/SynoPkgOpenList/target}" --package 'SynoPkgOpenList' || exit $?
synopkgland_native_keys() (
  set -eu
  umask 077
  v="${SYNOPKG_PKGVAR:-/var/packages/SynoPkgOpenList/var}/sso"
  if [ -L "$v" ]; then echo 'Native HTTPS state must not be a symlink.' >&2; exit 1; fi
  mkdir -p "$v"
  if [ "$(stat -c %a "$v")" != 700 ]; then echo 'Native HTTPS state must have mode 0700.' >&2; exit 1; fi
  c="$v/native-cert.pem"; k="$v/native-key.pem"
  if [ -e "$c" ] || [ -L "$c" ] || [ -e "$k" ] || [ -L "$k" ]; then
    for f in "$c" "$k"; do
      if [ -L "$f" ] || [ ! -f "$f" ] || [ "$(stat -c %a "$f")" != 600 ]; then
        echo 'Native HTTPS requires a complete private keypair with mode 0600.' >&2; exit 1
      fi
    done
    /usr/bin/openssl x509 -in "$c" -noout -checkend 0 >/dev/null || exit 1
    cp=$(/usr/bin/openssl x509 -in "$c" -pubkey -noout | /usr/bin/openssl sha256)
    kp=$(/usr/bin/openssl pkey -in "$k" -pubout | /usr/bin/openssl sha256)
    [ "$cp" = "$kp" ] || { echo 'Native HTTPS keypair does not match.' >&2; exit 1; }
    exit 0
  fi
  t=$(mktemp -d "$v/.native-tls.XXXXXX")
  trap 'rm -rf "$t"' EXIT HUP INT TERM
  cat > "$t/openssl.cnf" <<'NATIVE_TLS_CONFIG'
[req]
distinguished_name=subject
x509_extensions=extensions
prompt=no
[subject]
CN=gateway.invalid
[extensions]
basicConstraints=critical,CA:TRUE
keyUsage=critical,digitalSignature,keyEncipherment,keyCertSign
extendedKeyUsage=serverAuth,clientAuth
subjectAltName=DNS:gateway.invalid
NATIVE_TLS_CONFIG
  /usr/bin/openssl req -x509 -newkey rsa:2048 -nodes -days 3650 -config "$t/openssl.cnf" -keyout "$t/key.pem" -out "$t/cert.pem" >/dev/null 2>&1 || {
    echo 'Could not prepare the private native HTTPS connection.' >&2; exit 1;
  }
  chmod 600 "$t/key.pem" "$t/cert.pem"
  mv "$t/key.pem" "$k"
  mv "$t/cert.pem" "$c"
)
synopkgland_native_keys || exit $?
# synopkgland:install.sharedFolder {"mode":"dedicated","key":"library","name":"OpenList","permission":"ro","picker":"folder"}
set -eu
PKGVAR="${SYNOPKG_PKGVAR:-/var/packages/SynoPkgOpenList/var}"
synopkgland_folder_check() {
  "${SYNOPKG_PKGDEST:-/var/packages/SynoPkgOpenList/target}/install/control" folder-check --root "${SYNOPKG_PKGDEST:-/var/packages/SynoPkgOpenList/target}" --package 'SynoPkgOpenList' || :
}
if [ "${SYNOPKG_PKG_STATUS:-}" = UPGRADE ] && [ "${library_subpath+x}" != x ]; then
  saved=$(sed -n 's/^.*"shared_folders":{"library":"\([^"\\]*\)"}.*$/\1/p' "$PKGVAR/config.json")
  if [ "${library+x}" != x ] || [ "${library-}" = "$saved" ]; then synopkgland_folder_check; exit 0; fi
fi
name="${library:?missing shared folder}"
[ "${library_subpath+x}" = x ] || { echo "missing folder subpath answer" >&2; exit 1; }
# Empty subpath selects the root; presence is mandatory, content may be empty.
subpath="${library_subpath-}"
case "$name$subpath" in *'"'*|*'\'*|*[[:cntrl:]]*) echo "[COMPOSE_SHARE_INVALID] invalid folder selection" >&2; exit 1 ;; esac
umask 077
mkdir -p "$PKGVAR"
tmp="$PKGVAR/config.json.tmp.$$"
printf '{"schema":1,"shared_folders":{"library":"%s"},"shared_folder_paths":{"library":"%s"}}\n' "$name" "$subpath" > "$tmp"
mv "$tmp" "$PKGVAR/config.json"
# DSM data-share has acquired before postinst. Report recoverable access status
# without turning a successful installation or upgrade into a failed install.
synopkgland_folder_check
exit 0
)
synopkgland_app="$(dirname "$0")/app/postinst"
synopkgland_sdk "$@" || exit $?
exec "$synopkgland_app" "$@"
