-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_file_header.sh
More file actions
152 lines (133 loc) · 3.91 KB
/
create_file_header.sh
File metadata and controls
152 lines (133 loc) · 3.91 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
#
# Prints author, copyright and license info to insert in header of file
#
# Time-stamp: <Monday 2025-08-04 20:30:26 Jess Moore>
#
# Copyright (C) 2023-2025, Software Innovation Institute, ANU.
#
# Licensed under the GNU General Public License, Version 3 (the "License").
#
# License: https://www.gnu.org/licenses/gpl-3.0.en.html
#
# This program 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.
#
# This program 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
# this program. If not, see <https://www.gnu.org/licenses/>.
function usage() {
echo "Usage: create_file_header.sh [-t type] [-d desc]"
echo ""
echo "Description: Prints author, copyright and license info to insert in"
echo "header of file. Inserts a description at top (below slash bang), if "
echo "description provided."
echo ""
echo "Arguments:"
echo " -d desc: Optional file or package description. (No default)."
echo " -t type: Script type (bash, dart, py)"
echo ""
exit 1 # Exit with a non-zero status to indicate an error
}
if [[ $* == *"help"* || $* == *"-h"* ]]; then
usage
fi
NAME=$(id -F)
NOW=$(date "+%A %Y-%m-%d %H:%M:%S")
YEAR=$(date "+%Y")
RIGHTS_HOLDER=$NAME
LICENSE="GPLV3"
# Use this tmp header file
FILEBASE=tmp_header
# By default, is_shell is false
is_shell=false
# Parse arguments and flags
while getopts :hd:t: opt; do
case $opt in
h) usage;;
d) DESC="$OPTARG";;
t) TYPE="$OPTARG";;
:) echo "Missing argument for option -$OPTARG"; exit 1;;
\?) echo "Unknown option -$OPTARG"; exit 1;;
esac
done
# Shift positional parameters to remove processed options
shift $(( OPTIND - 1 ))
# Get script type, define extension and comment string
case "${TYPE}" in
'bash')
is_shell=true;
SLASHBANG="#!/bin/bash";
ext="sh";
c="#";;
'dart')
ext="dart";
c="//";;
'py')
ext='py';
c="#";;
*)
echo "Error: Supported script types are: bash, dart, py";
exit 1;;
esac
# Set temp filename
FILENAME=$FILEBASE.$ext
# Create header file
if [[ -e $FILENAME ]]; then
rm $FILENAME
fi
touch $FILENAME
# Add slash bang if shell
if $is_shell; then
printf "%s\n#\n" "$SLASHBANG" >> $FILENAME
fi
# Add description if provided
if [[ -n "$DESC" ]]; then
printf "%s %s\n%s\n" "$c" "$DESC" "$c" >> $FILENAME
fi
# Add timestamp
# shellcheck disable=SC2129
cat >> "${FILENAME}" << EOF
$c Time-stamp: <${NOW} ${NAME}>
$c
EOF
# Add copyright statement
cat >> "${FILENAME}" << EEOF
$c Copyright (C) $YEAR, $RIGHTS_HOLDER.
$c
EEOF
# Add license statement
if [[ $LICENSE == "GPLV3" ]]; then
cat >> "${FILENAME}" << EEEOF
$c Licensed under the GNU General Public License, Version 3 (the "License").
$c
$c License: https://www.gnu.org/licenses/gpl-3.0.en.html
$c
$c This program is free software: you can redistribute it and/or modify it under
$c the terms of the GNU General Public License as published by the Free Software
$c Foundation, either version 3 of the License, or (at your option) any later
$c version.
$c
$c This program is distributed in the hope that it will be useful, but WITHOUT
$c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
$c FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
$c details.
$c
$c You should have received a copy of the GNU General Public License along with
$c this program. If not, see <https://www.gnu.org/licenses/>.
$c
EEEOF
fi
# Add author
cat >> "${FILENAME}" << EEEEOF
$c Authors: $NAME
EEEEOF
# Print to stdout
cat $FILENAME
rm $FILENAME