copyright.pl


# This work is licensed under the Creative Commons
# Attribution-ShareAlike 3.0 Unported License. To view a copy of this
# license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send
# a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042,
# USA. 
# Reference: http://www.lombardinetworks.net
# (C) Robert Tolksdorf, http://www.robert-tolksdorf.de
use Switch;
require "/vhome/lombardinetworks.net/htdocs/bin/helper.pl";

# Licence texts
$licence_text="/vhome/lombardinetworks.net/htdocs/include/cc-by-sa-nc-4.0."; # without ending
# for all filenames passed
foreach $filename (@ARGV) {
    # extract ending
    $filename=~/\.(.*)$/;
    $ending=$1;
    # is it a file we can act upon?
    if ($ending =~ /(pl|xml|xgmml|graphml|html|xslt|json|js)$/) {
	switch ($ending) {
	    case /pl|js/ {
		# use .pl or .js licence
		$licence_file=$licence_text.$ending;
	    }
	    case /xml|xgmml|graphml|html|xslt/ {
		# use .xml licence
		$licence_file=$licence_text."xml";
	    }
	}
	# get licence
	$licence=&read_file($licence_file);
	$file=&read_file($filename);
	# test whether licence is already included
	$already_licenced=0;
	switch ($ending) {
	    case /pl|js|xml|xgmml|graphml|html|xslt/ {
		# does it start with the licence? TODO change for XML formats, it does work as it is, also for perl
		if ($file =~/\Q$licence\E/) {
		    $already_licenced=1;
		}
	    }
	}
	# emit licence if not already contained
	if (!$already_licenced) {
	    # backup the original
	    rename $filename, $filename."~";
	    # emit to a new file
	    switch ($ending) {
		case /html|js/ {
		    # just prepend the licence
		    &write_file($filename,$licence.$file);
		}
# TODO: change for executable perls in which the first line is like #!/usr/bin/perl and has to stay the first line.
		case /pl/ {
		    # does it start with a shebang?
		    if ($file=~/\#!/) {
			# get first line (the #! )
			$file=~/(.*?)\n/;
			# append license
			$l=$1."\n".$licence;
			# get and append the remaining file
			$file=~/.*?\n(.*)$/s;
			$l.=$1;
			# write it
			&write_file($filename,$l);
		    } else {
			# no shebang, just prepend the licence
			&write_file($filename,$licence.$file);
		    }
		}
		case /xml|xgmml|graphml|xslt/ {
		    # licence has to come after first line
		    # get first line (the <?xml which always has to be the first line )
		    $file=~/(.*?)\n/;
		    # append license
		    $l=$1."\n".$licence;
		    # get and append the remaining file
		    $file=~/.*?\n(.*)$/s;
		    $l.=$1;
		    # write it
		    &write_file($filename,$l);
		}
    
	    }
	}
    }
}