-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepo-add-license
executable file
·125 lines (90 loc) · 2.3 KB
/
repo-add-license
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
#!/usr/bin/env bash
#
# Add a LICENSE file to a software repository.
#
# Supported SPDX License Identifiers:
# - MIT
# - BSD-2-Clause
#
# Downloads a template file from <https://f.malte70.de/>, and inserts
# the current year as well as your name into it (using placeholders).
#
SCRIPT_NAME="$(basename $0)"
SCRIPT_VERSION="0.20230502"
source $(dirname $0)/_base.inc.sh
#
# Supported SPDX IDs
#
LICENSE_IDS_SUPPORTED=("MIT" "BSD-2-Clause")
#
# Usage help
#
usage() {
echo "Usage:"
echo "\t$SCRIPT_NAME [--help|--version]"
echo "\t$SCRIPT_NAME [<spdx-license-id>]"
echo
echo "Currently supported license IDs:"
echo "\tMIT"
echo "\tBSD-2-Clause"
echo "\t(or none to use the default license)"
echo
echo "$0 places a LICENSE file in the current directory; it's generated from a template,"
echo "with usable default values for the placeholders \"[year]\" and \"[fullname]\"."
echo
echo "The template files are always downloaded from <https://f.malte70.de/>, with this naming convention:"
echo -e "\tLICENSE.tpl *"
echo -e "\tLICENSE.${_ANSI_ATTR_ITALIC}<spdx-license-id>${_ANSI_ATTR_ITALIC_RESET}.tpl"
echo
echo "If no license ID was specified at the command line, a default template (*) is used;"
echo "which is just a symlink on the webserver."
echo
}
#
# Parse command line arguments
#
LICENSE_ID=""
while [[ $# -gt 0 ]]; do
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
version
usage
exit 0
elif [[ "$1" == "--version" || "$1" == "-V" ]]; then
version
exit 0
elif [[ "${1:0:1}" == "-" && $CFG_DASHDASH -ne 1 ]]; then
message_error "Unknown argument: $1"
exit 1
else
LICENSE_ID="$1"
fi
shift
done
#
# Get template URL
#
[[ -n "$LICENSE_ID" ]] \
&& LICENSE_ID=".${LICENSE_ID}"
LICENSE_TEMPLATE="https://f.malte70.de/LICENSE$LICENSE_ID.tpl"
#
# Don't overwrite existing files
#
if [[ -f "LICENSE" ]]; then
message_error "File already exists: LICENSE"
exit 2
fi
#
# Get the current year and the user's real name,
# to replace the placeholders
#
_year="$(date +%Y)"
_fullname="$(getent passwd $USER | cut -d: -f5)"
[[ -z "$_fullname" ]] && _fullname="$USER"
#
# Download, replace placeholders and save to LICENSE on-the-fly
#
curl -s -o- \
"${LICENSE_TEMPLATE}" \
| sed "s/\[year\]/$_year/g" \
| sed "s/\[fullname\]/$_fullname/g" \
> LICENSE