#!/bin/sh

# Copyright (C) 2012, Benjamin Drung <bdrung@debian.org>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

if test "$1" = --installed; then
    COMMAND="licensecheck --no-conf"
    shift
else
    COMMAND="../scripts/licensecheck.pl --no-conf"
fi

WORKDIR="$(readlink -f "${0%/*}")"

. "${0%/*}/shunit2-helper-functions.sh"

license() {
    cd "$WORKDIR"
    runCommand "licensecheck/$1" "licensecheck/$1: $2" "" 0
}

license2() {
    cd "$WORKDIR"
    runCommand "$1 licensecheck/$2" "licensecheck/$2	$3" "" 0
}

testBeerware() {
    license "beerware.cpp" "Beerware"
}

testBoost() {
    license "boost.h" "BSL"
}

testAcademic() {
    license "academic.h" "AFL-3.0"
}

testEclipse() {
    license2 "-m --copyright" "epl.h" "EPL-1.0	2004, 2006 IBM Corporation and others"
}

testBSD() {
    license "bsd-1-clause-1.c" "BSD"
    license "bsd-3-clause.cpp" "BSD (3 clause)"
    license "bsd-3-clause-authorsany.c" "BSD (3 clause)"
}

testMultiLineDeclaration() {
    # test copyright declared on 2 lines
    license2 "-m --copyright" "bsd-regents.c" "BSD (3 clause)	1987, 1993, 1994 The Regents of the University of California. All rights reserved."
    # or 3 lines
    license2 "-m --copyright" "texinfo.tex" "GPL (v3 or later)	1985, 1986, 1988, 1990, 1991, 1992, 1993, 1994, 1995 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc."
}

testDuplicatedCopyright() {
    license2 "-m --copyright" "duplicated-copyright.rb" "Apache (v2.0)	2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs"
}

testLGPL() {
    license "lgpl-2.1.c" "LGPL (v2.1)"
    license "lgpl-2.1.h" "LGPL (v2.1)"
    license "lgpl-2.s" "LGPL (v2)"
    license "lgpl-2-3.c" "LGPL (v2 or v3)"
    license "lgpl-3+.h" "LGPL (v3 or later)"
    license "lgpl-variation.c" "LGPL (v3 or later)"
    license "lgpl-no-version.h" "LGPL (unversioned/unknown version)"
    license2  "-m --copyright" "lgpl-big-blue.h" "LGPL (v2.1)	2000, 2010 IBM Corporation and others. All rights reserved"
    license2  "-m --copyright" "lgpl-digia.c" "LGPL (v2.1 or v3)	2014 Digia Plc and/or its subsidiary(-ies)"
}

testGPL() {
    license "gpl-1" "GPL (v1 or later)"
    license "gpl-2" "GPL (v2)"
    license "gpl-2+" "GPL (v2 or later)"
    license "gpl-2+.scm" "GPL (v2 or later)"
    license "gpl-2-comma.sh" "GPL (v2)"
    license "gpl-2-incorrect-address" "GPL (v2) (with incorrect FSF address)"
    license "gpl-3.sh" "GPL (v3)"
    license "gpl-3+" "GPL (v3 or later)"
    license "gpl-3+-with-rem-comment.xml" "GPL (v3 or later)"
    license "gpl-3-only.c" "GPL (v3)"
    license "gpl-variation.c" "GPL (v3 or later)"
    license "gpl-3+.el" "GPL (v3 or later)"
    license "gpl-no-version.h" "GPL (unversioned/unknown version)"
}

testDual() {
    license "dual.c" "Public domain GPL (v3)"
}

testMPL() {
    license "mpl-1.1.sh" "MPL (v1.1)"
    license "mpl-2.0.sh" "MPL (v2.0)"
    license "mpl-2.0-comma.sh" "MPL (v2.0)"
}

testCDDL() {
    license2 "-m --copyright" "cddl.h" "CDDL	2006 Sun Microsystems, Inc. All rights reserved"
}

testMachine() {
    license2 "-m" "beerware.cpp" "Beerware"
    license2 "--machine" "lgpl-2.1.h" "LGPL (v2.1)"
    license2 "-m --copyright" "gpl-2" "GPL (v2)	2012 Devscripts developers"
}

testFortranComments() {
    license "bsd.f" "BSD (2 clause)"
}

testCommentsDetection() {
    license "comments-detection.h" "GPL (v3 or later)"
    license "comments-detection.txt" "*No copyright* LGPL (v2.1 or later)"
}

testFalsePositives() {
    license2 "-m --copyright" "false-positives" "Public domain	2013 Devscripts developers"
}

testRegexpKiller() {
    license "regexp-killer.c" "UNKNOWN"
}

testEncoding () {
    license2 "-m --copyright" "copr-iso8859.h" "GPL (v2) (with incorrect FSF address)	2011 Heinrich Müller <henmull@src.gnome.org>"
    license2 "-m --copyright" "copr-utf8.h" "GPL (v2 or later)	2004-2015 Oliva 'f00' Oberto / 2001-2010 Paul 'bar' Stevénsön"
}
. shunit2
