Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions addcopyright.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
#!/bin/bash

## Solution found as part of this stackoverflow discussion: https://stackoverflow.com/questions/151677/tool-for-adding-license-headers-to-source-files
declare -A applyRE
declare -A addpre
declare -A addpost

for x in $*; do
head -$COPYRIGHTLEN $x | diff copyright.txt - || ( ( cat copyright.txt; echo; cat $x) > /tmp/file;
mv /tmp/file $x )
done
addpre[c]='/*'
applyRE[c]='s/^/ * /'
addpost[c]=' */'

applyRE[shell]='s/^/# /'
applyRE[matlab]='s/^/% /'
applyRE[lua]='s/^/-- /'

if [[ x$1 == x ]]; then
echo 'Must provide comments style as first argument' >&2
exit 1
elif [[ -v "applyRE[$1]" ]] ; then
STYLE=$1
else
echo "Unknown style"
exit 2
fi

if [[ x$2 == x ]]; then
# Apply to all files, excluding directories
FILES=$(find . -type f)
else
D=$(dirname "$2")
F="${2#${D}/}"
FILES=$(find $D -type f -name "$F" -print)
fi


tmpcopyright=$(mktemp /tmp/addcopyright.XXXXXX)

if [[ -v "addpre[$STYLE]" ]] ; then echo "${addpre[$STYLE]}" > $tmpcopyright ; fi
sed "${applyRE[$STYLE]}" < copyright.txt >> $tmpcopyright
if [[ -v "addpost[$STYLE]" ]] ; then echo "${addpost[$STYLE]}" >> $tmpcopyright; fi
COPYRIGHTLEN=$(wc -l < $tmpcopyright)

# Solution found as part of this stackoverflow discussion:
# https://stackoverflow.com/questions/151677/tool-for-adding-license-headers-to-source-files
for x in $FILES; do
tmpsrcfile=$(mktemp /tmp/addcopyright.XXXXXX)
head -$COPYRIGHTLEN $x | diff -q $tmpcopyright - > /dev/null
if [[ $? == 1 ]]; then
(cat $tmpcopyright; cat $x) > $tmpsrcfile
mv $tmpsrcfile $x
fi
done

#rm -f $tmpcopyright $tmpsrcfile

exit 0
16 changes: 7 additions & 9 deletions copyright.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/*
© Copyright 2020 tapasadhikary.com or one of its affiliates.
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
*/
(c) Copyright 2022 tapasadhikary.com or one of its affiliates.
Some Sample Copyright Text Line
Some Sample Copyright Text Line
Some Sample Copyright Text Line
Some Sample Copyright Text Line
Some Sample Copyright Text Line
Some Sample Copyright Text Line
3 changes: 2 additions & 1 deletion drill/abcd.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ const abcd = () => {
console.log('abcd');
}

abcd();
abcd();

5 changes: 5 additions & 0 deletions drill/abcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

def abcd():
print('abcd')

abcd()
13 changes: 7 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
/*
© Copyright 2020 tapasadhikary.com or one of its affiliates.
/*
* (c) Copyright 2022 tapasadhikary.com or one of its affiliates.
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
* Some Sample Copyright Text Line
*/
*/

const test = () => {
console.log('test');
const abcd = () => {
console.log('abcd');
}

test();
abcd();

12 changes: 12 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# (c) Copyright 2022 tapasadhikary.com or one of its affiliates.
# Some Sample Copyright Text Line
# Some Sample Copyright Text Line
# Some Sample Copyright Text Line
# Some Sample Copyright Text Line
# Some Sample Copyright Text Line
# Some Sample Copyright Text Line

def abcd():
print('abcd')

abcd()