Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Directory for launcher scripts
- script_dir="$HOME/.local/bin"
- # Default operation mode
- operation="generate"
- dry_run=false
- # Display usage information
- show_help() {
- echo "Usage: $0 [option]"
- echo "Options:"
- echo " -h Display this help message"
- echo " -r Remove all generated launcher scripts"
- echo " -n Dry run (show actions without executing)"
- }
- # Remove generated launcher scripts
- remove_generated_scripts() {
- echo "Initiating removal of generated launcher scripts..."
- for script in "$script_dir"/*; do
- # Check for the GENERATED-FLATPAK-LAUNCHER tag
- if [[ -f "$script" && $(grep -q "#GENERATED-FLATPAK-LAUNCHER" "$script"; echo $?) -eq 0 ]]; then
- # Ensure the script is not protected by the FLATPAK-LAUNCHER-CREATOR tag
- if ! grep -q "#FLATPAK-LAUNCHER-CREATOR" "$script"; then
- if [[ "$dry_run" == true ]]; then
- echo "Removing: $script (dry run)"
- else
- echo "Removing: $script"
- rm "$script"
- fi
- else
- echo "Skipping: $script (protected)"
- fi
- fi
- done
- }
- # Parse command-line options
- while getopts ":hrn" opt; do
- case ${opt} in
- h)
- show_help
- exit 0
- ;;
- r)
- operation="remove"
- ;;
- n)
- dry_run=true
- ;;
- \?)
- echo "Invalid option: $OPTARG" 1>&2
- show_help
- exit 1
- ;;
- esac
- done
- # Ensure the script directory exists
- mkdir -p "$script_dir"
- # Generate launcher script name from Flatpak application ID
- generate_script_name() {
- local app_id=$1
- local name_parts IFS='.'
- read -ra name_parts <<< "$app_id"
- local name="${name_parts[-1]}" # Default to the last part
- # If the last part is too generic, use the prior segment that seems descriptive
- #if [[ "$name" == "desktop" || "$name" == "app" || "$name" == "net" || "$name" == "Browser" || "$name" == "browser" ]]; then
- # name="${name_parts[-2]}"
- #fi
- if [[ "${name,,}" =~ ^(desktop|app|net|browser)$ ]]; then
- name="${name_parts[-2]}"
- fi
- # For GitHub projects, prioritize the repository name directly after 'github'
- if [[ "$app_id" =~ github\.([^.]*)\.(.*) ]]; then
- name="${BASH_REMATCH[2]}"
- fi
- # Simplify name by removing common separators and converting to lowercase
- name="${name//-/}" # Remove dashes
- name="${name//_/}" # Remove underscores
- echo "${name,,}" # Convert to lowercase
- }
- write_launcher_script() {
- local app_id="$1"
- local script_path="$2"
- cat > "$script_path" << EOF
- #!/bin/bash
- #GENERATED-FLATPAK-LAUNCHER
- kdialog --passivepopup "Launching $app_id" 2 &
- # Launch the Flatpak application with nohup, outputting to a uniquely named log file in /tmp, and run in the background
- nohup flatpak run "$app_id" "\$@" > /tmp/\$(basename "$script_path").\$\$.log 2>&1 &
- disown
- EOF
- chmod +x "$script_path"
- }
- generate_or_update_launchers() {
- # Get the maximum length of application IDs
- max_length=$(flatpak list --app --columns=application | awk '{ if (length($0) > max) max = length($0); } END { print max; }')
- flatpak list --app --columns=application | while read app_id; do
- script_name=$(generate_script_name "$app_id")
- script_path="$script_dir/$script_name"
- # Check if the script exists and does NOT contain the GENERATED-FLATPAK-LAUNCHER tag
- if [[ -f "$script_path" && ! $(grep -q "#GENERATED-FLATPAK-LAUNCHER" "$script_path"; echo $?) -eq 0 ]]; then
- echo "Will not overwrite existing script (not generated by this tool): $script_path"
- continue # Skip to the next app_id
- fi
- # Determine the action message based on the existence and content of script_path
- if [[ -f "$script_path" ]]; then
- action_message="Update"
- else
- action_message="Create"
- fi
- # Pad app_id to the right to ensure it has a fixed length
- printf -v padded_app_id "%-${max_length}s" "$app_id"
- if $dry_run; then
- echo "$action_message (dry run) [$padded_app_id]: $script_path"
- else
- echo "$action_message [$padded_app_id]: $script_path"
- write_launcher_script "$app_id" "$script_path"
- fi
- done
- }
- # Main operation for generating or updating launcher scripts
- if [[ "$operation" == "generate" ]]; then
- generate_or_update_launchers
- elif [[ "$operation" == "remove" ]]; then
- remove_generated_scripts
- fi
- echo "Operation concluded."
Advertisement
Add Comment
Please, Sign In to add comment