summaryrefslogtreecommitdiff
path: root/build-aux/enlicense
blob: 79b98db11a82e6f0e63bf5408cfaa030fb4a2571 (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
#!/bin/bash

TOP="${0%/*}/.."
LICENSE="$TOP/LICENSE-BSD"
EXCLUDE=""

show_usage () {
    echo "usage: $0 [options]"
    echo "The possible options are:"
    echo "  --dry-run|-n         Just find files lacking license info."
    echo "  --license|-L <file>  Use file to obtain license text."
    echo "  --git|-g             Add license only to files in the repository."
    echo "  --exclude|-e <pat>   Exclude files matching egrep pattern <pat>."
    echo "  --help|-h            Show this help and exit."
}

fatal () {
    local _err _msg

    _err="$1"
    shift
    _msg="$*"

    echo "fatal error: $_msg"
    exit $_err
}

enlicense () {
    local _file _in _out

    case $1 in
        *-func-info.c)
            return 0
            ;;
    esac

    _file="$1"
    _in="$1.no-license"
    _out="$1.license"

    cp $_file $_in
    echo "Inserting licensing information to $_file..."
    echo "/*"                                    > $_out
    cat $LICENSE | sed 's/^    /  /g;s/^/ * /g' \
                 | sed 's/ *$//g'               >> $_out
    echo " */"                                  >> $_out
    echo ""                                     >> $_out
    cat $_in                                    >> $_out
    cp $_out $_file
}

find_missing_licenses () {
    local _lacking _files _f

    _lacking=""
    if [ -z "$EXCLUDE" ]; then
        _files="`find . -name '*.[hc]'`"
    else
        _files="`find . -name '*.[hc]' | egrep -v -e $EXCLUDE`"
    fi

    for _f in $_files; do
        _f="${_f#./}"
        grep -ql 'Copyright .*Intel .*' $_f
        if [ $? != 0 ]; then
            if [ "$GIT" = "y" ]; then
                git ls-files | grep -q "$_f\$" && _lacking="$_lacking $_f" || :
            else
                _lacking="$_lacking $_f"
            fi
        fi
    done

    echo "$_lacking"
}


DRY_RUN=""
GIT=""

while [ "${1#-}" != "$1" -a -n "$1" ]; do
    case $1 in
        --dry-run|-n)
            DRY_RUN="y"
            ;;
        --license|-L)
            if [ -n "$2" ]; then
                shift
                LICENSE="$1"
            else
                fatal 1 "missing license argument"
            fi
            ;;
        --git|-g)
            GIT="y"
            ;;
        --exclude|-e)
            if [ -n "$2" ]; then
                shift
                EXCLUDE="$1"
            else
                fatal 1 "missing exclusion pattern"
            fi
            ;;
        --help|-h)
            show_usage
            exit 0
            ;;
         *)
            echo "Unknown command line option \'$1\'."
            show_usage
            exit 1
            ;;
    esac
    shift
done

if [ ! -f "$LICENSE" ]; then
    fatal 1 "license file \'$LICENSE\' missing"
fi

pushd $TOP >& /dev/null

lacking="`find_missing_licenses`"

for f in $lacking; do
    if [ "$DRY_RUN" != "y" ]; then
        enlicense $f
    else
        echo "$f is lacking licensing information."
    fi
done