#!/bin/bash
# Synology postinst — 生成 ENCRYPTION_KEY
ETC="${SYNOPKG_PKGETC}"
VAR="${SYNOPKG_PKGVAR}"
mkdir -p "$ETC" "$VAR/data"
ENV_FILE="$ETC/app.env"
if [ -f "$ENV_FILE" ] && grep -q '^ENCRYPTION_KEY=' "$ENV_FILE"; then
  exit 0
fi
if command -v openssl >/dev/null 2>&1; then
  KEY="$(openssl rand -hex 32)"
else
  KEY="$(head -c 32 /dev/urandom | xxd -p -c 32)"
fi
cat >"$ENV_FILE" <<EOF
ENCRYPTION_KEY=${KEY}
PORT=3001
HOST_BIND=0.0.0.0
NODE_ENV=production
FREEAPI_DB_PATH=${VAR}/data/freeapi.db
EOF
chmod 600 "$ENV_FILE" || true
exit 0
