Add '-f' option to force overwriting to '_templates/add.sh'
[senf.git] / _templates / add.sh
1 #!/bin/sh -e
2
3 if [ -z "$1" ]; then
4     cat <<EOF
5 Usage: $0 [-f] <name>.<ext> [<author>]
6
7 Will add the file with that name to the current directory by copying
8 the appropriate template and replacing the @NAME@ placeholders
9 appropriately.
10
11 <ext> needs to be one of
12
13   hh   header
14   ih   internal header
15   cc   non-inline non-template code
16   cci  inline non-template code
17   ct   non-inline template code
18   cti  inline template code
19   mpp  special Boost.Preprocessor sourcefile (external iteration)
20   dox  additional doxygen documentation file
21
22 If <author> is given, it is also set correctly replaced. If <author>
23 is unset but the environment variable SENF_AUTHOR is set, it's value
24 is used. Otherwise, the @AUTHOR@ placeholder will need to be replaced
25 manually.
26
27 $0 will refrain from overwriting existing files except when the '-f'
28 option is specified.
29 EOF
30     exit 1
31 fi
32
33 base="`dirname "$0"`"; base="`cd "$base"; pwd`"
34
35 if [ "$1" == "-f" ]; then
36     force=1
37     shift
38 fi
39
40 type="${1##*.}"
41 name="`basename "$1" ".$type"`"
42
43 if [ ! -r "$base/Example.$type" ] ; then
44     echo "Don't know about file type '$type'"
45     exit 1
46 fi
47
48 [ -z "$2" ] || SENF_AUTHOR="$2"
49
50 if [ -z "$force" -a -r "$1" ] ; then
51     echo "Target file '$1' exists."
52     exit 1
53 fi
54
55 sed -e "s/@NAME@/$name/g" -e "s/@AUTHOR@/${SENF_AUTHOR:-@AUTHOR@}/g" \
56     < "$base/Example.$type" \
57     > "$1"
58
59 svn add "$1"
60 svn propset svn:keywords "Author Date Id Revision" "$1"