aboutsummaryrefslogtreecommitdiffstats
path: root/third-party/License/add_license.sh
blob: 9b383c6248218b8fdd8a44b90e77a4e201292c27 (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
#!/bin/bash
# Copyright justin.chigang@gmail.com
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

TMP_FILE=tmp.file
LICENSE_TOKEN="http://www.apache.org/licenses/LICENSE-2.0"

function get_first_author()
{
    author=$(git log --reverse --pretty=format:"%ae" $1 | head -1)
    substring=$(git log --reverse --pretty=format:"%ae" $1 | head -1 | awk -F@ '{print $2}')
    case $substring in
     huawei*)
       echo "HUAWEI TECHNOLOGIES CO.,LTD"
          ;;
     orange*)
       echo "Orange"
          ;;
     zte*)
       echo "ZTE Corporation"
          ;;
     *)
       echo "$author"
          ;;
     esac
}

function get_latest_year()
{
    git log --pretty=format:"%ad" $1 | head -1 | awk '{print $5}'
}

function gen_c_license()
{
cat << EOF >$1
/******************************************************************************* 
 * Copyright (c) $2 $3 and others. 
 *
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Apache License, Version 2.0 
 * which accompanies this distribution, and is available at 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *******************************************************************************/ 

EOF
}

function gen_xml_license()
{
cat << EOF >$1
<!--
 Copyright (c) $2 $3 and others.

 All rights reserved. This program and the accompanying materials
 are made available under the terms of the Apache License, Version 2.0
 which accompanies this distribution, and is available at
 http://www.apache.org/licenses/LICENSE-2.0
-->

EOF
}

function gen_bash_license()
{
cat << EOF >$1
##############################################################################
# Copyright (c) $2 $3 and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################

EOF
}

function add_c_license()
{
    C_LICENSE="c_license_header.tmp"
    author=`get_first_author "$1"`
    year=`get_latest_year "$1"`
    gen_c_license $C_LICENSE $year "$author"
    cat $C_LICENSE $1 > $TMP_FILE
    rm -f $C_LICENSE
    mv $TMP_FILE $1
}

function add_xml_license()
{
    XML_LICENSE="xml_license_header.tmp"
    author=`get_first_author "$1"`
    year=`get_latest_year "$1"`
    gen_xml_license $XML_LICENSE $year "$author"
    cat $XML_LICENSE $1 > $TMP_FILE
    rm -f $XML_LICENSE
    mv $TMP_FILE $1
}

function add_bash_license()
{
    BASH_LICENSE="bash_license_header.tmp"
    author=`get_first_author "$1"`
    year=`get_latest_year "$1"`
    gen_bash_license $BASH_LICENSE $year "$author"
    cat $1 | head -1 | grep "#!" > /dev/null
    if [ $? -eq 0 ]; then
        #insert 2
        sed -i "1 r $BASH_LICENSE" $1
    else
        #sed -i "1 R $BASH_LICENSE" $1
        cat $BASH_LICENSE $1 > $TMP_FILE
        mv $TMP_FILE $1
    fi
    rm -f $BASH_LICENSE
}

if [[ -z "$1" ]] || [[ ! -d "$1" ]]; then  
    echo "The directory is empty or not exist!"  
    echo "It will use the current directory."  
    nowdir=$(pwd)  
else  
    nowdir=$(cd $1; pwd)  
fi  
echo "$nowdir"  

n=0

function Searchfile()  
{  
    cd $1  
    
    dirlist=$(ls)  
    for dirname in $dirlist
    do  
        if [[ -d "$dirname" ]];then
            n=$((n+4))
            cd $dirname
            for i in $( seq 0 $n );do echo -n ' ';done;echo "$dirname ..."  
            Searchfile $(pwd)  
            cd ..  
            n=$((n-4))
        fi;

        filename=$dirname
        if [[ -f "$filename" ]]; then
            for i in $( seq 0 $n );do echo -n ' ';done;echo " |--$filename"
            grep -rn $LICENSE_TOKEN $filename >/dev/null
            if [ $? -eq 1 ]; then
                if [ "${filename##*.}" = "c" -o "${filename##*.}" = "cpp" -o "${filename##*.}" = "java" ]; then
                    add_c_license $filename 
                    for i in $( seq 0 $n );do echo -n ' ';done;echo " |--add license for $filename... "
                elif [ "${filename##*.}" = "py" -o "${filename##*.}" = "yml" -o "${filename##*.}" = "yaml" -o "${filename##*.}" = "sh" ]; then
                    add_bash_license $filename 
                    for i in $( seq 0 $n );do echo -n ' ';done;echo " |--add license for $filename... "
                elif [ "${filename##*.}" = "xml" ]; then
                    add_xml_license $filename
                    for i in $( seq 0 $n );do echo -n ' ';done;echo " |--add license for $filename... "
                fi;
            fi;
        fi;
    done;  
}  
  
Searchfile $nowdir 

# Revert changes of skipped files, e.g. __init__.py

git checkout \*\*/__init__.py