#!/bin/sh
# Copyright (C) 2013-2018 Michael Gilbert <mgilbert@debian.org>
# Copyright (C) 2018-2023 Simon McVittie
# Copyright (C) 2020-2024 Collabora Ltd.
# Copyright (C) 2025 Manjaro GmbH & Co. KG
# SPDX-License-Identifier: MIT

set -e

if [ -e /arkdep ]; then
    # Get user details
    owner=$(awk -F : 'NR == 2 { print $3":"$4 }' /arkdep/overlay/etc/passwd)
    homedir=$(awk -F : 'NR == 2 { print $1 }' /arkdep/overlay/etc/passwd)
fi

# Used by diagnostic tools to identify the launcher
export STEAMSCRIPT="$0"
# Used by diagnostic tools to identify the launcher version
# Edited by debian/rules to fill in the real version number
export STEAMSCRIPT_VERSION='uninstalled/Manjaro'

# According to Valve, ~/.steam is intended to be a control directory containing
# symbolic links pointing to the currently-running or most-recently-run Steam
# installation. This is part of Steam's API, and is relied on by external
# components.
#
# The shell variable name STEAMCONFIG matches what's used in Valve's
# /usr/bin/steam (available at $STEAMDIR/bin_steam.sh in a Steam
# installation).
STEAMCONFIG="$HOME/.steam"

# Fast-path: if Steam is already running, try to forward command-line to it
if [ -p "$STEAMCONFIG/steam.pipe" ] \
    && [ -x "$STEAMCONFIG/root/ubuntu12_32/steam-runtime/amd64/usr/bin/steam-runtime-steam-remote" ] \
    && "$STEAMCONFIG/root/ubuntu12_32/steam-runtime/amd64/usr/bin/steam-runtime-steam-remote" "$@" 2>/dev/null
then
    exit 0
fi

: "${XDG_DATA_HOME:="$HOME/.local/share"}"

# Fix dangling symlinks
if [ -L "$STEAMCONFIG/steam" ] && ! [ -e "$STEAMCONFIG/steam" ]; then
    rm -f "$STEAMCONFIG/steam"
fi
if [ -L "$STEAMCONFIG/root" ] && ! [ -e "$STEAMCONFIG/root" ]; then
    rm -f "$STEAMCONFIG/root"
fi

# STEAMDIR points to the actual installation root: the equivalent of
# C:\Program Files\Steam in the Windows Steam client. To avoid filename
# collisions this should be distinct from ~/.steam.
#
# The shell variable name STEAMDIR matches what's used in Valve's
# /usr/bin/steam (bin_steam.sh).
#
# Strictly speaking, there can be two separate Steam directories:
# ~/.steam/steam is a symlink to the Steam data directory (containing
# e.g. games), while ~/.steam/root is a symlink to the Steam installation
# (containing the Steam executable and the Steam Runtime). This is used
# when testing new Steam client binaries, and older versions of this Debian
# package set up a similar situation by mistake.
if [ -L "$STEAMCONFIG/steam" ]; then
    STEAMDIR="$(readlink -e -q "$STEAMCONFIG/steam")"
elif [ -L "$STEAMCONFIG/root" ]; then
    STEAMDIR="$(readlink -e -q "$STEAMCONFIG/root")"
elif [ -d "$STEAMCONFIG/steam" ] && ! [ -L "$STEAMCONFIG/steam" ]; then
    # The historical Manjaro behaviour has been to use ~/.steam as the
    # installation directory in addition to using it as the control
    # directory.This causes some file collisions, so we've moved away
    # from that, but we can't easily disentangle this in existing
    # installations.
    STEAMDIR="$HOME/.steam"
else
    # This is a new installation, so use a distinct directory to avoid
    # file collisions. Valve would use $XDG_DATA_HOME/Steam here.
    # Manjaro uses a subdirectory of ~/.steam, to avoid having a mixture
    # of XDG basedirs and traditional dotfiles in the same application.
    STEAMDIR="$HOME/.steam/manjaro-installation"
fi

# use C locale (bug #764311)
test -n "$LANG" || export LANG=C

# do an initial update when expected pieces are missing
test ! -d "$STEAMCONFIG" && rm -rf "$STEAMCONFIG" && mkdir -p "$STEAMCONFIG" || true
test ! -d "$STEAMDIR" && rm -rf "$STEAMDIR" && mkdir -p "$STEAMDIR" || true

# Recent versions of the proprietary steam executable exit with an
# assertion failure if these links aren't already set up.
if ! [ -d "$STEAMCONFIG/steam" ]; then
    ln -fns "$STEAMDIR" "$STEAMCONFIG/steam"
fi
if ! [ -d "$STEAMCONFIG/root" ]; then
    ln -fns "$STEAMDIR" "$STEAMCONFIG/root"
fi

version="1.0.0.84"
# This only needs to differ from ${version} or be incremented when the
# installed files from steam-launcher change
mjr_version="${version}"
sha256="31fa762d58793f1aa8ea9df0122c3469c253fc8acb6daa7140fb7433fe077b44"
url="https://repo.steampowered.com/steam/archive/stable/steam_${version}.tar.gz"
installed="$(cat "$STEAMDIR/mjr-installer/version" 2>/dev/null || true)"
new_installation=

mkdir -p "$STEAMDIR"

for needed in \
    steam.sh \
    ubuntu12_32/steam \
    ubuntu12_32/steam-runtime/run.sh \
    ubuntu12_32/steam-runtime/setup.sh \
; do
    if ! [ -x "$STEAMDIR/$needed" ]; then
        new_installation=yes
    fi
done

if [ "$installed" != "$mjr_version" ] || [ -n "$new_installation" ]; then
    mkdir -p "$STEAMDIR/mjr-installer"
    if [ $(wget -q --spider https://manjaro.org ; echo $?) == "0" ]; then
        curl -o "$STEAMDIR/mjr-installer/steam_${version}.tar.gz.$$" "$url"
    fi
    got="$(sha256sum -b "$STEAMDIR/mjr-installer/steam_${version}.tar.gz.$$")"

    if [ "${got% *}" != "$sha256" ]; then
        echo "SHA256 verification failed" >&2
        echo "Expected: $sha256" >&2
        echo "Got:      $got" >&2
        rm -f "$STEAMDIR/mjr-installer/steam_${version}.tar.gz.$$"
        exit 1
    fi

    tar \
        -C "$STEAMDIR/mjr-installer" \
        -zxf "$STEAMDIR/mjr-installer/steam_${version}.tar.gz.$$" \
        steam-launcher/bootstraplinux_ubuntu12_32.tar.xz \
        steam-launcher/icons \
        steam-launcher/steam.desktop \
        ${NULL+}
    mv \
        "$STEAMDIR/mjr-installer/steam-launcher/bootstraplinux_ubuntu12_32.tar.xz" \
        "$STEAMDIR/bootstrap.tar.xz"
    rm -f "$STEAMDIR/mjr-installer/steam_${version}.tar.gz.$$"

    if [ -n "$new_installation" ]; then
        tar -C "$STEAMDIR" \
            -xf "$STEAMDIR/bootstrap.tar.xz" \
            ${NULL+}
    fi

    # Use the proprietary icons to replace the ones from the .deb
    for i in 16 24 32 48 256; do
        target="$STEAMDIR/mjr-installer/steam-launcher/icons/$i/steam.png"
        dest="${XDG_DATA_HOME}/icons/hicolor/${i}x${i}/apps/steam.png"
        mkdir -p "${dest%/*}"
        if [ -L "$dest" ] || ! [ -e "$dest" ]; then
            ln -fns "$target" "$dest"
        fi

        target="$STEAMDIR/mjr-installer/steam-launcher/icons/$i/steam_tray_mono.png"
        dest="${XDG_DATA_HOME}/icons/hicolor/${i}x${i}/apps/steam_tray_mono.png"
        if [ -e "$target" ]; then
            if [ -L "$dest" ] || ! [ -e "$dest" ]; then
                ln -fns "$target" "$dest"
            fi
        fi
    done

    # Use the proprietary .desktop file to replace the one from the .deb
    target="$STEAMDIR/mjr-installer/steam.desktop"
    dest="${XDG_DATA_HOME}/applications/steam.desktop"
    mkdir -p "${dest%/*}"
    # Adjust proprietary .desktop file for our installation location
    sed \
        -e '1i#!/usr/bin/env xdg-open' \
        -e '/^Actions=/ a Keywords=Games' \
        < "$STEAMDIR/mjr-installer/steam-launcher/steam.desktop" \
        > "$target"
    # It needs to be executable for KDE (#1029806), because KIO and GLib
    # have different interpretations of what it means for a .desktop file
    # to be in a non-standard location if it's a symlink: GLib uses the path
    # of the symlink for its check, but KIO uses the realpath().
    chmod +x "$target"
    if [ -L "$dest" ] || ! [ -e "$dest" ]; then
        ln -fns "$target" "$dest"
    fi

    # Force update
    mkdir -p "${XDG_DATA_HOME}/icons/hicolor/"
    touch "${XDG_DATA_HOME}/icons/hicolor/"
    touch "${XDG_DATA_HOME}/applications/"

    echo "$mjr_version" > "$STEAMDIR/mjr-installer/version.$$"
    mv "$STEAMDIR/mjr-installer/version.$$" "$STEAMDIR/mjr-installer/version"
fi

# Remove old log file to avoid confusion. To debug game/Steam issues,
# please run this script from an interactive terminal, run it with its
# stdout/stderr redirected, or wrap it with script(1).
rm -f "$STEAMDIR/error.log"

# launch the Valve run script
exec "$STEAMDIR/steam.sh" -nominidumps -nobreakpad "$@" &

if [ -e /arkdep ]; then
    process_id=$!
    # Wait for steam process to end ...
    wait $process_id
    if [ -e "$STEAMDIR/ubuntu12_32/steam-runtime/run.sh" ] && ! [ -e /arkdep/shared/home/$homedir/Desktop/com.steampowered.Logout.nonplus.desktop ]; then
        rm /arkdep/shared/home/$homedir/Desktop/steam.desktop
        if [ -e /etc/skel/Desktop/com.steampowered.Logout.nonplus.desktop ]; then
            cp /etc/skel/Desktop/com.steampowered.Logout.nonplus.desktop /arkdep/shared/home/$homedir/Desktop/com.steampowered.Logout.nonplus.desktop
            chmod +x /arkdep/shared/home/$homedir/Desktop/com.steampowered.Logout.nonplus.desktop
            chown $owner /arkdep/shared/home/$homedir/Desktop/com.steampowered.Logout.nonplus.desktop
        fi
        if [ -e /etc/skel/Desktop/com.steampowered.Logout.desktop ]; then
            cp /etc/skel/Desktop/com.steampowered.Logout.desktop /arkdep/shared/home/$homedir/Desktop/com.steampowered.Logout.desktop
            chmod +x /arkdep/shared/home/$homedir/Desktop/com.steampowered.Logout.desktop
            chown $owner /arkdep/shared/home/$homedir/Desktop/com.steampowered.Logout.desktop
        fi
    fi
fi

# vi:set sw=4 sts=4 et:
