-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlicense.sh
More file actions
82 lines (64 loc) · 1.63 KB
/
license.sh
File metadata and controls
82 lines (64 loc) · 1.63 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
#!/bin/sh
set -e
license_url="https://github.com/dk949/LICENSES.git"
cache_dir="${XDG_CACHE_HOME:-$HOME/.cache}"
year=$(date +'%Y')
die() {
#shellcheck disable=SC2059 # all args are forwarded to printf
printf "$@"
echo
exit 1
}
help() {
echo "Usage: $(basename "$0") LICENSE"
echo
echo " -l|--list List available licences"
echo " -c|--cache Ensure list of licenses is cached"
echo " -u|--update Update cached list of license files"
echo " -h|--help Print help message"
}
cache() {
[ -z "$cache_dir" ] && die "could not locate cache directory"
license_dir="$cache_dir/LICENSES"
if [ ! -d "$license_dir" ]; then
git clone "$license_url" "$license_dir"
fi
}
list() {
cache
\find "$license_dir" -maxdepth 1 -type f -exec "basename" "{}" ";"
}
update() {
cache
git -C "$license_dir" pull
}
[ $# -ne 1 ] && die "%s" "$(help)"
case $1 in
"-l"|"--list")
list
exit 0
;;
"-c"|"--cache")
cache
echo "Licenses have been cached"
exit 0
;;
"-u"|"--update")
update
echo "Licenses cache is up to date"
exit 0
;;
"-h"|"--help")
help
exit 0
;;
esac
cache
___USER=$(git config user.name)
[ -z "$___USER" ] && ___USER="$USER"
[ -z "$___USER" ] && die "could not determine username"
[ -f "LICENSE" ] && die '"LICENSE" already exists. Back it up and try again.'
[ ! -f "$license_dir/$1" ] && die "No such license %s
Available licenses are:
%s" "$1" "$(list)"
sed "s/=YEAR=/$year/;s/=AUTHOR=/$___USER/" < "$license_dir/$1" > ./LICENSE