summaryrefslogtreecommitdiff
path: root/clide
blob: a748a83622d1c625c5de321ed9c80c1559b11199 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#-
# This file is part of Clide.
#
# Clide 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 3 of the License, or
# (at your option) any later version.
#
# Clide 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.
#
# You should have received a copy of the GNU General Public License
# along with Clide.  If not, see <http://www.gnu.org/licenses/>.
#

#!/bin/sh

__help()
{
	echo ''
}

__update_copyright()
{
	local YEAR=$(date +%Y)
	local COPYRIGHT=copyright.txt  # Filename of the copyright to append to the source code

	# Extension, to know wich symbol to use for the copyright
	local EXTENSION="${1##*.}"
	case $EXTENSION in
		java|c|h)   local START='/*-'; local END=' */'; local ROW=' \*';;
		properties) local START='#-';  local END='#';   local ROW='#'  ;;
		Makefile)   local START='#-';  local END='#';   local ROW='#'  ;;
	esac

	# This is the range between the start and the end of the copyright header
	local RANGE=$(echo $(cat $1 | grep -n "^$START$\|^$END$" | head -2 | sed -E 's/([0-9]+):.*/\1/g') | tr ' ', ',')

	if [ -z $RANGE ]; then
		# If the range is empty, copyright notice must be added
		RANGE=0a
		local ADD_BLANK_LINE=1
	else
		# Otherwise must be replaced
		RANGE=${RANGE}c
	fi

	# Generate the content with
	local CONTENT=$(cat $COPYRIGHT \
		| sed "s/%YEAR%/$YEAR/g" \
		| sed -E "s/^(.+)/$ROW \1/g" | sed -E "s/^$/$ROW/g")

	if [ ! -z $ADD_BLANK_LINE ]; then
ed "$1" <<EOF
$RANGE
$START
$CONTENT
$END

.
w
q
EOF
	else
ed "$1" <<EOF
$RANGE
$START
$CONTENT
$END
.
w
q
EOF
	fi
}

if [ $# -eq 0 ]; then
	__help
	exit 1
fi

while [ $# -gt 0 ]; do
	case $1 in
		--update-copyright|-uc) __update_copyright $2;;
	esac
	shift
done