#!/bin/bash

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2020-present Shanti Gilbert (https://github.com/shantigilbert)
# Copyright (C) 2023 JELOS (https://github.com/JustEnoughLinuxOS)

case "${1}" in
  "retroarch")
    rm -f /storage/.config/retroarch/retroarch.cfg
    cp -rf /usr/config/retroarch/retroarch.cfg /storage/.config/retroarch/retroarch.cfg
  ;;
  "retroarch-full")
    rm -rf /storage/.config/retroarch
    cp -rf /usr/config/retroarch /storage/.config/
  ;;
  "mednafen")
    rm -f /storage/.config/mednafen/mednafen.cfg
  ;;
  "overlays")
    rm -rf $(cat /usr/lib/systemd/system/tmp-*.mount | grep -Eo 'upperdir=.*,' | sed -e 's~upperdir=~~g; s~,~~g')
    sync
    systemctl reboot
  ;;
  "audio")
    systemctl stop pipewire-pulse pipewire-pulse.socket pipewire pipewire.socket wireplumber
    rm -rf /storage/.local/state /storage/.config/pulse /storage/asound*
    systemctl reboot
  ;;
  "ALL")
    swapoff -a 2>/dev/null
    umount /storage/roms 2>/dev/null ||:
    umount /storage/games-external 2>/dev/null ||:
    cd /
    find /storage -mindepth 1 \( ! -regex '^/storage/.update.*' -a ! -regex '^/storage/roms.*' -a ! -regex '^/storage/games-.*' \) -delete 2>/dev/null
    mkdir /storage/.config/
    sync 2>/dev/null
    systemctl reboot
  ;;
esac
