#!/usr/bin/perl -w
# J.J.Green 2008
# $Id: colocpt.pl,v 1.1 2025/01/04 13:42:10 jjg Exp $

use strict;

use POSIX;
use XML::Simple;
use LWP::Simple;

my $jcptdir  = "src/cpt/colo";
my $jhdrdir  = "cpt-city/colo";
my $jxmldir  = "var/colo";

my $cptdir = ".";
my $hdrdir = undef;
my $xmldir = ".";

my $usage = "
Usage: colocpt [options] <id>

Fetches the COLOURlovers XML specification of the palette
with the given <id> and writes a cpt file (GMT colour palette
table) into the file <cptdir>/<user>/<title>.cpt, the values
of <user> and <title> taken from the XML file.

where <id> is the numeric id of a COLOURlovers palette
and options can contain

  -c <cptdir> create cpt file <cptdir>/<user>/<title>.cpt (default \"$cptdir\")
  -j          equivalent to -c $jcptdir -H $jhdrdir -x $jxmldir
  -h          brief help
  -H <dir>    write header file in spcified <dir>
  -x <dir>    write xml file in spcified <dir> (default \"$xmldir\")

The -H and -j options are probably only of interest to the author.
";

use Getopt::Std;

my %opt = ();
getopts('c:hH:jx:',\%opt);

if ($opt{h})
{
    print $usage;
    exit EXIT_SUCCESS;
}

if ($opt{j})
{
    $cptdir  = $jcptdir;
    $hdrdir  = $jhdrdir;
    $xmldir  = $jxmldir;
}

if ($opt{c})
{
    $cptdir = $opt{c};
}

if ($opt{H})
{
    $hdrdir = $opt{c};
}

if ($opt{x})
{
    $xmldir = $opt{c};
}

my ($id) = @ARGV or die "need a colorlovers id : $usage\n";

print "This is colocpt\n";
print "processing $id\n";

my $clxfile = "$xmldir/$id.xml";
my $clxopts = "showPaletteWidths=1";
my $clxurl  = "http://www.colourlovers.com/api/palette/$id\?$clxopts";

unless (-e $clxfile)
{
    printf "fetching $clxfile\n";
    getstore($clxurl,$clxfile);
}

# read xml data

my $conf = XMLin($clxfile);

my $hexs   = $conf->{palette}->{colors}->{hex};
my $user   = $conf->{palette}->{userName};
my $title  = $conf->{palette}->{title};
my @widths = split /,/, $conf->{palette}->{colorWidths};

# remove trailing utf8 and whitespace from title

$title =~ s/[^[:ascii:]]+$//g;
$title =~ s/ +$//g;

# remove trailing whitespace from title

$user =~ s/ +$//g;

# generate user url

my $uurl = "http://www.colourlovers.com/lover/$user/";

sub sanitise
{
    my $str = shift;

    $str =~ s/^ +//g;
    $str =~ s/ +$//g;
    $str =~ s/ +/_/g;
    $str =~ s/\W//g;
    $str =~ s/__/_/g;
    $str =~ s/_$//g;
    $str =~ s/^_//g;

    return $str;
}

my $santitle = sanitise($title);
my $sanuser  = sanitise($user);

# header file, just for the author

if ($hdrdir)
{
    my $hdruserdir = "$hdrdir/$sanuser";

    unless (-e $hdruserdir)
    {
	mkdir $hdruserdir;
    }

    # header

    my $hdrfile = "$hdruserdir/header.txt";

    print "creating $hdrfile\n";

    open HRD,"> $hdrfile" or die "opening $hdrfile";

    print HRD <<EOF
<!-- autogenerated by colocpt -*- htmp -*- -->
<h2>$user</h2>

<p>
COLOURlover palettes from
<a href="$uurl">$user</a>.
</p>
EOF
;

    close HRD;

    # footer

    my $footfile = "$hdruserdir/footer.txt";

    print "creating $footfile\n";

    open FOOT,"> $footfile" or die "opening $footfile";

    print FOOT <<EOF
<!-- autogenerated by colocpt -->
<p>
Copyright is creative commons noncommercial: See
<a href="copying.html">details</a> and
<a href="COPYING.xml">XML record</a>.
</p>
EOF
;

    close FOOT;

    # copying

    my $copyfile = "$hdruserdir/COPYING.xml";

    print "creating $copyfile\n";

    open COPY,"> $copyfile" or die "opening $copyfile";

    print COPY <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<!-- autogenerated by colocpt -->
<copying>
  <authors>
    <author href="$uurl">
      <name>$user</name>
    </author>
  </authors>
  <license>
    <informal>Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported</informal>
    <year>2009</year>
    <text href="http://creativecommons.org/licenses/by-nc-sa/3.0/"/>
  </license>
  <src>
    <format>Web API</format>
    <link href="http://www.colourlovers.com/api/">Color Lovers</link>
    <link
       href="http://seaviewsensing.com/pub/cpt-city/notes/colocpt.pl"
       >Colocpt script</link>
  </src>
</copying>
EOF
;

    close COPY;

}

# cptfile

my $cptuserdir = "$cptdir/$sanuser";

unless (-e $cptuserdir)
{
    mkdir $cptuserdir;
}

my $cptfile = "$cptuserdir/$santitle.cpt";

print "creating $cptfile\n";

open CPT, "> $cptfile";

print CPT "# $santitle.cpt\n";
print CPT "# colourlover palette $title ($id) by $user\n";
print CPT "# license http://creativecommons.org/licenses/by-nc-sa/3.0/\n";
print CPT "# autogenerated by colocpt\n";
print CPT "# COLOR_MODEL = RGB\n";

my $offset = 0;

foreach my $hex (@$hexs)
{
    my $width = shift @widths;
    $width *= 5.0;

    if (my ($rs,$gs,$bs) = ($hex =~ /(..)(..)(..)/o))
    {
	my $r = hex($rs);
	my $g = hex($gs);
	my $b = hex($bs);

	printf CPT "%5.3f %3i %3i %3i %5.3f %3i %3i %3i\n",
	$offset,$r,$g,$b,$offset+$width,$r,$g,$b;
	$offset += $width;
    }
}

close CPT;

print "done.\n";
