#!/usr/bin/perl

#create synonym tables from taxon input files
 
@INFILES = (
    "/tmp/etaxon.txt",
    "/tmp/elwttaxon.txt",
    "/tmp/efwataxon.txt",
    "/tmp/ewattletaxon.txt",
    )
    ;

@OUTPATHS = (
    "/data/apache/htdocs/PlantNet/NSWflora/",
    "/data/apache/htdocs/PlantNet/Liverworts/",
    "/data/apache/htdocs/PlantNet/fwalgae/",
    "/data/apache/htdocs/PlantNet/WattleWeb/",
    )
    ;

$LOG = ">>/tmp/syn_log";
 
open (LOG);

foreach $i (0 .. $#INFILES) {
    if (-e $INFILES[$i]) {
        $TAXON    = $INFILES[$i];
        $newtaxon = $OUTPATHS[$i]."etaxon.txt";
        $newsyn   = $OUTPATHS[$i]."syn.txt";
   
        open (TAXON) || die "Cannot open TAXON for read :$!";
        open (SYN, ">$newsyn") || die "Cannot open $newsyn for write :$!";
        open (NEW,">$newtaxon");

#if llwt || fwa $nsw = "N.S.W. ";

        while (<TAXON>) {  
            chomp;
            print NEW "$_\n";
            @new = split (/\~/);
            if ($new[13] ne "") {
                if ($new[14] ne "") { 
                    $state = $new[13]." ".$new[14]; 
                }
                else { 
                    $state = $new[13]; 
                }
            } 
            elsif ($new[14] ne "") {
                $state = $new[14]; 
            }
            if ($state ne "") {
                if ($new[15] ne "") {
                    $country = $nsw.$new[15]; 
                }
                else  { 
                    $country = "N.S.W.";  
                }  
            }
            else  { 
                $country = $new[15];  
            }
            if ($new[13] =~ /\*[A-Z]/ && $new[13] !~ / [A-Z]|^[A-Z]/) { 
                $intro = "Y"; # introduced
            }
            if ($new[13] =~ /\*[A-Z]/ && $new[13] =~ / [A-Z]|^[A-Z]/) { 
                $intro = "YN"; # both introduced and native
            }
            if ($new[34] ne "noxious" && $new[34] ne "") { 
                $enda = "Y"; 
            }
            if ($new[20] ne q{} && $new[20] !~ /single/) { 
                $infr = "&gt;"; 
            }
            if ($new[0] eq "{sp}" || $new[0] eq "{in}") {
                print SYN "$new[2] $new[3] $new[4]~OK~$new[5]~$new[1]~$intro~$enda~$new[34]~$state~$country~$infr~$new[38]\n";
            }
            if ($new[17] ne "") {
                @syn = split(/; /,$new[17]);
                foreach $i ( 0 .. $#syn) {
                    if ($new[2] eq q{}) { 
                    print SYN "$syn[$i]~$new[1]~~$new[1]~$intro~$enda~~$state~$country~~$new[38]\n"; #family synonym
                    }
                    else {
                    print SYN "$syn[$i]~$new[2] $new[3] $new[4]~~$new[1]~$intro~$enda~~$state~$country~~$new[38]\n";
                    }
                }
            }
            $intro = $enda = $state = $country = $infr = "";
        }
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        print LOG "$INFILES[$i] written - $hour:$min, $mday $mon\n";
#       unlink $TAXON;
    } 
    else {
        ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
        print LOG "$INFILES[$i] not found - $hour:$min, $mday $mon\n";
    }
}

#etaxon.txt

#0    {in}~
#1    Orchidaceae~
#2    Dendrobium~
#3    gracilicaule~
#4    var. howeanum~
#5    Bush Orchid~
#6    ~
#7    ~
#8    ~
#9    ~
#10   ~
#11   ~
#12   ~
#13   ~
#14   LHI~
#15   ~
#16   ~
#17   Thelychiton howeanus (Maiden) M.A.Clem. & D.L.Jones; Dendrobium macropus subsp. howeanum (Maiden) P.S.Green~
#18   ~
#19   ~
#20   ~
#21   ~
#22   Maiden~
#23   ~
#24   ~
#25   ~
#26   ~
#27   ~
#28   ~
#29   ~
#30   ~
#31   ~
#32   ~
#33   ~
#34   ~
#35   ~
#36   ~
#37   ~
#38   Monocot~
#39   27 Nov 2002

#0    {sp}~
#1    Orchidaceae~
#2    Dendrobium~
#3    gracilicaule~
#4    ~
#5    ~
#6    P. H. Weston~
#7    Epiphyte or occasionally an epilith, with stems erect to spreading, succulent, cylindrical, tapering towards the base, rooting only at the base, 20-60 cm long, 5-10 mm diam., with 3-6 leaves at apex; roots smooth, creeping.~
#8    Leaves spreading to erect, elliptic, 5-1 3 cm long, 20-40 mm wide, conduplicate, thin, smooth.~
#9    Inflorescences 5-12 cm long, 5-30-flowered. Sepals and lateral petals deep dull yellow with dense reddish blotching on the outside; dorsal sepal 7-10 mm long, 3-4 mm wide; labellum dull yellow, 7-8 mm long, 5-6 mm wide. Column c. 3 mm long; column foot c. 5 mm long.~
#10   ~
#11   July-Sept.~
#12   Grows on rainforest trees and also occasionally on rocks; north from the Hawkesbury R.~
#13   NC CC~
#14   ~
#15   Qld~
#16   ~
#17   Thelychiton gracilicaulis (F.Muell.) M.A.Clem. & D.L.Jones; Dendrobium macropus subsp. gracilicaule (F.Muell.) P.S.Green~
#18   ~
#19   ~
#20   ~
#21   ~
#22   F.Muell.~
#23   Flora of NSW 4 (1993)~
#24   37/647/1579.png~
#25   ~
#26   ~
#27   ~
#28   35/590/Dendrobium gracilicaule2 cult.jpg~
#29   ~
#30   ~
#31   ~
#32   ~
#33   ~
#34   ~
#35   ~
#36   ~
#37   ~
#38   Monocot~
#39   20 Jul 2001


#syn.txt

#Dendrobium kingianum x gracilicaule~Dendrobium x suffusum~~Orchidaceae~~~~NC ?NT~Qld
#Thelychiton howeanus (Maiden) M.A.Clem. & D.L.Jones~Dendrobium gracilicaule~~Orcidaceae~~~~LHI~N.S.W.
#Dendrobium macropus subsp. howeanum (Maiden) P.S.Green~Dendrobium gracilicaule~~Orchidaceae~~~~LHI~N.S.W.
#Dendrobium gracilicaule ~OK~~Orchidaceae~~~~NC CC~Qld~~Monocot Thelychiton gracilicaulis (F.Muell.) M.A.Clem. & D.L.Jones~Dendrobium gracilicaule~~Orchidaceae~~~~NC CC~Qld
#Dendrobium macropus subsp. gracilicaule (F.Muell.) P.S.Green~Dendrobium gracilicaule~~Orchidaceae~~~~NC CC~Qld
