#!/bin/sh
# After Package Center installed the package: keeps the settings in the package's var folder and says where KumoYami
# is and how it reads the NAS's shared folders. DSM then enables the package (the data-share and video-driver workers
# run) and start-stop-status starts it. A non-zero exit would leave the package «damaged», so problems are only
# reported.
. "$(dirname "$0")/common"

DEST="${SYNOPKG_PKGDEST:-/var/packages/$PKG/target}"

mkdir -p "$PKGVAR"
if [ -f "$DEST/$SETTINGS_NAME" ]; then
  mv -f "$DEST/$SETTINGS_NAME" "$PKGVAR/$SETTINGS_NAME"
  chmod 600 "$PKGVAR/$SETTINGS_NAME"
fi
settings="$PKGVAR/$SETTINGS_NAME"
port=$(setting "$settings" PORT)
port="${port:-4890}"

address=$(ip -o -4 addr show scope global 2>/dev/null | awk '{ sub(/\/.*/, "", $4); print $4; exit }')
url="http://${address:-IP-del-NAS}:$port"
say "KumoYami está en $url (también en el menú principal de DSM), con la Biblioteca en el puerto $((port + 1)) y Archivos en el $((port + 2)). La primera vez te pide un código de configuración, que está en File Station, en la carpeta compartida de sus datos → logs → kumoyami.log; luego la contraseña del panel y la licencia." \
  "KumoYami is at $url (also in DSM's main menu), with the Library on port $((port + 1)) and Files on $((port + 2)). The first time it asks for a setup code, which is in File Station, in its data shared folder → logs → kumoyami.log; then the panel's password and the license."

# DSM gives a package no access to the other shared folders: the owner grants it the ones KumoYami should show
# (read/write to upload from Archivos: the local storage uploads and makes folders). Its own data folder needs nothing
# (the data-share resource gives it read/write), nor do folders that already let everyone or the users group in (as
# the one Container Manager makes often does). KumoYami sees a folder as soon as it is granted: no restart.
say "Para usar tus carpetas del NAS: Panel de control → Carpeta compartida → la carpeta → Editar → Permisos → desplegable «Usuario interno del sistema» → kumoyami → «Solo lectura» (o «Lectura/Escritura» si quieres subir archivos desde Archivos). Luego, en el panel de KumoYami: Almacenamientos → Añadir almacenamiento → Carpeta local → Examinar → volume1 → la carpeta." \
  "To use your NAS folders: Control Panel → Shared Folder → the folder → Edit → Permissions → «System internal user» list → kumoyami → «Read only» (or «Read/Write» if you want to upload files from Archivos). Then, in KumoYami's panel: Almacenamientos → Añadir almacenamiento → Carpeta local → Examinar → volume1 → the folder."
exit 0
