A few months back, on the TODO list for LibreJS, it was suggested
       that we create a patch for websites to ease the freeing of their
       javascript.  I've got the start of a bash script that would free
       an entire directory tree of javascript.  I'm not sure if that's
       what the idea was, but for the moment, this will insert the
       @licstart/@licend markers for the MPL and Apache licenses, or
       insert the users choice of AGPL, MPL, or Apache licenses if no
       license is found.  I think this might help while we work out
       other options.

Ian D

#! /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. */'

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

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 "$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 \(*#\) License, v\. 2\.0\. If a copy of the MPL was not distributed with this\n \(*#\) 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
    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