#!/bin/bash
#
# This script prepends predefined licence headers into java source files. Existing
# headers are recognized by the start of java block comments (/*) at the beggining
# of files and will be replaced.
#
# The path to a license file to store the full license text can be defined with
# LICENSE_FILE (no license file will be created if empty).
#
# The script will initially attempt to remove existing license files specified by
# LICENSE_FILES_OLD (no files will be removed if empty).
#
# USAGE:
#  licensify <license> <title> <author> <files>...
#
# EXAMPLE: 
#  licensify lgpl3 "Example Mod" "Joe Doe" $(find . -name *.java)

# ARR (all rights reserverd)
ARR_HEADER="/*
 * This file is part of <title>.
 * Copyright (c) $(date +%Y), <author>, All rights reserved.
 */"

ARR_FULL="Copyright (c) $(date +%Y), <author>, All rights reserved."

# MIT LICENSE (TODO)
MIT_FULL=''
MIT_HEADER=''

# GNU LESSER GENERAL PUBLIC LICENSE
LGPL3_FULL="# GNU LESSER GENERAL PUBLIC LICENSE

Version 3, 29 June 2007

Copyright (C) 2007 Free Software Foundation, Inc.
<https://fsf.org/>

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.

This version of the GNU Lesser General Public License incorporates the
terms and conditions of version 3 of the GNU General Public License,
supplemented by the additional permissions listed below.

## 0. Additional Definitions

As used herein, \"this License\" refers to version 3 of the GNU Lesser
General Public License, and the \"GNU GPL\" refers to version 3 of the
GNU General Public License.

\"The Library\" refers to a covered work governed by this License, other
than an Application or a Combined Work as defined below.

An \"Application\" is any work that makes use of an interface provided
by the Library, but which is not otherwise based on the Library.
Defining a subclass of a class defined by the Library is deemed a mode
of using an interface provided by the Library.

A \"Combined Work\" is a work produced by combining or linking an
Application with the Library. The particular version of the Library
with which the Combined Work was made is also called the \"Linked
Version\".

The \"Minimal Corresponding Source\" for a Combined Work means the
Corresponding Source for the Combined Work, excluding any source code
for portions of the Combined Work that, considered in isolation, are
based on the Application, and not on the Linked Version.

The \"Corresponding Application Code\" for a Combined Work means the
object code and/or source code for the Application, including any data
and utility programs needed for reproducing the Combined Work from the
Application, but excluding the System Libraries of the Combined Work.

## 1. Exception to Section 3 of the GNU GPL

You may convey a covered work under sections 3 and 4 of this License
without being bound by section 3 of the GNU GPL.

## 2. Conveying Modified Versions

If you modify a copy of the Library, and, in your modifications, a
facility refers to a function or data to be supplied by an Application
that uses the facility (other than as an argument passed when the
facility is invoked), then you may convey a copy of the modified
version:

- a) under this License, provided that you make a good faith effort
    to ensure that, in the event an Application does not supply the
    function or data, the facility still operates, and performs
    whatever part of its purpose remains meaningful, or
- b) under the GNU GPL, with none of the additional permissions of
    this License applicable to that copy.

## 3. Object Code Incorporating Material from Library Header Files

The object code form of an Application may incorporate material from a
header file that is part of the Library. You may convey such object
code under terms of your choice, provided that, if the incorporated
material is not limited to numerical parameters, data structure
layouts and accessors, or small macros, inline functions and templates
(ten or fewer lines in length), you do both of the following:

- a) Give prominent notice with each copy of the object code that
    the Library is used in it and that the Library and its use are
    covered by this License.
- b) Accompany the object code with a copy of the GNU GPL and this
    license document.

## 4. Combined Works

You may convey a Combined Work under terms of your choice that, taken
together, effectively do not restrict modification of the portions of
the Library contained in the Combined Work and reverse engineering for
debugging such modifications, if you also do each of the following:

- a) Give prominent notice with each copy of the Combined Work that
    the Library is used in it and that the Library and its use are
    covered by this License.
- b) Accompany the Combined Work with a copy of the GNU GPL and this
    license document.
- c) For a Combined Work that displays copyright notices during
    execution, include the copyright notice for the Library among
    these notices, as well as a reference directing the user to the
    copies of the GNU GPL and this license document.
- d) Do one of the following:
  - 0) Convey the Minimal Corresponding Source under the terms of
        this License, and the Corresponding Application Code in a form
        suitable for, and under terms that permit, the user to
        recombine or relink the Application with a modified version of
        the Linked Version to produce a modified Combined Work, in the
        manner specified by section 6 of the GNU GPL for conveying
        Corresponding Source.
  - 1) Use a suitable shared library mechanism for linking with
        the Library. A suitable mechanism is one that (a) uses at run
        time a copy of the Library already present on the user's
        computer system, and (b) will operate properly with a modified
        version of the Library that is interface-compatible with the
        Linked Version.
- e) Provide Installation Information, but only if you would
    otherwise be required to provide such information under section 6
    of the GNU GPL, and only to the extent that such information is
    necessary to install and execute a modified version of the
    Combined Work produced by recombining or relinking the Application
    with a modified version of the Linked Version. (If you use option
    4d0, the Installation Information must accompany the Minimal
    Corresponding Source and Corresponding Application Code. If you
    use option 4d1, you must provide the Installation Information in
    the manner specified by section 6 of the GNU GPL for conveying
    Corresponding Source.)

## 5. Combined Libraries

You may place library facilities that are a work based on the Library
side by side in a single library together with other library
facilities that are not Applications and are not covered by this
License, and convey such a combined library under terms of your
choice, if you do both of the following:

- a) Accompany the combined library with a copy of the same work
    based on the Library, uncombined with any other library
    facilities, conveyed under the terms of this License.
- b) Give prominent notice with the combined library that part of it
    is a work based on the Library, and explaining where to find the
    accompanying uncombined form of the same work.

## 6. Revised Versions of the GNU Lesser General Public License

The Free Software Foundation may publish revised and/or new versions
of the GNU Lesser General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.

Each version is given a distinguishing version number. If the Library
as you received it specifies that a certain numbered version of the
GNU Lesser General Public License \"or any later version\" applies to
it, you have the option of following the terms and conditions either
of that published version or of any later version published by the
Free Software Foundation. If the Library as you received it does not
specify a version number of the GNU Lesser General Public License, you
may choose any version of the GNU Lesser General Public License ever
published by the Free Software Foundation.

If the Library as you received it specifies that a proxy can decide
whether future versions of the GNU Lesser General Public License shall
apply, that proxy's public statement of acceptance of any version is
permanent authorization for you to choose that version for the
Library."

LGPL3_HEADER="/*
 * This file is part of <title>.
 * Copyright (c) $(date +%Y), <author>, All rights reserved.
 *
 * <title> is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * <title> 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 Lesser General Public License for
 * more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along
 * with <title>. If not, see <http://www.gnu.org/licenses/lgpl>.
 */"

# MOD COMMUNITY LICENSE (https://gitlab.com/srcmc/mcoml/-/blob/main/v1/license.txt)
MCOML_FULL="                            MOD COMMUNITY LICENSE
                           Version 1, 21 March 2026

Copyright (c) $(date +%Y) <author>

0. Foreword

The Mod Community License (MCOML) is an open license meant to better protect
the rights of software authors. It does not adhere to the usual definition of
\"Free Open Source Software\" (FOSS), but rather it aims to strike a middle
ground between the freedom of consumers and the protection of the authors' work.

In short, you may share or modify the source code as you wish, as long as the
work stays under this license, and if you have modified the code, you must:

  (a) state your changes if you don't use an SVC like Git
  (b) list the original authors (as listed in the copyright at the top of
      this license)

When sharing binaries, you must adhere to a few more rules. Either:

  (a) your derivative work differs sufficiently from the original or
  (b) you have received written permission from all Project Owners

If you are running a modified version of this software on your servers and
users can freely interact with it, you will need to make your source code
public the same way.

The precise terms and conditions can be found below:

1. Definitions

  1.1. \"Source Code\": The Source Code is any digital files or tools that are
       part of the work covered by this text and/or used in the creation of
       Binaries, preferred for making modifications. This does not include
       any publicly available tools or libraries. This may include textual or
       binary digital files.

  1.2. \"Binaries\": Binaries correspond to any form of the work other than the
       Source Code.

  1.3. \"This License\": This License corresponds to the full text of this
       document.

  1.4. \"Copyright Notice\": The Copyright Notice corresponds to the line towards
       the top of This License, starting with \"Copyright (c)\".

  1.5. \"Project Owner\": The Project Owners are all physical persons or legal
       entities mentioned in the Copyright Notice. They may be referred to by
       an alias.

  1.6. \"You\": \"You\" corresponds to an individual or a legal entity exercising
       rights under this License. For legal entities, \"You\" includes any entity
       that controls, is controlled by, or is under common control with You. For
       purposes of this definition, control means

         (a) the power, direct or indirect, to cause the direction or management
             of such an entity, whether by contract or otherwise, or
         (b) ownership of more than fifty percent (50%) of the outstanding shares
             or beneficial ownership of such an entity.

2. Using the Source Code

You may copy, modify, merge, publish, distribute, and/or sell copies of the
Source Code under the following conditions:

  2.1. The Source Code and any derivative work of the Source Code are licensed
       under This License.

  2.2. You must explicitly state your changes, if any, in a prominent place.

    2.2.1. Commit history as provided by source version control software
           programs is considered to fulfill 2.2.

  2.3. If You modify the Source Code in any way, You must list the original
       Project Owners in a prominent place.

    2.3.1. Commit history as provided by source version control software
           programs is NOT considered to fulfill 2.3.

  2.4. You may add your name to the Copyright Notice, but You must not remove
       any name from the existing Copyright Notice.

3. Using Binaries

You may share, publish, distribute, make derivative work, and/or sell copies
of Binaries, or license the right to do so, under the following conditions:

  3.1. Any derivative work must differ sufficiently from the original. The
       following criteria must be satisfied:

    3.1.1. At least 50% of the derivative work must consist of code or
           functionality that significantly alters and/or adds to the original
           work in a meaningful way.

    3.1.2. Mere additions, such as empty classes, comments, or trivial changes
           that do not enhance functionality in a reasonable way, do not meet
           this criterion.

    3.1.3. An explicit written permission by all Project Owners can serve as a
           replacement for any conditions mentioned under 3.1.

  3.2. The Binaries and any derivative work of the Binaries are licensed under
       This License.

  3.3. You must provide documentation stating in particular any differences to
       the original work.

  3.4. You must make the Source Code used to create the Binaries publicly
       available, free of charge.

4. Remote network interaction

If You offer users the ability to interact remotely through a computer network
with a Binary created from a modification of the Source Code, You must make the
modified Source Code publicly available, free of charge. You must also clearly
document and respond to requests asking how to obtain the aforementioned Source
Code.

5. Disclaimer

THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE, ARISING FROM, OUT OF, OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

In case of a conflict between the Section 0 (\"Foreword\") and any other Section,
the latter Section shall prevail."

# https://gitlab.com/srcmc/mcoml/-/blob/main/v1/header.txt
MCOML_HEADER="/*
 * This file is part of <title>.
 * Copyright (c) $(date +%Y), <author>, All rights reserved.
 * 
 * <title> can be redistributed and/or modified under the terms of the
 * Mod Community License (MCOML).
 * 
 * In short, you may share or modify the source code as you wish, as long as the
 * work stays under this license, and if you have modified the code, you must:
 * 
 *   (a) state your changes if you don't use an SVC like Git
 *   (b) list the original authors (as listed in the copyright at the top of
 *       this license)
 * 
 * When sharing binaries, you must adhere to a few more rules. Either:
 * 
 *   (a) your derivative work differs sufficiently from the original or
 *   (b) you have received written permission from all Project Owners
 * 
 * <title> 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 MCOML for more details.
 * 
 * You should have received a copy of the MCOML along with <title>.
 * If not, see <https://gitlab.com/srcmc/mcoml/-/blob/main/v1/license.txt>.
 */"

# parse arguments
case "$1" in
    ARR|arr)
        full="$ARR_FULL"
        header="$ARR_HEADER" ;;
    MIT|mit)
        full="$MIT_FULL"
        header="$MIT_HEADER" ;;
    LGPL3|lgpl3)
        full="$LGPL3_FULL"
        header="$LGPL3_HEADER" ;;
    MCOML|mcoml)
        full="$MCOML_FULL"
        header="$MCOML_HEADER" ;;
    *)
        echo "error: invalid license '$1'"
        exit 1 ;;
esac

if [ -z "$2" ]; then
    echo "error: missing title"
    exit 1
fi

if [ -z "$3" ]; then
    echo "error: missing author"
    exit 1
fi

title="$2"
author="$3"
NL='
'

shift
shift
shift

# add license header to source files
while [ "$#" -gt 0 ]; do
    if [ -f "$1" ]; then
        line="$(head "$1" -n 1)"

        if [ "$line" == '/*' ]; then
            while IFS="" read -r p || [ -n "$p" ]; do
                if [ -z "$first" ]; then
                    first=1
                else
                    if [ -n "$accept" ]; then
                        content="${content:+$content$NL}$p"
                    else
                        if [ "${p:0:3}" == " */" ]; then
                            accept=1
                        elif [ "${p:0:2}" != " *" ]; then
                            content="$(cat "$1")"
                            break;
                        fi
                    fi
                fi
            done < "$1"
        else
            content="$(cat "$1")"
        fi

        if [ -n "$header" ]; then
            echo "$(echo "$header" | sed -e s/"<title>"/"$title"/g -e s/"<author>"/"$author"/g)" > "$1"
            echo "$content" >> "$1"
        else
            echo "$content" > "$1"
        fi

        unset first
        unset accept
        unset content
    else
        echo "warning: invalid file '$1' -> skipped"
    fi

    shift
done

# remove existing license files
if [ -n "$LICENSE_FILES_OLD" ]; then
    rm -f $LICENSE_FILES_OLD
fi

# create license file with full text
if [ -n "$LICENSE_FILE" ]; then
    echo "$(echo "$full" | sed -e s/"<title>"/"$title"/g -e s/"<author>"/"$author"/g)" > "$LICENSE_FILE"
fi