#!/bin/bash

### This code basically just generates the license PMs on-the-fly ###

PMDIR='lib/Software/License'
LYNX='lynx -dump -width 77 -display_charset US-ASCII -nolist -nonumbers'

for VER in 1.0; do
   for CODE in PDM; do
      PACKAGE=CC_`echo $CODE"_"$VER | tr '.-' '__'`
      URL=http://creativecommons.org/publicdomain/mark/$VER/
      LNAME="Public Domain Mark 1.0"
      META_NAME=unrestricted

      echo "$PACKAGE.pm - $LNAME"

      echo "package Software::License::$PACKAGE;

use strict;
use warnings;

use base 'Software::License';

# AUTHORITY
# VERSION
# ABSTRACT: Creative Commons $LNAME

### NOTE: This file was auto-generated using cc-pdmark.sh.  Do not edit this file!

sub name { 'Creative Commons $LNAME' }
sub url  { '$URL' }

sub version    { '$VER' }
sub meta_name  { '$META_NAME' }

1;
__DATA__
__LICENSE__" > $PMDIR/$PACKAGE.pm
      $LYNX $URL | perl -e '
         $_ = join("", <>);
         s/.+?\n(?=Public Domain Mark)//sm; # garbage above
         s/\n\s*Endorsement.+/\n/gs;        # garbage below
         s/ ( You can copy, modify, distribute)/\*$1/;  # missing bullet point
         s/^\s+\*\n//ms;                                #   extra bullet point
         s/^\s+\+ Non-binding use guidelines.+?\n(?=^\s+\*)//ms;  # more garbage
         print $_;
      ' >> $PMDIR/$PACKAGE.pm
   done
done
