diff options
author | Louie S <louie@example.com> | 2024-01-01 12:16:45 -0500 |
---|---|---|
committer | Louie S <louie@example.com> | 2024-01-01 12:16:45 -0500 |
commit | 211cfdc280b99f00765071945dbc09e1d7a91b0f (patch) | |
tree | 25a7b44d2187bbcbe3c646588fb008a8a19f294a /rom-desktop-entry-generator.sh | |
parent | 2b506fdcf79212f6ea84ae78070bc6b11a401a43 (diff) |
Add support for various flags
Diffstat (limited to 'rom-desktop-entry-generator.sh')
-rwxr-xr-x | rom-desktop-entry-generator.sh | 223 |
1 files changed, 178 insertions, 45 deletions
diff --git a/rom-desktop-entry-generator.sh b/rom-desktop-entry-generator.sh index d4eea49..6f53064 100755 --- a/rom-desktop-entry-generator.sh +++ b/rom-desktop-entry-generator.sh @@ -5,14 +5,16 @@ BASEDIR="$(dirname "$0")" SCRIPTNAME="$(basename "$0" .sh)" DESKTOP_ENTRY_OUTPUT_DIR="$BASEDIR/output" DESKTOP_ENTRY_INSTALL_DIR="$HOME/.local/share/applications" +ICON_SOURCE_DIR="$BASEDIR/icon" ICON_INSTALL_DIR="$HOME/.local/share/icons/hicolor" # Build an individual .desktop entry given a name, rompath, and system +# Usage: build_desktop_file NAME ROMPATH SYSTEM build_desktop_file() { NAME="$1" ROMPATH="$2" SYSTEM="$3" - OUTPUT="$DESKTOP_ENTRY_OUTPUT_DIR/${SCRIPTNAME}_${SYSTEM}_${NAME}.desktop" + OUTPUT="$DESKTOP_ENTRY_OUTPUT_DIR/$SYSTEM/${SCRIPTNAME}_${SYSTEM}_${NAME}.desktop" echo "Building $NAME" @@ -43,7 +45,7 @@ build_desktop_file() { # shellcheck source=/dev/null . "$BASEDIR/config/systems/$SYSTEM" - mkdir -p "$DESKTOP_ENTRY_OUTPUT_DIR" + mkdir -p "$DESKTOP_ENTRY_OUTPUT_DIR/$SYSTEM" echo "[Desktop Entry]" > "$OUTPUT" echo "Type=Application" >> "$OUTPUT" echo "Name=$NAME" >> "$OUTPUT" @@ -51,31 +53,67 @@ build_desktop_file() { echo "Exec=$launcher $flags $ROMPATH" >> "$OUTPUT" echo "Categories=Game" >> "$OUTPUT" - echo "Installing $NAME" - install_icon "$NAME" "$SYSTEM" - install_desktop_file "$OUTPUT" + #echo "Installing $NAME" + #install_icon "$NAME" "$SYSTEM" + #install_desktop_file "$OUTPUT" } -# empty the output directory -clean() { - rm -rf "$BASEDIR/output" +# Wrapper for a call to build +# Usage: build_wrapper [TARGET_SYSTEMS]... +build_wrapper() { + # find all romlist files in config if --system is not specified + if [ -z "$1" ]; then + find "$BASEDIR/config/" -name 'romlist_*' | while read -r file; do + parse_config "$file" + done + else + while [ -n "$1" ]; do + if [ -r "$BASEDIR/config/romlist_$1" ]; then + parse_config "$BASEDIR/config/romlist_$1" + fi + shift + done + fi } -install_desktop_file() { - OUTPUT="$1" +# empty the output directory +# Usage: clean_wrapper [TARGET_SYSTEMS] +clean_wrapper() { + while [ -n "$1" ]; do + rm -rf "$DESKTOP_ENTRY_OUTPUT_DIR/$1" + shift + done + + rmdir --ignore-fail-on-non-empty "$DESKTOP_ENTRY_OUTPUT_DIR" +} - # TODO look into using xdg-desktop-menu for install and uninstall - install "$OUTPUT" "$DESKTOP_ENTRY_INSTALL_DIR" +# help message +# Usage: help +help() { + echo "Usage: $0 [OPTION]... [TARGET]..." + echo "Generate desktop entries for video game roms" + echo + echo "Targets:" + echo " build build desktop entries into $DESKTOP_ENTRY_OUTPUT_DIR" + echo " clean remove $DESKTOP_ENTRY_OUTPUT_DIR and its contents (also calls uninstall target)" + echo " install install desktop entries into $DESKTOP_ENTRY_INSTALL_DIR" + echo " uninstall uninstall desktop entries from $DESKTOP_ENTRY_INSTALL_DIR" + echo + echo "Options" + echo " -h, --help print this help message and exit" + echo " --icon-dir [DIR] Specify an icon source directory (default is $ICON_SOURCE_DIR)" + echo " -s, --system [SYSTEM] Specify a system, or comma-separated list of systems. Default is all systems" } +# Usage: install_icon NAME SYSTEM install_icon() { NAME="$1" SYSTEM="$2" - ICON_SOURCE="$BASEDIR/icon/$SYSTEM/$NAME" + ICON_SOURCE="$ICON_SOURCE_DIR/$SYSTEM/$NAME" ICON_EXTENSION="" # Try a number of file extensions for the icon source - for extension in ".png"; do + for extension in ".png" ".svg" ".svgz" ".xpm"; do if [ -r "${ICON_SOURCE}${extension}" ]; then ICON_EXTENSION="$extension" break @@ -95,6 +133,27 @@ install_icon() { done } +# Wrapper for a call to install +# Usage: install_wrapper [TARGET_SYSTEMS]... +install_wrapper() { + while [ -n "$1" ]; do + find "$DESKTOP_ENTRY_OUTPUT_DIR/$1" -type f | while read -r file; do + echo "Installing $file" + + # Install desktop entry + # TODO look into using xdg-desktop-menu for install and uninstall + install "$file" "$DESKTOP_ENTRY_INSTALL_DIR" + + # Install icon + NAME="$(grep "^Name=" "$file" | cut -d "=" -f 2)" + SYSTEM="$(basename "$(dirname "$file")")" + install_icon "$NAME" "$SYSTEM" + done + + shift + done +} + parse_config() { CONFIG_PATH="$1" @@ -109,42 +168,116 @@ parse_config() { eval "build_desktop_file $line" done < "$CONFIG_PATH" - - # Workaround to refresh icons - touch "$ICON_INSTALL_DIR" } -uninstall() { - #NAME="$1" - #ROMPATH="$2" - #SYSTEM="$3" +# Usage: uninstall_wrapper [TARGET_SYSTEMS]... +uninstall_wrapper() { + while [ -n "$1" ]; do + find "$DESKTOP_ENTRY_OUTPUT_DIR/$1" -type f | while read -r file; do + echo "Uninstalling $(basename "$file")" - find "$BASEDIR/output/" -type f | while read -r file; do - echo "Removing $(basename "$file")" + # Remove icons + ICON="$(grep "^Icon=" "$file" | cut -d "=" -f 2)" + for size in "16" "24" "32" "48" "64" "96" "128" "256"; do + rm -f "$ICON_INSTALL_DIR/${size}x${size}/apps/$ICON"* + done - # Remove icons - ICON="$(grep "^Icon=" "$file" | cut -d "=" -f 2)" - for size in "16" "24" "32" "48" "64" "96" "128" "256"; do - rm -f "$ICON_INSTALL_DIR/${size}x${size}/apps/$ICON"* + # Remove the desktop entry + rm -f "$DESKTOP_ENTRY_INSTALL_DIR/$(basename "$file")" done - - # Remove the desktop entry - rm -f "$DESKTOP_ENTRY_INSTALL_DIR/$(basename "$file")" + + shift done } -# TODO allow for building without also installing -MODE="build" -case "$1" in - clean) - MODE="clean" - eval $MODE - ;; - uninstall) - MODE="uninstall" - eval $MODE - ;; - *) - parse_config "$BASEDIR/config/romlist" - ;; -esac +# Read arguments +GETOPT=$(getopt -o 'hs:' --long 'help,icon-dir:,system:' -n "$(basename "$0")" -- "$@") + +# Terminate if getopt goes wrong +if [ $? -ne 0 ]; then + echo "Terminating..." >&2 + exit 1 +fi + +eval set -- "$GETOPT" +unset GETOPT + +while true; do + echo "$1" + case "$1" in + '-h'|'--help') + help + exit + ;; + '--icon-dir') + shift + ICON_SOURCE_DIR="$1" + shift + continue + ;; + '-s'|'--system') + shift + TARGET_SYSTEMS="$1" + shift + continue + ;; + '--') + shift + break + ;; + *) + echo "Internal error!" >&2 + exit 1 + ;; + esac +done + +# By default, build, but do not install +if [ -z "$1" ]; then + build_wrapper +fi + +while [ -n "$1" ]; do + case "$1" in + build) + if [ -z "$TARGET_SYSTEMS" ]; then + build_wrapper + else + build_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ') + fi + shift + continue + ;; + clean) + if [ -z "$TARGET_SYSTEMS" ]; then + uninstall_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR") + clean_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR") + else + uninstall_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ') + clean_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ') + fi + shift + continue + ;; + install) + if [ -z "$TARGET_SYSTEMS" ]; then + install_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR") + else + install_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ') + fi + # Workaround to refresh icons + touch "$ICON_INSTALL_DIR" + shift + continue + ;; + uninstall) + if [ -z "$TARGET_SYSTEMS" ]; then + uninstall_wrapper $(ls -1 "$DESKTOP_ENTRY_OUTPUT_DIR") + else + uninstall_wrapper $(echo "$TARGET_SYSTEMS" | tr ',' ' ') + fi + shift + continue + ;; + esac +done |