• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3# Author: Dodji Seketeli <dodji@redhat.com>
4
5file=
6prog=$0
7display_file_name=
8be_quiet=
9show_no_spdx=
10
11display_usage()
12{
13    echo "$prog: [options] file"
14    echo "  where options can be:"
15    echo "   -h|--help		display this help"
16    echo "   -f|--file		prefix output with file name"
17    echo "   -q|--quiet		emit no output if no license was found"
18    echo "   --show-no-spdx	show file name if it has no spdx header"
19}
20
21emit_output_no_license()
22{
23    if test x$show_no_spdx != x; then
24	echo $display_file_name
25    elif test x$be_quiet = x; then
26	if test "x$display_file_name" = x; then
27	    echo "NO-LICENSE"
28	else
29	    echo "$display_file_name: NO-LICENSE"
30	fi
31    fi
32
33    exit 1
34}
35
36emit_output_with_license()
37{
38    license=$1
39    if test x$show_no_spdx != x; then
40	:
41    elif test "x$display_file_name" = x; then
42	echo "$license"
43    else
44	echo "$display_file_name: $license"
45    fi
46    exit 0
47}
48
49main()
50{
51    license=$(head --lines=5 $file | sed -n -E "s/^.*(SPDX-License-Identifier:)[ 	]*([^*/]+).*$/\2/p")
52
53    if test "x$license" = x; then
54	emit_output_no_license
55    else
56	emit_output_with_license "$license"
57    fi
58}
59
60while test $# -ge 1
61do
62    case "$1" in
63	-h|--help)
64	    display_usage
65	    exit 0
66	    ;;
67
68	-f|--file)
69	    if test x$2 = x; then
70		>&2 display_usage
71		exit 1
72	    fi
73	    display_file_name=$2
74	    shift
75	    ;;
76
77	-q|--quiet)
78	    be_quiet=yes
79	    shift
80	    ;;
81
82	--show-no-spdx)
83	    if test x$2 = x; then
84		>&2 display_usage
85		exit 1
86	    fi
87	    show_no_spdx=yes
88	    display_file_name=$2
89	    shift
90	    ;;
91
92	*)
93	    break
94	    ;;
95    esac
96done
97
98if test $# -lt 1; then
99    >&2 display_usage
100    exit 1
101fi
102
103file=$1
104
105main
106