Loic (and everyone else),

        I found a small bug in the script, part of the MPL license
        regexp.  I fixed it, and also added the public domain license
        declaration to the regexps, and the insert licenses.  I figured
        I should push the update before too long.

Ian D

[email protected] (Loic J. Duros) writes:

> Hi Ian:
>
> Nice job!
>
> I'll test this when I get a chance.
> This sounds like a a useful tool to provide webmasters!
>
> Loic
>
#! /bin/bash

# License declarations.

AGPL='/* @licstart  The following is the entire license notice for the
 * JavaScript code in this page.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this page.'

Apache='/* @licstart  The following is the entire license notice for the
 * JavaScript code in this page.
 *
 * 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.
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this page. */'

MPL='/* @licstart  The following is the entire license notice for the
 * JavaScript code in this page.
 * 
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 * 
 * @licend  The above is the entire license notice
 * for the JavaScript code in this page. */'

Public='/* @licstart  The following is the entire license notice for the
 * JavaScript code in this page.
 *
 * Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/
 *
 * @licend  The above is the entire license notice
 * for the JavaScript code in this page. */'

LICENSES=("$AGPL" "$MPL" "$Apache" "$Public")

FILES=

# License defaults to the AGPL
LICENSE=0

while :
do
    case "$1" in
	-l) LICENSE="$2" ;;
	-f) FILES="$2" ;;
	*) break ;;
    esac
    shift; shift
done

if [ ! -z "$1" ]
then
    echo "Usage: $(basename $0) [-f file] [-l license-number]"
    echo "license numbers are 0 - AGPL, 1 - MPL, 2 - Apache, 3 - Public Domain"
    exit 88
fi

if [ -z "$FILES" ]
then
    # Run this on every javascript file in this directory,
    #  and all subdirectories.
    FILES=$(find . -regex ".*\.js")
fi

if [ "$LICENSE" == "-1" ]
then
    LICENSE=0
fi

# First, check for a license.
# If one, then replace the license with the identical license, but
#    with licstart/licend.
# If none is found, then insert the user's request for the license.

for i in $FILES
do
    if [ ! -e "$i" ]
    then
	echo "Skipping non-existent file $i"
	continue
    fi

    echo "Checking $i for a license..."

    GREPS=$(grep -l '@licstart' "$i")
    GREPE=$(grep -l '@licend' "$i")
    if [ ! -z "$GREPS" ] && [ ! -z "$GREPE" ]
    then
	echo "Found a license in $i, continuing."
	continue
    else
	if [ ! -z "$GREPS" ] || [ ! -z "$GREPE" ]
	then
	    echo "Found license fragment in $i, skipping."
	    continue
	fi
    fi

    sed -sn '
1h
1!H
$ {
    g
    s|This Source Code Form is subject to the terms of the Mozilla Public\n\([ \t*#]*\) License, v\. 2\.0\. If a copy of the MPL was not distributed with this\n\([ \t*#]*\) file, You can obtain one at http://mozilla\.org/MPL/2\.0/\.|@licstart  The following is the entire license notice for the\n\1 JavaScript code in this page.\n\1\n\1 This Source Code Form is subject to the terms of the Mozilla Public\n\1 License, v. 2.0. If a copy of the MPL was not distributed with this\n\1 file, You can obtain one at http://mozilla.org/MPL/2.0/.\n\1\n\1 @licend  The above is the entire license notice\n\1 for the JavaScript code in this page.|g
    s|/\*\([^/]*\)Licensed under the Apache License, Version 2\.0 (the "License");\n \* you may not use this file except in compliance with the License\.\n \* You may obtain a copy of the License at\([ \t\n*]*\)http://www\.apache\.org/licenses/LICENSE-2\.0\([^/]*\)\/|/* @licstart  The following is the entire license notice for the\n * JavaScript code in this page.\n *\n *\1Licensed under the Apache License, Version 2.0 (the "License");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\2http://www.apache.org/licenses/LICENSE-2.0\3\n *\n * @licend  The above is the entire license notice\n * for the JavaScript code in this page.*/|g
    s|Any copyright is dedicated to the Public Domain\.\n \* http://creativecommons\.org/publicdomain/zero/1\.0/|@licstart  The following is the entire license notice for the\n * JavaScript code in this page.\n *\n * Any copyright is dedicated to the Public Domain.\n * http://creativecommons.org/publicdomain/zero/1.0/\n *\n * @licend  The above is the entire license notice\n * for the JavaScript code in this page.|g
    p
}
' "$i" > "$i".tmp

    # If anything changed, then this won't be empty.
    DIFFS=$(diff -q "$i" "$i.tmp")
    if [ -z "$DIFFS" ]
    then
	rm "$i.tmp"
	if [ "$LICENSE" == "-1" ]
	then
	    echo "No License found in $i, and none specified."
	    continue
	fi

	# No license was found, insert it at the top of the file.
	DES_LIC="${LICENSES[$LICENSE]}"
	echo "$DES_LIC" | cat - "$i" > "$i.tmp"
    fi
    mv -f "$i.tmp" "$i"
done
echo "Finished."

Reply via email to