Files
manjaro-sway-mirror-arm-patch/src/patch-manjaro-sway-mirror-for-arm.sh
2023-05-28 21:35:02 +00:00

77 lines
1.8 KiB
Bash
Executable File

#!/bin/env sh
usage() {
echo "Usage: patch-manjaro-sway-mirror-for-arm.sh [OPTIONS]"
echo "Options:"
echo " -t, --target-file Path to the target file (default: /usr/bin/manjaro-sway-mirrors)"
echo " -s, --search-pattern Search pattern to replace (default: $(pacman-mirrors -G)"
echo " -r, --replace-pattern Replace pattern (default: \$(pacman-mirrors -G \| sed 's/^arm-//'))"
echo " --dry-run Run the sed command without modifying the file"
echo " --help Display this help message"
exit 0
}
# Default values
dry_run=false
# Parse command-line options
while [ $# -gt 0 ]; do
case "$1" in
-t | --target-file)
shift
target_file="$1"
;;
-s | --search-pattern)
shift
search_pattern="$1"
;;
-r | --replace-pattern)
shift
replace_pattern="$1"
;;
--dry-run)
dry_run=true
;;
--help)
usage
;;
*)
# Check if positional arguments are provided
if [ -z "$target_file" ]; then
target_file="$1"
elif [ -z "$search_pattern" ]; then
search_pattern="$1"
elif [ -z "$replace_pattern" ]; then
replace_pattern="$1"
else
echo "Invalid argument: $1"
usage
fi
;;
esac
shift
done
if [ -z "$target_file" ]; then
target_file="${MSMAP_TARGET_ROOT}/usr/bin/manjaro-sway-mirrors"
fi
if [ -z "$search_pattern" ]; then
# shellcheck disable=SC2016
search_pattern='\$(pacman-mirrors -G)'
fi
if [ -z "$replace_pattern" ]; then
# shellcheck disable=SC2016
replace_pattern='\$(pacman-mirrors -G \| sed \"s/^arm-//\")'
fi
sed_command="sed -i \"s|${search_pattern}|${replace_pattern}|g\" \"${target_file}\""
# Perform the sed command
if $dry_run; then
echo "$sed_command"
sed "s|${search_pattern}|${replace_pattern}|g" "${target_file}"
else
eval "${sed_command}"
fi