Skip to content

Instantly share code, notes, and snippets.

@Zegnat
Created November 19, 2025 20:34
Show Gist options
  • Select an option

  • Save Zegnat/64a475b2c61e24d2cb87cbb305002bdf to your computer and use it in GitHub Desktop.

Select an option

Save Zegnat/64a475b2c61e24d2cb87cbb305002bdf to your computer and use it in GitHub Desktop.
#!/bin/sh
# Portable POSIX script to append rm command to all launch.sh files
# found in Emus/*/.pak/ directories to remove recent.txt file.
#
# Usage: ./append_comment.sh [--dryrun]
# --dryrun: Show what would be changed without modifying files
COMMAND_TO_ADD='rm -f "$SHARED_USERDATA_PATH/.minui/recent.txt"'
TARGET_FILENAME="launch.sh"
# Dry run mode flag
dryrun=0
# Counters
modified=0
skipped=0
errors=0
# Function to append command to a file
append_command_to_file() {
file_path="$1"
# Check if command already exists (look for rm at start of line with recent.txt)
if grep -q "^rm.*recent\.txt" "$file_path" 2>/dev/null; then
printf "⏭️ Skipped (already exists): %s\n" "$file_path"
skipped=$((skipped + 1))
return
fi
# Dry run mode - just log what would happen
if [ "$dryrun" -eq 1 ]; then
printf "πŸ” Would modify: %s\n" "$file_path"
modified=$((modified + 1))
return
fi
# Check if file ends with newline (busybox-compatible method)
last_char=$(tail -c 1 "$file_path" 2>/dev/null | od -An -tx1 | tr -d ' ')
if [ -n "$last_char" ] && [ "$last_char" != "0a" ]; then
# File doesn't end with newline, add one
printf "\n%s\n" "$COMMAND_TO_ADD" >> "$file_path"
else
# File ends with newline, just append command
printf "%s\n" "$COMMAND_TO_ADD" >> "$file_path"
fi
if [ $? -eq 0 ]; then
printf "βœ… Modified: %s\n" "$file_path"
modified=$((modified + 1))
else
printf "❌ Error processing: %s\n" "$file_path"
errors=$((errors + 1))
fi
}
# Function to check if path is in Emus/*/.pak/ directory structure
is_in_emus_pak_directory() {
file_path="$1"
# Check if path contains "Emus" directory and a .pak directory after it
# Matches both:
# - Emus/*.pak/* (direct .pak under Emus)
# - Emus/*/*.pak/* (.pak under a subdirectory of Emus)
case "$file_path" in
*/Emus/*.pak/* | */Emus/*/*.pak/*)
return 0 # true
;;
*)
return 1 # false
;;
esac
}
# Recursive function to find and process launch.sh files
process_directory() {
dir="$1"
# Process all entries in the directory
for entry in "$dir"/* "$dir"/.[!.]* "$dir"/..?*; do
# Skip if glob didn't match anything
[ -e "$entry" ] || continue
if [ -d "$entry" ]; then
# Recursively process subdirectories
process_directory "$entry"
elif [ -f "$entry" ]; then
# Check if filename matches and is in correct directory structure
basename_entry=$(basename "$entry")
if [ "$basename_entry" = "$TARGET_FILENAME" ]; then
if is_in_emus_pak_directory "$entry"; then
append_command_to_file "$entry"
fi
fi
fi
done
}
# Main execution
main() {
# Parse command line arguments
for arg in "$@"; do
case "$arg" in
--dryrun)
dryrun=1
;;
*)
printf "Unknown option: %s\n" "$arg"
printf "Usage: %s [--dryrun]\n" "$0"
exit 1
;;
esac
done
if [ "$dryrun" -eq 1 ]; then
printf "πŸ” DRY RUN MODE - No files will be modified\n"
fi
printf "πŸ” Scanning for launch.sh files in Emus/*/.pak/ directories...\n\n"
# Start processing from current directory
process_directory "."
# Print summary
printf "\n"
printf "==================================================\n"
printf "πŸ“Š Summary:\n"
if [ "$dryrun" -eq 1 ]; then
printf " Would modify: %d\n" "$modified"
else
printf " Modified: %d\n" "$modified"
fi
printf " Skipped: %d\n" "$skipped"
printf " Errors: %d\n" "$errors"
printf "==================================================\n"
}
# Run main function
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment