Skip to content

Instantly share code, notes, and snippets.

@Elemecca
Created January 12, 2013 01:26
Show Gist options
  • Save Elemecca/4515535 to your computer and use it in GitHub Desktop.
Save Elemecca/4515535 to your computer and use it in GitHub Desktop.
Prepends a license notice to a set of files.
#!/bin/bash
#
# addlicense.sh - prepends a license notice to a set of files
#
# Originally written by Sam Hanes <sam@maltera.com>.
# To the extent possible under law, the author has waived all copyright
# and related or neighboring rights in this work, which was originally
# published in the United States. Attribution is appreciated but not
# required. The complete legal text of the release is available at
# http://creativecommons.org/publicdomain/zero/1.0/
if [[ $# -lt 3 ]]; then
echo "usage: addlicense.sh <license file> <target dir> <extension...>" >&2
exit 1
fi
license=$1
target=$2
shift; shift
if [[ ! -f "$license" ]]; then
echo "license file '$license' is not a regular file" >&2
exit 1
fi
if [[ ! -d "$target" ]]; then
echo "target directory '$target' is not a directory" >&2
exit 1
fi
predicate="( -name *.$1"
shift
for ext in $@; do
predicate="$predicate -o -name *.$ext"
done
predicate="$predicate )"
for file in `find -L "$target" \( -type f -a $predicate \) -print`; do
echo $file
cat "$license" "$file" > /tmp/licenseupdate
if [[ $? -ne 0 ]]; then
echo "failed to concatenate file '$file'" >&2
continue
fi
mv /tmp/licenseupdate $file
if [[ $? -ne 0 ]]; then
echo "failed to replace file '$file'" >&2
continue
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment