2019-01-13 06:25:44 +01:00
|
|
|
# Helper function for calling sed on files and checking if the
|
|
|
|
# file is actually changed
|
|
|
|
#
|
|
|
|
# NOTE: this will not check if the input is valid, you can problably
|
|
|
|
# make it execute arbirtrary commands via passing '; cmd' to a vsed
|
|
|
|
# call.
|
|
|
|
|
|
|
|
vsed() {
|
2019-02-18 11:54:33 +01:00
|
|
|
local files=() regexes=() OPTIND OPTSTRING="ie:" has_inline=
|
2019-01-13 06:25:44 +01:00
|
|
|
|
2021-02-17 12:22:49 +01:00
|
|
|
eval set -- "$(getopt -s bash "$OPTSTRING" "$@")";
|
2019-02-18 11:54:33 +01:00
|
|
|
|
|
|
|
while getopts "$OPTSTRING" opt; do
|
2019-01-13 06:25:44 +01:00
|
|
|
case $opt in
|
2019-02-18 11:54:33 +01:00
|
|
|
i) has_inline=1 ;;
|
2019-01-13 06:25:44 +01:00
|
|
|
e) regexes+=("$OPTARG") ;;
|
|
|
|
*) ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2019-02-18 11:54:33 +01:00
|
|
|
if ! [ "$has_inline" ]; then
|
|
|
|
msg_red "$pkgver: vsed: you must specify -i.\n"
|
2019-01-13 06:25:44 +01:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2019-02-18 11:54:33 +01:00
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
|
|
|
if [ ${#regexes[@]} -eq 0 ] && [ $# -ge 2 ]; then
|
|
|
|
regexes+=("$1")
|
|
|
|
shift
|
|
|
|
fi
|
|
|
|
|
2019-01-13 06:25:44 +01:00
|
|
|
if [ ${#regexes[@]} -eq 0 ]; then
|
2019-02-18 11:54:33 +01:00
|
|
|
msg_red "$pkgver: vsed: no regexes specified.\n"
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
for i; do
|
|
|
|
files+=("$i")
|
|
|
|
done
|
|
|
|
|
|
|
|
if [ ${#files[@]} -eq 0 ]; then
|
|
|
|
msg_red "$pkgver: vsed: no files specified.\n"
|
2019-01-13 06:25:44 +01:00
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
|
2019-05-12 05:32:37 +02:00
|
|
|
for f in "${files[@]}"; do
|
|
|
|
olddigest="$($XBPS_DIGEST_CMD "$f")"
|
|
|
|
olddigest="${olddigest%% *}"
|
2019-01-13 06:25:44 +01:00
|
|
|
|
2019-05-12 05:32:37 +02:00
|
|
|
for rx in "${regexes[@]}"; do
|
2019-01-13 06:25:44 +01:00
|
|
|
sed -i "$f" -e "$rx" || {
|
|
|
|
msg_red "$pkgver: vsed: sed call failed with regex \"$rx\" on file \"$f\"\n"
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2019-05-12 05:17:13 +02:00
|
|
|
newdigest="$($XBPS_DIGEST_CMD "$f")"
|
2019-05-12 05:25:17 +02:00
|
|
|
newdigest="${newdigest%% *}"
|
2019-01-13 06:25:44 +01:00
|
|
|
|
2019-05-12 05:25:17 +02:00
|
|
|
if [ "$olddigest" = "$newdigest" ]; then
|
2019-01-13 06:25:44 +01:00
|
|
|
msg_warn "$pkgver: vsed: regex \"$rx\" didn't change file \"$f\"\n"
|
|
|
|
fi
|
2019-05-12 05:32:37 +02:00
|
|
|
olddigest="${newdigest}"
|
2019-01-13 06:25:44 +01:00
|
|
|
done
|
|
|
|
done
|
|
|
|
}
|