#!/bin/bash

# Check if server is active
status=$(systemctl show valheim -p ActiveState --value)
# shutdown server if it is active
if [[ $status == active ]]; then
    echo "Server is active: executing shutdown..."
    systemctl stop valheim
    sleep 1
fi

# Install Valheim
installdir="/home/vhadmin"
steamcmd +login anonymous +force_install_dir "$installdir" +app_update 896660 validate +exit

# Install Mods
modsite="https://valheim.thunderstore.io"
moddir="$installdir/BepInEx/plugins"

rm_ife() {
    if [[ -e "$1" ]]; then
        rm -r "$1"
    fi
}

get_modver() {
    mod="$1"
    modauthor=$(echo "$mod" | cut -d '-' -f 1)
    modname=$(echo "$mod" | cut -d '-' -f 2)
    modpath="$modauthor/$modname"

    web_version=$(wget -qO - "$modsite/package/$modpath/" | grep -oP "/package/download/$modpath/\K([\d.]+)" | head -1)
    loc_version=$(grep -osP '"version_number": "\K([\d.]+)' "$moddir/$mod/manifest.json")

    if [[ $web_version != $loc_version ]]; then
        echo $web_version
    fi
}

dl_mod() {
    modauthor=$(echo "$1" | cut -d '-' -f 1)
    modname=$(echo "$1" | cut -d '-' -f 2)
    modpath="$modauthor/$modname"

    wget -qO "$mod.zip" "$modsite/package/download/$modpath/$2"
}

tmpdir=$(mktemp -d)
cd "$tmpdir"

# Install BepinEx
mod='denikson-BepInExPack_Valheim'
modver=$(get_modver "$mod")
if [[ -n $modver ]]; then
    echo "Installing mod: $mod"
    dl_mod "$mod" "$modver"
    unzip -q "$mod.zip"
    mkdir "BepInExPack_Valheim/BepInEx/plugins/$mod"
    mv BepInExPack_Valheim/BepInEx/plugins/*.dll "BepInExPack_Valheim/BepInEx/plugins/$mod/"
    mv manifest.json README.md "BepInExPack_Valheim/BepInEx/plugins/$mod/"
    rm -r BepInExPack_Valheim/BepInEx/config
    ln -s /vol/data/config/BepInEx BepInExPack_Valheim/BepInEx/config
    mv BepInExPack_Valheim/* "$installdir/"
    rm -r *
else
    echo "Mod already latest: $mod"
fi

# Install Additional Mods
if [[ -e /vol/data/config/server ]]; then
    . /vol/data/config/server
else
    vh_mods=()
fi

for mod in "${vh_mods[@]}"; do
    modver=$(get_modver "$mod")
    if [[ -n $modver ]]; then
        echo "Installing mod: $mod"
        dl_mod "$mod" "$modver"
        unzip -q "$mod.zip"
        # annoyingly, mod zips don't all have the same structure
        # find all .dll files, and put them in the plugin dir
        mkdir -p "$moddir/$mod/"
        for dll in $(find -name '*.dll'); do
            mv "$dll" "$moddir/$mod/"
        done
        mv manifest.json README.md "$moddir/$mod/"
        rm -r *
    else
        echo "Mod already latest: $mod"
    fi
done

vh_mods+=('denikson-BepInExPack_Valheim')
cd "$moddir"

for mod in *; do
    if $(echo "${vh_mods[@]}" | grep -qv "\b$mod\b"); then
        echo "Removing mod: $mod"
        rm -r "$mod"
    fi
done

# Clean Up
rm -r "$tmpdir"
chown -R vhadmin:vhadmin $installdir
# start server back up if it was active
if [[ $status == active ]]; then
    echo "Server was active: starting server again..."
    systemctl start valheim
    sleep 5
fi