#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2024-present ArchR (https://github.com/archr-linux/Arch-R)

DEVICE_NAME="Retroid Pocket Gamepad"
CONFIG_DIR="/storage/.config/yabasanshiro"
CONFIG_FILE="input.cfg"

if [ ! -d "${CONFIG_DIR}" ]
then
  mkdir -p "${CONFIG_DIR}"
fi

if [ ! -e "${CONFIG_DIR}/${CONFIG_FILE}" ]; then
    GAMEPADCONFIG=$(xmlstarlet sel -t -c "//inputList/inputConfig[@deviceName='${DEVICE_NAME}']" -n /storage/.emulationstation/es_input.cfg)

    if [ ! -z "${GAMEPADCONFIG}" ]; then
        cat <<EOF >${CONFIG_DIR}/${CONFIG_FILE}
<?xml version="1.0"?>
<inputList>
${GAMEPADCONFIG}
</inputList>
EOF
    fi
fi
