Guest User

~/.local/bin/generate-flatpak-launchers.sh

a guest
Aug 19th, 2024
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 4.73 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. # Directory for launcher scripts
  4. script_dir="$HOME/.local/bin"
  5.  
  6. # Default operation mode
  7. operation="generate"
  8. dry_run=false
  9.  
  10. # Display usage information
  11. show_help() {
  12.     echo "Usage: $0 [option]"
  13.     echo "Options:"
  14.     echo "  -h  Display this help message"
  15.     echo "  -r  Remove all generated launcher scripts"
  16.     echo "  -n  Dry run (show actions without executing)"
  17. }
  18.  
  19. # Remove generated launcher scripts
  20. remove_generated_scripts() {
  21.     echo "Initiating removal of generated launcher scripts..."
  22.     for script in "$script_dir"/*; do
  23.         # Check for the GENERATED-FLATPAK-LAUNCHER tag
  24.         if [[ -f "$script" && $(grep -q "#GENERATED-FLATPAK-LAUNCHER" "$script"; echo $?) -eq 0 ]]; then
  25.             # Ensure the script is not protected by the FLATPAK-LAUNCHER-CREATOR tag
  26.             if ! grep -q "#FLATPAK-LAUNCHER-CREATOR" "$script"; then
  27.                 if [[ "$dry_run" == true ]]; then
  28.                     echo "Removing: $script (dry run)"
  29.                 else
  30.                     echo "Removing: $script"
  31.                     rm "$script"
  32.                 fi
  33.             else
  34.                 echo "Skipping: $script (protected)"
  35.             fi
  36.         fi
  37.     done
  38. }
  39.  
  40. # Parse command-line options
  41. while getopts ":hrn" opt; do
  42.     case ${opt} in
  43.         h)
  44.             show_help
  45.             exit 0
  46.             ;;
  47.         r)
  48.             operation="remove"
  49.             ;;
  50.         n)
  51.             dry_run=true
  52.             ;;
  53.         \?)
  54.             echo "Invalid option: $OPTARG" 1>&2
  55.             show_help
  56.             exit 1
  57.             ;;
  58.     esac
  59. done
  60.  
  61. # Ensure the script directory exists
  62. mkdir -p "$script_dir"
  63.  
  64. # Generate launcher script name from Flatpak application ID
  65. generate_script_name() {
  66.     local app_id=$1
  67.     local name_parts IFS='.'
  68.     read -ra name_parts <<< "$app_id"
  69.     local name="${name_parts[-1]}" # Default to the last part
  70.  
  71.     # If the last part is too generic, use the prior segment that seems descriptive
  72.     #if [[ "$name" == "desktop" || "$name" == "app" || "$name" == "net" ||  "$name" == "Browser" || "$name" == "browser" ]]; then
  73.     #    name="${name_parts[-2]}"
  74.     #fi
  75.     if [[ "${name,,}" =~ ^(desktop|app|net|browser)$ ]]; then
  76.         name="${name_parts[-2]}"
  77.     fi
  78.  
  79.  
  80.     # For GitHub projects, prioritize the repository name directly after 'github'
  81.     if [[ "$app_id" =~ github\.([^.]*)\.(.*) ]]; then
  82.         name="${BASH_REMATCH[2]}"
  83.     fi
  84.  
  85.     # Simplify name by removing common separators and converting to lowercase
  86.     name="${name//-/}"  # Remove dashes
  87.     name="${name//_/}"  # Remove underscores
  88.     echo "${name,,}"    # Convert to lowercase
  89. }
  90.  
  91. write_launcher_script() {
  92.     local app_id="$1"
  93.     local script_path="$2"
  94.  
  95.     cat > "$script_path" << EOF
  96. #!/bin/bash
  97. #GENERATED-FLATPAK-LAUNCHER
  98.  
  99. kdialog --passivepopup "Launching $app_id" 2 &
  100.  
  101. # Launch the Flatpak application with nohup, outputting to a uniquely named log file in /tmp, and run in the background
  102. nohup flatpak run "$app_id" "\$@" > /tmp/\$(basename "$script_path").\$\$.log 2>&1 &
  103. disown
  104. EOF
  105.     chmod +x "$script_path"
  106. }
  107.  
  108.  
  109. generate_or_update_launchers() {
  110.     # Get the maximum length of application IDs
  111.     max_length=$(flatpak list --app --columns=application | awk '{ if (length($0) > max) max = length($0); } END { print max; }')
  112.  
  113.     flatpak list --app --columns=application | while read app_id; do
  114.         script_name=$(generate_script_name "$app_id")
  115.         script_path="$script_dir/$script_name"
  116.  
  117.         # Check if the script exists and does NOT contain the GENERATED-FLATPAK-LAUNCHER tag
  118.         if [[ -f "$script_path" && ! $(grep -q "#GENERATED-FLATPAK-LAUNCHER" "$script_path"; echo $?) -eq 0 ]]; then
  119.             echo "Will not overwrite existing script (not generated by this tool): $script_path"
  120.             continue  # Skip to the next app_id
  121.         fi
  122.  
  123.         # Determine the action message based on the existence and content of script_path
  124.         if [[ -f "$script_path" ]]; then
  125.             action_message="Update"
  126.         else
  127.             action_message="Create"
  128.         fi
  129.        
  130.         # Pad app_id to the right to ensure it has a fixed length
  131.         printf -v padded_app_id "%-${max_length}s" "$app_id"
  132.        
  133.         if $dry_run; then
  134.             echo "$action_message (dry run) [$padded_app_id]: $script_path"
  135.         else
  136.             echo "$action_message [$padded_app_id]: $script_path"
  137.             write_launcher_script "$app_id" "$script_path"
  138.         fi
  139.     done
  140. }
  141.  
  142. # Main operation for generating or updating launcher scripts
  143. if [[ "$operation" == "generate" ]]; then
  144.     generate_or_update_launchers
  145. elif [[ "$operation" == "remove" ]]; then
  146.     remove_generated_scripts
  147. fi
  148.  
  149. echo "Operation concluded."
  150.  
Tags: flatpak
Advertisement
Add Comment
Please, Sign In to add comment