#!/bin/bash

# Author: Infernio <infernio at icloud dot com>
# SSSM - Simple steam skin manager.
# This script can install, upgrade and remove steam skins for specific users.

# Constants
C_ORANGE='\033[0;33m'
C_RED='\033[1;31m'
C_GREEN='\033[1;32m'
C_BLUE='\033[1;34m'
C_YELLOW='\033[1;33m'
C_NC='\033[0m'
SSSM_VERSION="1.2.1"

# Parameters
action="$1"

# Functions
function usage()
{
    name=$(basename "${BASH_SOURCE[@]}")
    echo -e "Usage: ${name} ${C_BLUE}<action>${C_NC} ${C_YELLOW}[action-specific options]${C_NC}"
    echo "SSSM (Simple Steam Skin Manager) is a simple script for managing steam skins."
    echo "Skins must be installed to /usr/share/steam/skins in order to be used."
    echo ""
    echo "Actions:"
    echo -e "  - ${C_BLUE}list ${C_YELLOW}[user]${C_NC}"
    echo "      Lists all currently installed skins."
    echo "      Parameters:"
    echo -e "        - ${C_YELLOW}user${C_NC} (optional): If specified, list only the skins that user has installed."
    echo "                           Otherwise, list all globally installed skins."
    echo -e "  - ${C_BLUE}sync ${C_YELLOW}<skin> [user]${C_NC}"
    echo "      Installs or upgrades a skin for a user."
    echo "      Parameters:"
    echo -e "        - ${C_YELLOW}skin${C_NC}: The skin to install / upgrade."
    echo -e "        - ${C_YELLOW}user${C_NC} (optional): The user to install / upgrade the skin for."
    echo "                           If left empty, install / upgrade for all users."
    echo -e "  - ${C_BLUE}remove ${C_YELLOW}<skin> [user]${C_NC}"
    echo "      Uninstalls a skin for a user."
    echo "      Parameters:"
    echo -e "        - ${C_YELLOW}skin${C_NC}: The skin to uninstall."
    echo -e "        - ${C_YELLOW}user${C_NC} (optional): The user to uninstall the skin for."
    echo "                           If left empty, uninstall for all users."
    echo -e "  - ${C_BLUE}version${C_NC}"
    echo "      Prints the manager's version and exits."
    exit 1
}

function version()
{
    echo "Simple Steam Skin Manager v${SSSM_VERSION}"
    return 0
}

function list()
{
    # Parameters
    user="$1"

    # if 'user' is empty, list globally installed ones
    if [ -z "$user" ]
    then
        for skin in /usr/share/steam/skins/*
        do
            if [ -d "${skin}" ]
            then
                skin=${skin#"/usr/share/steam/skins/"}
                echo "${skin}"
            fi
        done
    else
        # otherwise, list the ones installed by the specified user
        for skin in "/home/${user}/.local/share/Steam/skins"/*
        do
            if [ -d "${skin}" ]
            then
                skin=${skin#"/home/${user}/.local/share/Steam/skins/"}
                echo "${skin}"
            fi
        done
    fi
    return 0
}

function sync()
{
    # Parameters
    skin="$1"
    user="$2"

    # 'skin' may not be empty
    if [ -z "$skin" ]
    then
        echo -e "sssm sync: ${C_RED}The first argument (skin) may not be empty.${C_NC}\n"
        usage
    fi

    # if 'user' is empty, install for all users
    if [ -z "$user" ]
    then
        sync_forall "$skin"
        return $?
    fi

    echo -e "${C_BLUE} => ${C_NC}Installing skin ${C_GREEN}${skin}${C_NC} for user ${C_GREEN}${user}${C_NC}."

    # if the skin doesn't exist, say as much
    if [ ! -d "/usr/share/steam/skins/${skin}" ]
    then
        echo -e "${C_BLUE} => ${C_RED}Installation failed:${C_NC} Skin ${C_GREEN}${skin}${C_NC} could not be found in /usr/share/steam/skins."
        exit 2
    fi

    # Check to make sure the user actually has Steam installed
    if [ ! -d "/home/${user}/.local/share/Steam" ]
    then
        echo -e "${C_BLUE} => ${C_ORANGE}Installation skipped:${C_NC} User ${C_GREEN}${user}${C_NC} does not seem to have Steam installed."
        return 1
    fi

    # Create the directory, nuke the skin if it already exists and copy the new skin over
    SKINDIR="/home/${user}/.local/share/Steam/skins"
    mkdir -p "${SKINDIR}"
    rm -rf "${SKINDIR:?}/${skin:?}"
    cp -r "/usr/share/steam/skins/${skin}" "${SKINDIR}"
    res1=$?
    chown -R "${user}:$(id -g "$user")" "${SKINDIR}/${skin}"
    res2=$?

    # Check if the installation succeeded
    if [ $res1 -eq 0 ] && [ $res2 -eq 0  ]
    then
        echo -e "${C_BLUE} => ${C_NC}Installation completed successfully."
    else
        echo -e "${C_BLUE} => ${C_RED}Installation failed:${C_NC} Error while copying or setting ownership."
        exit 2
    fi
    return 0
}

function sync_forall()
{
    # Parameters
    skin="$1"

    echo -e "${C_BLUE}=> ${C_NC}Installing skin ${C_GREEN}${skin}${C_NC} for all users."
    for user in /home/*
    do
        user=${user#"/home/"}

        # Skip lost+found and root (which you shouldn't use for steam)
        if [ "$user" != "lost+found" ] && [ "$user" != "root" ]
        then
            sync "$skin" "$user"

            # 0 means success, 1 means skipped, > 1 means failed
            res=$?
            if [ $res -gt 1 ]
            then
                exit $res
            fi
        fi
    done
    echo -e "${C_BLUE}=> ${C_NC}Installation completed successfully for all users."
    return 0
}

function remove()
{
    # Parameters
    skin="$1"
    user="$2"

    # 'skin' may not be empty
    if [ -z "$skin" ]
    then
        echo -e "sssm remove: ${C_RED}The first argument (skin) may not be empty.${C_NC}\n"
        usage
    fi

    # if 'user' is empty, uninstall for all users
    if [ -z "$user" ]
    then
        remove_forall "$skin"
        return $?
    fi

    echo -e "${C_BLUE} => ${C_NC}Uninstalling skin ${C_GREEN}${skin}${C_NC} for user ${C_GREEN}${user}${C_NC}."

    # Check to make sure the user actually has Steam installed
    if [ ! -d "/home/${user}/.local/share/Steam" ]
    then
        echo -e "${C_BLUE} => ${C_ORANGE}Uninstallation skipped:${C_NC} User ${C_GREEN}${user}${C_NC} does not seem to have Steam installed."
        return 1
    fi

    # Check if the skin is installed and, if so, remove it
    SKINDIR="/home/${user}/.local/share/Steam/skins"
    if [ -d "${SKINDIR}/${skin}" ]
    then
        rm -rf "${SKINDIR:?}/${skin:?}"

        # Check if the uninstallation succeeded
        res=$?
        if [ $res -eq 0 ]
        then
            echo -e "${C_BLUE} => ${C_NC}Uninstallation completed successfully."
        else
            echo -e "${C_BLUE} => ${C_RED}Uninstallation failed:${C_NC} Error while removing files."
            exit $res
        fi

    else
        echo -e "${C_BLUE} => ${C_ORANGE}Uninstallation skipped:${C_NC} User ${C_GREEN}${user}${C_NC} does not seem to have the skin ${C_GREEN}${skin}${C_NC} installed."
        return 1
    fi
    return 0
}

function remove_forall()
{
    # Parameters
    skin="$1"

    echo -e "${C_BLUE}=> ${C_NC}Uninstalling skin ${C_GREEN}${skin}${C_NC} for all users."
    for user in /home/*
    do
        user=${user#"/home/"}

        # Skip lost+found and root (which you shouldn't use for steam)
        if [ "$user" != "lost+found" ] && [ "$user" != "root" ]
        then
            remove "$skin" "$user"

            # 0 means success, 1 means skipped, > 1 means failed
            res=$?
            if [ $res -gt 1 ]
            then
                exit $res
            fi
        fi
    done
    echo -e "${C_BLUE}=> ${C_NC}Uninstallation completed successfully for all users."
    return 0
}

# Main Script
case "$action" in
    # just in case ;)
    "sync" | "install" | "isntall")
        sync "$2" "$3"
        ;;
    "list")
        list "$2"
        ;;
    "remove" | "uninstall")
        remove "$2" "$3"
        ;;
    "version")
        version
        ;;
    *)
        echo -e "sssm: ${C_RED}Unknown action '${action}'.${C_NC}\n"
        usage
        ;;
esac
exit $?
