#!/bin/sh
#
# SPDX-License-Identifier: MIT
#

# So we can check the version
if [ ! -z "$PM_FUNCS_VERSION" ]; then
    return
fi

export PM_FUNCS_VERSION=2

export PM_SCRIPTNAME="$(basename "${PM_SCRIPTNAME:-$0}")"
export PM_PORTNAME="${PM_PORTNAME:-${PM_SCRIPTNAME%.sh}}"
export PM_VERSION="$(cat "$controlfolder/version")"
export PM_RESOURCE_DIR="$controlfolder/resources"
export PM_CAN_MOUNT="${PM_CAN_MOUNT:-Y}"

# muOS stuff.
if [[ "$CFW_NAME" == "muOS" ]]; then
    export LD_LIBRARY_PATH="/opt/muos/extra/lib:$LD_LIBRARY_PATH"
fi


# Extract the latest NotoSans.tar.xz
if [ -f "$controlfolder/pylibs/resources/NotoSans.tar.xz" ]; then
    # TODO: make sure this works on all CFW.
    $ESUDO tar -C "$controlfolder/pylibs/resources" -xf "$controlfolder/pylibs/resources/NotoSans.tar.xz"

    if [ -f "$controlfolder/pylibs/resources/NotoSansJP-Regular.ttf" ]; then
        $ESUDO rm -f "$controlfolder/pylibs/resources/NotoSans.tar.xz"
    fi
fi

if [ -f "$PM_RESOURCE_DIR/do_init" ]; then
    $ESUDO cp -f "$controlfolder"/pylibs/resources/*.ttf "$PM_RESOURCE_DIR/"

    $ESUDO rm -f "$PM_RESOURCE_DIR/do_init"
fi


bind_directories() {
    # Usage: bind_directories ~/.config/StardewValley $GAMEDIR/savedata
    #
    # Reason: some platforms (batocera) use exfat for the home directory, so we can't use symbolic links
    # We then instead use bind mount, however retrodeck cannot bind mount because it does not run in root.
    #
    if [ "$PM_CAN_MOUNT" = "Y" ]; then
        [[ -L "$1" ]] && rm -f "$1" && echo "removed previous symlink $1"
        # Ensure the directory exists before attempting the bind mount
        if [ -d "$2" ]; then
            mkdir -p "$1"
            $ESUDO umount "$1"
            $ESUDO mount --bind "$2" "$1" && echo "successful bind mount from $2 to $1"
        else
            echo "no directory found at $2"
        fi
    else
        # RetroDECK cant use bind mount without root.
        rm -f "$1"
        ln -sfv "$2" "$1"
    fi
}

bind_files() {
    # Usage: bind_files ~/.config_file $GAMEDIR/conf/.config_file
    #
    # Reason: some platforms (batocera) use exfat for the home directory, so we can't use symbolic links
    # We then instead use bind mount, however retrodeck cannot bind mount because it does not run in root.
    #
    if [ "$PM_CAN_MOUNT" = "Y" ]; then
        [[ -L "$1" ]] && rm -f "$1" && echo "removed previous symlink $1"
        # Ensure the file exists before attempting the bind mount
        if [ -f "$2" ]; then
            touch "$1"
            $ESUDO umount "$1"
            $ESUDO mount --bind "$2" "$1" && echo "successful bind mount from $2 to $1"
        else
            echo "no file found at $2"
        fi
    else
        # RetroDECK cant use bind mount without root.
        rm -f "$1"
        ln -sfv "$2" "$1"
    fi
}

pm_begin_splash() {
    # Usage: pm_begin_splash
    #
    # Shows an splash screen, uses $PM_RESOURCE_DIR/splash.ini, override by setting `$PM_SPLASH_THEME`
    # Some platforms the splashscreen program will quit immediately, others it will stay in the background.
    # make sure you kill it with pm_end_splash, pm_show_error will kill the splashscreen so don't fret.
    #

    pm_end_splash

    if [ ! -e "/dev/dri/card0" ]; then
        PM_SPLASH_QUIT_MODE="-q"
    else
        PM_SPLASH_QUIT_MODE=""
    fi

    if [ -z "$PM_SPLASH_THEME" ]; then
        PM_SPLASH_THEME="$PM_RESOURCE_DIR/splash.ini"
    fi

    $ESUDO "$controlfolder/sdl2imgshow.${DEVICE_ARCH}" -z "$PM_SPLASH_THEME" $PM_SPLASH_QUIT_MODE &
}


pm_show_error() {
    # Usage: pm_show_error "Missing Game Files" "Game files are missing, please see wiki for details."
    #
    # Shows an error message, uses $PM_RESOURCE_DIR/error.ini, override by setting `$PM_ERROR_THEME`
    # this will wait for the user to press a button before quitting.
    #

    pm_end_splash

    if [ ! -z "$PM_ERROR_THEME" ]; then
        PM_ERROR_THEME="$PM_RESOURCE_DIR/error.ini"
    fi

    export PM_ERROR_TITLE="$1"
    export PM_ERROR_MESSAGE="$2" 

    $ESUDO "$controlfolder/sdl2imgshow.${DEVICE_ARCH}" -z "$PM_ERROR_THEME" -wY
}


pm_end_splash() {
    # Kills any splashscreen running.
    $ESUDO pkill -f "sdl2imgshow.${DEVICE_ARCH}"
}


pm_message() {
    # Usage: pm_message "Some message here."
    if [[ "$CFW_NAME" == "muOS" ]]; then
        /opt/muos/extra/muxstart "$1"
    elif [[ "$CFW_NAME" == "ROCKNIX" ]]; then
        foot -F /bin/bash -c "echo \"$1\"; sleep 5" & 
    else
        echo "$1" > $CUR_TTY
    fi
}


pm_gptokeyb_finish() {
    $ESUDO pkill -f gptokeyb
    $ESUDO pkill -f gptokeyb2
}


pm_finish() {
    pm_gptokeyb_finish

    $ESUDO systemctl restart oga_events 2> /dev/null &

    if [ ! -z "$(echo "$CUR_TTY" | grep -e '^/dev/tty')" ]; then
        printf "\033c" > "$CUR_TTY"
    fi
}
