2022-01-07 12:18:35 +00:00
|
|
|
#!/bin/sh -e
|
|
|
|
# Rename a file with a backup suffix or revert it
|
|
|
|
# Add -a to act on multiple files,
|
|
|
|
# otherwise the second arg is used as suffix rather than the default "bak".
|
2022-08-04 21:53:53 +00:00
|
|
|
# TODO no sudo for symlinks
|
2022-01-07 12:18:35 +00:00
|
|
|
if test "$1" = "-a"; then
|
|
|
|
shift
|
|
|
|
for arg; do "$0" "$arg"; done
|
|
|
|
exit $?
|
|
|
|
fi
|
2021-07-04 16:20:36 +00:00
|
|
|
suffix="${2:-bak}"
|
2021-07-04 19:56:12 +00:00
|
|
|
orig="${1%%.$suffix}"
|
2021-09-20 09:14:34 +00:00
|
|
|
smv () {
|
2022-01-07 12:18:35 +00:00
|
|
|
eval source=\$$(($#-1))
|
|
|
|
if test -w "$source"
|
|
|
|
then mv -v "$@"
|
|
|
|
else sudo mv -v "$@"
|
|
|
|
fi
|
2021-09-20 09:14:34 +00:00
|
|
|
}
|
2021-07-04 19:56:12 +00:00
|
|
|
if test -e "$orig.$suffix"
|
|
|
|
then
|
2022-01-07 12:18:35 +00:00
|
|
|
test -e "$orig" && smv "$orig" /tmp
|
|
|
|
smv -n "$orig.$suffix" "$orig"
|
2021-09-29 16:38:57 +00:00
|
|
|
else
|
2022-01-07 12:18:35 +00:00
|
|
|
smv -n "$1" "$1.$suffix"
|
2021-07-04 19:56:12 +00:00
|
|
|
fi
|