Skip to content

Instantly share code, notes, and snippets.

@adamhotep
Created September 22, 2020 22:02
Show Gist options
  • Save adamhotep/957f5295040ce1636b71ab3479db6de4 to your computer and use it in GitHub Desktop.
Save adamhotep/957f5295040ce1636b71ab3479db6de4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This is a wrapper that adds one option to rgain3's replaygain script
# replaygain-wrapper version 0.1, Copyright (C) 2020+ by Adam Katz <@adamhotep>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# Full license: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
real="/usr/bin/replaygain"
help() {
"$real" --help
echo " -t, --preserve-times Keep files' original timestamps"
echo " -x, --executable=FILE Use this instead of '$real'"
exit
}
# the revised list of arguments (for the real program)
args=()
while getopts dfhr:tx:-: OPT; do
# support long options: https://stackoverflow.com/a/28466267/519360
if [ "$OPT" = - ]; then
OPT="${OPTARG%%=*}" OPTARG="${OPTARG#$OPT}" OPTARG="${OPTARG#=}"
fi
case $OPT in
( d | dry-run )
args+=(--dry-run)
NO_OP=true ;;
( h | help )
help ;;
( show )
args+=(--show)
NO_OP=true ;;
( t | preserve-time* )
PRESERVE_TIMES=true ;;
( x | exe* )
real="$OPTARG" ;;
( ??* ) # other long option
if [ -z "$OPTARG" ]
then args+=("--$OPT")
else args+=("--$OPT=$OPTARG")
fi
;;
( * ) # other short option
args+=("-$OPT")
if [ -n "$OPTARG" ]; then args+=("$OPTARG"); fi
esac
done
# remove options as originally provided, then prepend revised list (if any)
shift $((OPTIND-1))
set -- ${args+"${args[@]}"} "$@"
if [ "$NO_OP" = true ]; then
PRESERVE_TIMES=false
elif [ "$PRESERVE_TIMES" = true ]; then
TMP="$(mktemp ${TMPDIR:-/tmp}/rgain.$$.XXXXX.sh)"
trap "rm -f $TMP" 0 1 2 5 9 11 15 18
if stat -c %Y / >/dev/null 2>&1; then # GNU coreutils (stat & date)
when() { date -d@"$(stat -c %Y "$@")" +%Y%m%d%H%M.%S; }
else # BSD stat & date
when() { date -jf %s "$(stat -f %m "$@")" +%Y%m%d%H%M.%S; }
fi
for input in "$@"; do
echo "$(when "$input")=$input" >> $TMP
done
fi
"$real" "$@"
retval=$?
if [ "$PRESERVE_TIMES" = true ]; then
cat $TMP | while IFS= read -r line; do
touch -t "${line%%=*}" "${line#*=}"
done
fi
exit $retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment