<?xml version='1.0' standalone='yes'?>
<!DOCTYPE PLUGIN [
<!ENTITY name    "varaverk">
<!ENTITY author  "gmer4lfe">
<!ENTITY version "2026.05.28">
<!ENTITY launch  "varaverk/monitor">
<!ENTITY github  "https://github.com/FailedProxy/Varaverk">
<!ENTITY branch  "main">
<!ENTITY cfgdir  "/boot/config/plugins/varaverk">
<!ENTITY plugdir "/usr/local/emhttp/plugins/varaverk">
]>
<PLUGIN name="&name;" author="&author;" version="&version;" launch="&launch;"
        icon="/plugins/varaverk/icons/varaverk.png">

<CHANGES>
###2026.05.28
- Initial release
</CHANGES>

<!--
  ── Install / Update ────────────────────────────────────────────────────────────
  Runs on first install and on every plugin update.
  Clones the repo on fresh install; pulls latest on update.
  Array must be started — appdata must be available.
  ────────────────────────────────────────────────────────────────────────────────
-->
<FILE Name="&cfgdir;/install.sh" Run="/bin/bash" Method="install update">
<INLINE>
<![CDATA[
#!/bin/bash
PLUGIN="varaverk"
GITHUB="https://github.com/FailedProxy/Varaverk"
BRANCH="main"
CFG_DIR="/boot/config/plugins/$PLUGIN"
CFG_FILE="$CFG_DIR/varaverk.cfg"
WEB_DIR="/usr/local/emhttp/plugins/$PLUGIN"
LOG="$CFG_DIR/install.log"

mkdir -p "$CFG_DIR"

log() { echo "[$(date '+%H:%M:%S')] $*" | tee -a "$LOG"; }

log "=== Varaverk install/update ==="

# Unraid only
if [[ ! -f /etc/unraid-version ]]; then
    log "ERROR: not running on Unraid — aborting"
    exit 1
fi

# Read SCRIPTS_DIR from cfg if present
if [[ -f "$CFG_FILE" ]]; then
    _sd=$(grep -oP '(?<=SCRIPTS_DIR=")[^"]+' "$CFG_FILE" 2>/dev/null)
    [[ -n "$_sd" ]] && SCRIPTS_DIR="$_sd"
fi
SCRIPTS_DIR="${SCRIPTS_DIR:-/mnt/user/appdata/Varaverk}"
CONF_DIR="$SCRIPTS_DIR/Configurations"

# Array must be started — appdata must be available
if ! df --output=fstype /mnt/user 2>/dev/null | grep -q 'shfs'; then
    log "Array not started — cannot install to appdata"
    log "Start the array then reinstall, or wait for next boot"
    exit 0
fi

# Clone or update repo
if [[ ! -d "$SCRIPTS_DIR/.git" ]]; then
    log "Cloning $GITHUB ($BRANCH) → $SCRIPTS_DIR"
    if ! git clone --branch "$BRANCH" "$GITHUB" "$SCRIPTS_DIR" >> "$LOG" 2>&1; then
        log "ERROR: git clone failed — check internet access and GitHub URL"
        exit 1
    fi
    log "Clone complete"
else
    log "Updating repo in $SCRIPTS_DIR"
    git -C "$SCRIPTS_DIR" pull --ff-only origin "$BRANCH" >> "$LOG" 2>&1 \
        && log "Pull complete" \
        || log "WARNING: git pull failed — repo may have local modifications"
fi

# Sanity check — Plugin/unraid must exist after clone
if [[ ! -d "$SCRIPTS_DIR/Plugin/unraid" ]]; then
    log "ERROR: Plugin/unraid not found in cloned repo — unexpected repo structure"
    exit 1
fi

# Symlink web files
if [[ -L "$WEB_DIR" ]]; then
    rm "$WEB_DIR"
elif [[ -d "$WEB_DIR" ]]; then
    log "WARNING: $WEB_DIR is a real directory — not replacing (remove manually if needed)"
fi
if [[ ! -e "$WEB_DIR" ]]; then
    ln -s "$SCRIPTS_DIR/Plugin/unraid" "$WEB_DIR"
    log "Symlinked: $WEB_DIR → $SCRIPTS_DIR/Plugin/unraid"
fi

# Write varaverk.cfg if not present
if [[ ! -f "$CFG_FILE" ]]; then
    printf 'SCRIPTS_DIR="%s"\n' "$SCRIPTS_DIR" > "$CFG_FILE"
    log "Created varaverk.cfg (SCRIPTS_DIR=$SCRIPTS_DIR)"
fi

# Create Configurations dir if git clone didn't
mkdir -p "$CONF_DIR"

# Bootstrap master.conf from template on first install
if [[ ! -f "$CONF_DIR/master.conf" ]]; then
    if [[ -f "$SCRIPTS_DIR/Configurations/master.conf.template" ]]; then
        cp "$SCRIPTS_DIR/Configurations/master.conf.template" "$CONF_DIR/master.conf"
        log "Created master.conf from template — fill in HOST1 and HOST2 to continue"
    else
        log "WARNING: master.conf.template not found — master.conf not created"
    fi
fi

log "Install complete"
]]>
</INLINE>
</FILE>

<!--
  ── Boot (every boot) ───────────────────────────────────────────────────────────
  Fast idempotent check — recreates the web symlink if it disappeared.
  No git operations. Safe to run before the array is fully started.
  ────────────────────────────────────────────────────────────────────────────────
-->
<FILE Name="&cfgdir;/boot.sh" Run="/bin/bash">
<INLINE>
<![CDATA[
#!/bin/bash
PLUGIN="varaverk"
CFG_FILE="/boot/config/plugins/$PLUGIN/varaverk.cfg"
WEB_DIR="/usr/local/emhttp/plugins/$PLUGIN"

# Read SCRIPTS_DIR
if [[ -f "$CFG_FILE" ]]; then
    _sd=$(grep -oP '(?<=SCRIPTS_DIR=")[^"]+' "$CFG_FILE" 2>/dev/null)
fi
SCRIPTS_DIR="${_sd:-/mnt/user/appdata/Varaverk}"

# Only act if source exists (array started) and symlink is missing
if [[ -d "$SCRIPTS_DIR/Plugin/unraid" ]] && [[ ! -e "$WEB_DIR" ]]; then
    [[ -L "$WEB_DIR" ]] && rm "$WEB_DIR"
    ln -s "$SCRIPTS_DIR/Plugin/unraid" "$WEB_DIR"
fi
]]>
</INLINE>
</FILE>

<!--
  ── Remove ──────────────────────────────────────────────────────────────────────
  Removes the web symlink. Scripts and conf in appdata are left intact.
  ────────────────────────────────────────────────────────────────────────────────
-->
<FILE Name="&cfgdir;/remove.sh" Run="/bin/bash" Method="remove">
<INLINE>
<![CDATA[
#!/bin/bash
WEB_DIR="/usr/local/emhttp/plugins/varaverk"
[[ -L "$WEB_DIR" ]] && rm -f "$WEB_DIR" && echo "Varaverk: removed web symlink"
echo "Varaverk: scripts and conf in appdata are preserved"
]]>
</INLINE>
</FILE>

</PLUGIN>
