blob: 5051c61fcad642e2cc8d059b7e22388a2deefa99 [file] [log] [blame]
Bob Badour7a958202021-01-07 03:34:31 +00001#!/bin/sh
2
3set -u
4
5ME=$(basename $0)
6
7USAGE="Usage: ${ME} {options}
8
9Builds a license metadata specification and outputs it to stdout or {outfile}.
10
11The available options are:
12
13-k kind... license kinds
14-c condition... license conditions
15-p package... license package name
16-n notice... license notice file
17-d dependency... license metadata file dependency
Bob Badour879cfa82021-04-15 10:41:38 -070018-s dependency... source (input) dependency
19-t target... built targets
20-i target... installed targets
21-r root... root directory of project
Bob Badour7a958202021-01-07 03:34:31 +000022-m target:installed... map dependent targets to their installed names
Bob Badour879cfa82021-04-15 10:41:38 -070023-mc module_class... module class
24-mt module_type... module type
Bob Badour7a958202021-01-07 03:34:31 +000025-is_container preserved dependent target name when given
26-o outfile output file
27"
28
29# Global flag variables
30license_kinds=
31license_conditions=
32license_package_name=
33license_notice=
34license_deps=
Bob Badour879cfa82021-04-15 10:41:38 -070035module_class=
36module_type=
37source_deps=
Bob Badour7a958202021-01-07 03:34:31 +000038targets=
Bob Badour879cfa82021-04-15 10:41:38 -070039installed=
Bob Badour7a958202021-01-07 03:34:31 +000040installmap=
41is_container=false
42ofile=
Bob Badour879cfa82021-04-15 10:41:38 -070043roots=
Bob Badour7a958202021-01-07 03:34:31 +000044
45# Global variables
46depfiles=" "
Bob Badour7a958202021-01-07 03:34:31 +000047
48
49# Exits with a message.
50#
51# When the exit status is 2, assumes a usage error and outputs the usage message
52# to stderr before outputting the specific error message to stderr.
53#
54# Parameters:
55# Optional numeric exit status (defaults to 2, i.e. a usage error.)
56# Remaining args treated as an error message sent to stderr.
57die() {
58 lstatus=2
59 case "${1:-}" in *[^0-9]*) ;; *) lstatus="$1"; shift ;; esac
60 case "${lstatus}" in 2) echo "${USAGE}" >&2; echo >&2 ;; esac
61 if [ -n "$*" ]; then
62 echo -e "$*\n" >&2
63 fi
64 exit $lstatus
65}
66
67
68# Sets the flag variables based on the command-line.
69#
70# invoke with: process_args "$@"
71process_args() {
72 lcurr_flag=
73 while [ "$#" -gt '0' ]; do
74 case "${1}" in
75 -h)
76 echo "${USAGE}"
77 exit 0
78 ;;
79 -k)
80 lcurr_flag=kind
81 ;;
82 -c)
83 lcurr_flag=condition
84 ;;
85 -p)
86 lcurr_flag=package
87 ;;
88 -n)
89 lcurr_flag=notice
90 ;;
91 -d)
92 lcurr_flag=dependency
93 ;;
Bob Badour879cfa82021-04-15 10:41:38 -070094 -s)
95 lcurr_flag=source
96 ;;
Bob Badour7a958202021-01-07 03:34:31 +000097 -t)
98 lcurr_flag=target
99 ;;
Bob Badour879cfa82021-04-15 10:41:38 -0700100 -i)
101 lcurr_flag=install
102 ;;
Bob Badour7a958202021-01-07 03:34:31 +0000103 -m)
104 lcurr_flag=installmap
105 ;;
Bob Badour879cfa82021-04-15 10:41:38 -0700106 -mc)
107 lcurr_flag=class
108 ;;
109 -mt)
110 lcurr_flag=type
111 ;;
Bob Badour7a958202021-01-07 03:34:31 +0000112 -o)
113 lcurr_flag=ofile
114 ;;
Bob Badour879cfa82021-04-15 10:41:38 -0700115 -r)
116 lcurr_flag=root
117 ;;
Bob Badour7a958202021-01-07 03:34:31 +0000118 -is_container)
119 lcurr_flag=
120 is_container=true
121 ;;
122 -*)
123 die "Unknown flag: \"${1}\""
124 ;;
125 *)
126 case "${lcurr_flag}" in
127 kind)
128 license_kinds="${license_kinds}${license_kinds:+ }${1}"
129 ;;
130 condition)
131 license_conditions="${license_conditions}${license_conditions:+ }${1}"
132 ;;
133 package)
134 license_package_name="${license_package_name}${license_package_name:+ }${1}"
135 ;;
136 notice)
137 license_notice="${license_notice}${license_notice:+ }${1}"
138 ;;
139 dependency)
140 license_deps="${license_deps}${license_deps:+ }${1}"
141 ;;
Bob Badour879cfa82021-04-15 10:41:38 -0700142 source)
143 source_deps="${source_deps}${source_deps:+ }${1}"
144 ;;
Bob Badour7a958202021-01-07 03:34:31 +0000145 target)
146 targets="${targets}${targets:+ }${1}"
147 ;;
Bob Badour879cfa82021-04-15 10:41:38 -0700148 install)
149 installed="${installed}${installed:+ }${1}"
150 ;;
Bob Badour7a958202021-01-07 03:34:31 +0000151 installmap)
152 installmap="${installmap}${installmap:+ }${1}"
153 ;;
Bob Badour879cfa82021-04-15 10:41:38 -0700154 class)
155 module_class="${module_class}${module_class:+ }${1}"
156 ;;
157 type)
158 module_type="${module_type}${module_type:+ }${1}"
159 ;;
160 root)
161 root="${1}"
162 while [ -n "${root}" ] && ! [ "${root}" == '.' ] && \
163 ! [ "${root}" == '/' ]; \
164 do
165 if [ -d "${root}/.git" ]; then
166 roots="${roots}${roots:+ }${root}"
167 break
168 fi
169 root=$(dirname "${root}")
170 done
171 ;;
Bob Badour7a958202021-01-07 03:34:31 +0000172 ofile)
173 if [ -n "${ofile}" ]; then
174 die "Output file -o appears twice as \"${ofile}\" and \"${1}\""
175 fi
176 ofile="${1}"
177 ;;
178 *)
179 die "Must precede argument \"${1}\" with type flag."
180 ;;
181 esac
182 ;;
183 esac
184 shift
185 done
186}
187
Bob Badour7a958202021-01-07 03:34:31 +0000188
189process_args "$@"
190
191if [ -n "${ofile}" ]; then
192 # truncate the output file before appending results
193 : >"${ofile}"
194else
195 ofile=/dev/stdout
196fi
197
198# spit out the license metadata file content
199(
Bob Badour879cfa82021-04-15 10:41:38 -0700200 echo 'license_package_name: "'"${license_package_name}"'"'
201 for t in ${module_type}; do
202 echo 'module_type: "'"${t}"'"'
203 done
204 for c in ${module_class}; do
205 echo 'module_class: "'"${c}"'"'
206 done
207 for r in ${roots}; do
208 echo 'root: "'"${r}"'"'
209 done
Bob Badour7a958202021-01-07 03:34:31 +0000210 for kind in ${license_kinds}; do
Bob Badour879cfa82021-04-15 10:41:38 -0700211 echo 'license_kind: "'"${kind}"'"'
Bob Badour7a958202021-01-07 03:34:31 +0000212 done
213 for condition in ${license_conditions}; do
Bob Badour879cfa82021-04-15 10:41:38 -0700214 echo 'license_condition: "'"${condition}"'"'
Bob Badour7a958202021-01-07 03:34:31 +0000215 done
216 for f in ${license_notice}; do
Bob Badour879cfa82021-04-15 10:41:38 -0700217 echo 'license_text: "'"${f}"'"'
Bob Badour7a958202021-01-07 03:34:31 +0000218 done
219 echo "is_container: ${is_container}"
220 for t in ${targets}; do
Bob Badour879cfa82021-04-15 10:41:38 -0700221 echo 'built: "'"${t}"'"'
222 done
223 for i in ${installed}; do
224 echo 'installed: "'"${i}"'"'
Bob Badour7a958202021-01-07 03:34:31 +0000225 done
226 for m in ${installmap}; do
Bob Badour879cfa82021-04-15 10:41:38 -0700227 echo 'install_map: "'"${m}"'"'
228 done
229 for s in ${source_deps}; do
230 echo 'source: "'"${s}"'"'
Bob Badour7a958202021-01-07 03:34:31 +0000231 done
232) >>"${ofile}"
Bob Badour879cfa82021-04-15 10:41:38 -0700233depfiles=" $(echo $(echo ${license_deps} | tr ' ' '\n' | sort -u)) "
Bob Badour7a958202021-01-07 03:34:31 +0000234for dep in ${depfiles}; do
Bob Badour879cfa82021-04-15 10:41:38 -0700235 echo 'dep: "'"${dep}"'"'
Bob Badour7a958202021-01-07 03:34:31 +0000236done >>"${ofile}"