#!/bin/sh

# thedarkmod_installer.sh
# 2022-01-08
# by Gernot Walzl

# The Dark Mod is a first person stealth game.
# Homepage: https://www.thedarkmod.com/

# Required packages:
# x11-common, libxxf86vm1, libopenal1, libxext6

# The user must be able to write files in the game directory.
# Therefore, a system-wide installation is not recommended.

# Alternative to installing binaries, the executable can be built as follows:
# wget https://www.thedarkmod.com/sources/thedarkmod.2.09b.src.7z
# mkdir darkmod && cd darkmod
# 7z x ../thedarkmod.2.09b.src.7z
# mkdir build && cd build
# cmake -DCMAKE_BUILD_TYPE="Release" ..
# make

OUTPUT=${OUTPUT:-'thedarkmod'}

# http://mirrors.thedarkmod.com/tdm_mirrors.txt
MIRROR="https://darkmod.taaaki.za.net/release"

BIN="tdm_shared_stuff.zip"
PKGS="
fms/newjob/newjob.pk4
fms/stlucia/stlucia.pk4
fms/training_mission/training_mission.pk4
tdm_ai_animals01.pk4
tdm_ai_base01.pk4
tdm_ai_humanoid_beasts01.pk4
tdm_ai_humanoid_beasts02.pk4
tdm_ai_humanoid_builders01.pk4
tdm_ai_humanoid_females01.pk4
tdm_ai_humanoid_guards01.pk4
tdm_ai_humanoid_heads01.pk4
tdm_ai_humanoid_mages01.pk4
tdm_ai_humanoid_nobles01.pk4
tdm_ai_humanoid_pagans01.pk4
tdm_ai_humanoid_townsfolk01.pk4
tdm_ai_humanoid_undead01.pk4
tdm_ai_monsters_spiders01.pk4
tdm_ai_steambots01.pk4
tdm_base01.pk4
tdm_defs01.pk4
tdm_env01.pk4
tdm_fonts01.pk4
tdm_gui01.pk4
tdm_gui_credits01.pk4
tdm_models01.pk4
tdm_models02.pk4
tdm_models_decls01.pk4
tdm_player01.pk4
tdm_prefabs01.pk4
tdm_sound_ambient01.pk4
tdm_sound_ambient02.pk4
tdm_sound_ambient03.pk4
tdm_sound_ambient_decls01.pk4
tdm_sound_sfx01.pk4
tdm_sound_sfx02.pk4
tdm_sound_vocals01.pk4
tdm_sound_vocals02.pk4
tdm_sound_vocals03.pk4
tdm_sound_vocals04.pk4
tdm_sound_vocals05.pk4
tdm_sound_vocals06.pk4
tdm_sound_vocals07.pk4
tdm_sound_vocals_decls01.pk4
tdm_standalone.pk4
tdm_textures_base01.pk4
tdm_textures_carpet01.pk4
tdm_textures_decals01.pk4
tdm_textures_door01.pk4
tdm_textures_fabric01.pk4
tdm_textures_glass01.pk4
tdm_textures_metal01.pk4
tdm_textures_nature01.pk4
tdm_textures_other01.pk4
tdm_textures_paint_paper01.pk4
tdm_textures_plaster01.pk4
tdm_textures_roof01.pk4
tdm_textures_sfx01.pk4
tdm_textures_stone_brick01.pk4
tdm_textures_stone_cobblestones01.pk4
tdm_textures_stone_flat01.pk4
tdm_textures_stone_natural01.pk4
tdm_textures_stone_sculpted01.pk4
tdm_textures_window01.pk4
tdm_textures_wood01.pk4
"

# exit on error
set -e

# create output directory
mkdir -p "$OUTPUT"
cd "$OUTPUT" || exit 1

# download binary if needed
if [ ! -f "$BIN" ]; then
  wget -O "$BIN" "$MIRROR/$BIN"
fi

# download packages if needed
for PKG in $PKGS; do
  if [ ! -f "$PKG" ]; then
    mkdir -p "$(dirname "$PKG")"
    wget -O "$PKG" "$MIRROR/$PKG"
  fi
done

unzip "$BIN"
chmod +x "thedarkmod.x64"