#!/usr/bin/env bash
set -e

STEAMCMD_DIR="/opt/zlh/steamcmd"

echo "[steamcmd] Installing SteamCMD..."

apt-get update -y
apt-get install -y --no-install-recommends \
    ca-certificates \
    lib32gcc-s1 \
    lib32stdc++6 \
    curl \
    wget \
    tar \
    python3-minimal

mkdir -p "$STEAMCMD_DIR"
cd "$STEAMCMD_DIR"

if [ ! -f "$STEAMCMD_DIR/steamcmd.sh" ]; then
    echo "[steamcmd] Downloading SteamCMD..."
    wget -q https://steamcdn-a.akamaihd.net/client/installer/steamcmd_linux.tar.gz \
        -O steamcmd_linux.tar.gz
    echo "[steamcmd] Extracting..."
    tar -xzf steamcmd_linux.tar.gz
    rm -f steamcmd_linux.tar.gz
fi

if [ ! -e /usr/local/bin/steamcmd ]; then
    ln -s "$STEAMCMD_DIR/steamcmd.sh" /usr/local/bin/steamcmd
fi

echo "[steamcmd] Running initial update..."
/usr/local/bin/steamcmd +quit || true

echo "[steamcmd] SteamCMD install complete."
