#!/bin/sh
synopkgland_short_path_error=$(synopkgland_short_path_check() (
  pkg='SynoPkgConnect'
  unavailable() { echo "Cannot check whether short path $1 is free: $2 is unreadable. Restore it and retry the installation."; exit 1; }
  taken() { echo "Short path $1 is already used by $2. Choose another ui.short_paths path and rebuild the package."; exit 1; }
  overlap() {
    synopkgland_b=${2%/}
    [ "$1" = "$synopkgland_b" ] && return 0
    case "$synopkgland_b" in ("$1"/*) return 0 ;; esac
    case "$1" in ("$synopkgland_b"/*) return 0 ;; esac
    return 1
  }
  lower() { printf '%s' "$1" | tr 'A-Z' 'a-z'; }
  alias_taken() {
    [ -r "$2" ] || unavailable "$1" "$2"
    synopkgland_a=$(tr '\n' ' ' < "$2" | sed -n -E 's/.*"alias"[[:space:]]*:[[:space:]]*"([^"]*)".*/\1/p' | sed 's#\\/#/#g; s#^/*##; s#/*$##')
    [ -n "$synopkgland_a" ] && overlap "$1" "/$synopkgland_a"
  }
  reaches() {
    [ -r "$2" ] || unavailable "$1" "$2"
    synopkgland_s=$1
    sed 's/#.*//' "$2" | tr '\n' ' ' | grep -o -E 'location[[:space:]]+((=|\^~|~\*?)[[:space:]]+)?("[^"]*"|[^[:space:]{]+)[[:space:]]*\{' | {
      while IFS= read -r synopkgland_m; do
        synopkgland_m=${synopkgland_m#location}
        synopkgland_m=${synopkgland_m%\{}
        set -f; set -- $synopkgland_m; set +f
        if [ $# -ge 2 ]; then synopkgland_mod=$1; synopkgland_p=$2; else synopkgland_mod=; synopkgland_p=$1; fi
        synopkgland_p=${synopkgland_p#[\"\']}; synopkgland_p=${synopkgland_p%[\"\']}
        case "$synopkgland_mod" in
          ("~"|"~*")
            synopkgland_i=
            [ "$synopkgland_mod" = "~*" ] && synopkgland_i=-i
            synopkgland_l=$(printf '%s\n' "$synopkgland_p" | sed -n -E 's#^\^(/[A-Za-z0-9_/-]+).*#\1#p')
            if [ -n "$synopkgland_l" ]; then
              if [ -n "$synopkgland_i" ]; then overlap "$(lower "$synopkgland_s")" "$(lower "$synopkgland_l")" && exit 0
              else overlap "$synopkgland_s" "$synopkgland_l" && exit 0; fi
              case "$synopkgland_l" in (*/) continue ;; esac
            fi
            for synopkgland_probe in "$synopkgland_s" "$synopkgland_s/" "$synopkgland_s/index.html"; do
              printf '%s\n' "$synopkgland_probe" | grep -q -E $synopkgland_i -e "$synopkgland_p" 2>/dev/null
              [ $? -eq 1 ] || exit 0
            done ;;
          (*)
            [ "$synopkgland_p" = / ] && continue
            overlap "$synopkgland_s" "$synopkgland_p" && exit 0
            if [ "$synopkgland_mod" != "=" ]; then case "$synopkgland_s/" in ("$synopkgland_p"*) exit 0 ;; esac; fi ;;
        esac
      done
      exit 1
    }
  }
  for synopkgland_path in /m; do
    synopkgland_dir=/usr/syno/synoman
    [ -d "$synopkgland_dir" ] && [ -r "$synopkgland_dir" ] || unavailable "$synopkgland_path" "$synopkgland_dir"
    { [ -e "$synopkgland_dir$synopkgland_path" ] || [ -L "$synopkgland_dir$synopkgland_path" ]; } && taken "$synopkgland_path" "DSM"
    synopkgland_dir=/usr/syno/etc/www/app.d
    [ -d "$synopkgland_dir" ] && [ -r "$synopkgland_dir" ] && [ -x "$synopkgland_dir" ] || unavailable "$synopkgland_path" "$synopkgland_dir"
    for synopkgland_f in "$synopkgland_dir"/*.json; do
      [ -e "$synopkgland_f" ] || continue
      alias_taken "$synopkgland_path" "$synopkgland_f" && taken "$synopkgland_path" "an Application Portal alias"
    done
    [ -d /var/packages ] && [ -r /var/packages ] && [ -x /var/packages ] || unavailable "$synopkgland_path" /var/packages
    for synopkgland_pkg in /var/packages/*; do
      synopkgland_n=${synopkgland_pkg##*/}
      [ "$synopkgland_n" = "$pkg" ] && continue
      for synopkgland_rel in sso/native-nginx.conf daemon/ingress-nginx.conf etc/short-paths-nginx.conf; do
        synopkgland_f=$synopkgland_pkg/target/$synopkgland_rel
        [ -e "$synopkgland_f" ] || continue
        reaches "$synopkgland_path" "$synopkgland_f" && taken "$synopkgland_path" "package $synopkgland_n"
      done
      synopkgland_f=$synopkgland_pkg/target/etc/portal.json
      [ -e "$synopkgland_f" ] && alias_taken "$synopkgland_path" "$synopkgland_f" && taken "$synopkgland_path" "package $synopkgland_n"
    done
    synopkgland_dir=/usr/local/etc/nginx/conf.d
    [ -d "$synopkgland_dir" ] && [ -r "$synopkgland_dir" ] && [ -x "$synopkgland_dir" ] || unavailable "$synopkgland_path" "$synopkgland_dir"
    for synopkgland_f in "$synopkgland_dir"/dsm.*.conf "$synopkgland_dir"/alias.*.conf; do
      [ -e "$synopkgland_f" ] || [ -L "$synopkgland_f" ] || continue
      [ -d "$synopkgland_f" ] && continue
      case "${synopkgland_f##*/}" in ("dsm.pkg-static.$pkg-"*) continue ;; esac
      reaches "$synopkgland_path" "$synopkgland_f" && taken "$synopkgland_path" "DSM or another package (${synopkgland_f##*/})"
    done
  done
  exit 0
); synopkgland_short_path_check 2>&1) || {
  printf '%s\n' "$synopkgland_short_path_error" >&2
  if [ -n "${SYNOPKG_TEMP_LOGFILE:-}" ]; then printf '%s\n' "$synopkgland_short_path_error" > "$SYNOPKG_TEMP_LOGFILE"; fi
  exit 1
}
# synopkgland: the SDK steps run in synopkgland_sdk; the App's dsm-scripts/postinst runs from scripts/app/postinst.
synopkgland_sdk() (
exit 0
)
synopkgland_app="$(dirname "$0")/app/postinst"
synopkgland_sdk "$@" || exit $?
exec "$synopkgland_app" "$@"
