summaryrefslogtreecommitdiffstats
path: root/scripts/lib/ptxd_make_world_license.sh
blob: 5e590c20491b663eabe51444bb24ceefcbcd3dbd (plain)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/bin/bash
#
# Copyright (C) 2013 by Michael Olbrich <m.olbrich@pengutronix.de>
#
# See CREDITS for details about who has contributed to this project.
#
# For further information about the PTXdist project and license conditions
# see the README file.
#

#
# parse "license_files" line
#
# out:
#
# $file = path to file containing license
# $startline = first line of license
# $endline = last line of license
# $md5 = md5sum of license
# $guess = 'yes' if the file was not specified explicitly
#
ptxd_make_world_parse_license_files() {
    local orig_ifs="${IFS}"
    IFS=";"
    set -- ${@}
    IFS="${orig_ifs}"
    unset orig_ifs

    file=""
    startline="1"
    endline="$"
    md5=""
    guess=""

    while [ ${#} -ne 0 ]; do
	local arg="${1}"
	shift

	case "${arg}" in
	    "file://"*)
		file="${arg##file://}"
		;;
	    "beginline="*)
		startline="${arg##beginline=}"
		;;
	    "startline="*)
		startline="${arg##startline=}"
		;;
	    "endline="*)
		endline="${arg##endline=}"
		;;
	    "md5="*)
		md5="${arg##md5=}"
		;;
	    "guess="*)
		guess="${arg##guess=}"
		;;
	    *)
		ptxd_bailout "unknown option '${arg}'"
		;;
	esac
    done

    if [ ! -e "${pkg_dir}/${file}" ]; then
	ptxd_bailout "

license file '$(ptxd_print_path "${file}")'
not found
"
    fi
}
export -f ptxd_make_world_parse_license_files

#
# Generate a "license_files" line for packages without it.
# This is done searching for file names typically used for licenses.
#
# out:
#
# $pkg_license_files = new "license_files" line if empty before
#
ptxd_make_world_license_expand() {
    local -a licenses

    [ -n "${pkg_license_files}" ] && return

    exec {fd}< <(md5sum "${pkg_dir}/COPYING"* "${pkg_dir}/LICENSE"* 2>/dev/null)
    while read md5 file <&${fd}; do
	file="${file#${pkg_dir}/}"
	licenses[${#licenses[@]}]="file://${file};md5=${md5}"
	pkg_license_files="${pkg_license_files} file://${file};md5=${md5};guess=yes"
    done
    exec {fd}<&-
}
export -f ptxd_make_world_license_expand

#
# escape string for latex
#
ptxd_make_latex_escape() {
    local s="${1}"
    s="${s//_/\\_}"
    s="${s//&/\\&}"
    echo "${s}"
}
export -f ptxd_make_latex_escape

#
# generate a latex chapter for the package
#
# in:
#
# $pkg_license = the license string
# $pkg_license_texts/$pkg_license_texts_guessed = license text files
#
ptxd_make_world_license_write() {
    local guess
    local pkg_chapter="$(ptxd_make_latex_escape ${pkg_label})"
    pkg_chapter="${pkg_chapter#host-}"
    pkg_chapter="${pkg_chapter#cross-}"

    case "${pkg_license}" in
	*proprietary*)
	    pkg_chapter="${pkg_chapter} *** Proprietary License!"
	    ;;
	*unknown*)
	    pkg_chapter="${pkg_chapter} *** Unknown License!"
	    ;;
	*ignore*)
	# ignore this package, e.g. do not list it in the report
	    return 0
	    ;;
    esac

    cat <<- EOF
		\chapter{${pkg_chapter}\label{${pkg_label}}}

		\begin{description}
		\item[Package:] $(ptxd_make_latex_escape ${pkg_label}) $(ptxd_make_latex_escape ${pkg_version})
		\item[License:] $(ptxd_make_latex_escape ${pkg_license})
		\item[URL:] \begin{flushleft}$(ptxd_make_latex_escape ${pkg_url})\end{flushleft}
		\item[MD5:] {\ttfamily ${pkg_md5}}
		\end{description}
	EOF

    if [ -n "${pkg_dot}" ]; then
	cat <<- EOF
		\begin{figure}[!ht]
		\centering
		\hspace*{-0.5in}\maxsizebox{0.9\paperwidth}{!}{
		\input{${pkg_tex}}}
		\caption{Dependency tree for $(ptxd_make_latex_escape ${pkg_label})}
		\label{${pkg_label}-deps}
		\end{figure}
	EOF
    fi

    for license in "${pkg_license_texts[@]}" - "${pkg_license_texts_guessed[@]}"; do
	if [ "${license}" = "-" ]; then
	    guess=" [automatically found]"
	    continue
	fi
	title="$(basename "${license}")"
	cat <<- EOF
		\section{$(ptxd_make_latex_escape ${title})${guess}}
		\begin{lstlisting}
	EOF
	cat "${license}" | sed -e 's/\f/\n/g'
	cat <<- EOF
		\end{lstlisting}
	EOF
    done
}
export -f ptxd_make_world_license_write

#
# extract and process all available license information
#
ptxd_make_world_license() {
    ptxd_make_world_init || return

    local arg
    local -a ptxd_reply
    local -a pkg_license_texts
    local -a pkg_license_texts_guessed
    local pkg_dot=${pkg_license_dir}/graph.dot
    local pkg_tex=${pkg_license_dir}/graph.tex
    pkg_license="${pkg_license:-unknown}"

    rm -rf "${pkg_license_dir}" &&
    mkdir -p "${pkg_license_dir}" &&

    ptxd_make_world_license_expand

    for arg in ${pkg_license_files}; do
	local file startline endline md5 guess

	ptxd_make_world_parse_license_files "${arg}" &&

	local lic="${pkg_license_dir}/${file//\//_}" &&
	sed -n "${startline},${endline}p" "${pkg_dir}/${file}" > "${lic}" &&

	if ! echo "${md5}  ${lic}" | md5sum --check > /dev/null 2>&1; then
	    ptxd_bailout "

checksum of license file '$(ptxd_print_path "${file}")'
changed: ${md5} -> $(md5sum "${lic}" | sed 's/ .*//')
"
	fi &&
	if [ -z "${guess}" ]; then
	    pkg_license_texts[${#pkg_license_texts[@]}]="${lic}"
	else
	    pkg_license_texts_guessed[${#pkg_license_texts_guessed[@]}]="${lic}"
	fi
    done &&

    ptxd_make_world_license_write | \
        sed -e 's/%/\\%/g' > "${pkg_license_dir}/license-report.tex" &&

    echo "${pkg_license}" > "${pkg_license_dir}/license-name"
}
export -f ptxd_make_world_license