Files
mgmt/misc/header.sh
Karpfen 7e8ced534f misc: Use /usr/bin/env for a more generic shebang
Use path based SHELL in Makefiles. It was suggested that this is a
better solution for make for cases when there is no /usr/bin/env.

See: https://github.com/purpleidea/mgmt/pull/694#discussion_r1015596204
2025-03-22 14:53:21 -04:00

28 lines
835 B
Bash
Executable File

#!/usr/bin/env bash
if [ "$1" = "" ] || [ "$1" = "--help" ]; then
echo "usage: append standard header to file"
echo "./$(basename "$0") <file> | --help"
exit 1
fi
ROOT="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && cd .. && pwd )" # dir!
FILE="${ROOT}/main.go" # file headers should match main.go
COUNT=0
while IFS='' read -r line; do # find what header should look like
echo "$line" | grep -q '^//' || break
COUNT=`expr $COUNT + 1`
done < "$FILE"
#cd "${ROOT}"
COUNT=`expr $COUNT + 1` # add one extra newline
# detect if header is correct before blasting another one in
if diff -q <( head -n $COUNT "$1" ) <( head -n $COUNT "$FILE" ) &>/dev/null; then
exit 0
fi
tmpfile=`mktemp` # get a temp file
# the output of the main.go header, dumped onto the file
head -n $COUNT "$FILE" | cat - "$1" > "$tmpfile" && mv "$tmpfile" "$1"