#! /usr/bin/perl
#############################################################################
# 
#############################################################################
#  
#   GLOBAL VARIABLES {
#

$dlfountain=12;
$dlsizinti=30;
$dlyeniumit=12;
$dlaksiyon=100;
$FOUNTAIN=1;
$YSA=1;

#############################################################################
 
$outfile="yeniaboneler.html";

$dirname = ".";
$subdir = "false";

$infile=$ARGV[0];
$mode=$ARGV[1];
$out=$ARGV[2];

if ($out eq "") {$out=$mode;}
   
if ( ($infile eq "")  ) {
    print"\n USAGE: \n \taboneler.pl   infile [F][YSA] ";
    exit();
}
$outfile=$infile;
$outfile=~ s/\..*//;
if ( ($mode eq "txt")  ) {$outfile.=".txt"}
else {$outfile.=".html"}
if ($infile =~ /\.html/) {$input="html";}

if ($out eq "F") {$YSA=0; $FOUNTAIN=1; $outfile="F".$outfile; }
if ($out eq "YSA") {$YSA=1; $FOUNTAIN=0; $outfile="YSA".$outfile;}



&readRecords;
&updateRecords;
&writeRecords;

sub readRecords() {
    $index=0;

 open(IN, "<$infile"); 
 while (<IN>) {
    $line=$_;    chop($line);

     if ($line =~ /.*\<abone\>.*/) {
	  while (!($line =~ /.*\<\/abone\>.*/ )) {
	      $_=<IN>; 
	      $line.=$_;
	      chop($line);
	  }
	  $aboneler[$index]=$line;
	  $index++;
      } elsif ($line =~ /.*<notes>.*/) {
	  while (!($line =~ /.*<\/notes>.*/ )) {
	      $_=<IN>; 
	      $line.=$_;
	      chop($line);
	  }
	  $_=$line;
	  ($notes)=/.*<notes>(.*)<\/notes>.*/;
      }
 }
 close(IN);

}


sub removeTag(){
    local($tag)=@_;
    $_=$line;
    ($b,$val,$e)=/(.*)<$tag>(.*)<\/$tag>(.*)/;
    $line=$b.$e;
    return $val;
}
sub processAbone(){
    local ($line)=@_;

 
    $line=&removeTag("abone");
    $name=&removeTag("name");
    $adres =&removeTag("adres");
    $f =&removeTag("f");
    $yu =&removeTag("yu");
    $szn =&removeTag("szn");
    $aks =&removeTag("aks");
    $total =&removeTag("total");
    $paid =&removeTag("paid");
    $bitis =&removeTag("bitis");


   # ($name,$adres,$f,$yu,$szn,$aks,$total,$paid,$bitis) = /.*<abone>.*<name>(.*)<\/name>.*<adres>(.*)<\/adres>.*<f>(.*)<\/f>.*<yu>(.*)<\/yu>.*<s>(.*)<\/s>.*<aks>(.*)<\/aks>.*<total>(.*)<\/total>.*<paid>(.*)<\/paid>.*<bitis>(.*)<\/bitis>.*<\/abone>.*/;

    $adres=~ s/<br>//g;
    $adres=~ s/Syracuse/<br>Syracuse/g;
    $total=~ s/\$//g;
    $paid=~ s/\$//g;


    $abone{$name}=$line;
    print " ($name,$adres,$f,$yu,$szn,$aks,$total,$paid,$bitis) \n";
    $counter_f+=$f;
    $counter_yu+=$yu;
    $counter_szn+=$szn;
    $counter_aks+=$aks;
    $counter_total+=$total;
    $counter_paid+=$paid;
  
}   

sub updateRecords(){

}

sub writeRecords(){

 open(OUT, ">$outfile"); 

 if ($mode ne "txt"){  &printHeader}
 $ind=0;
 foreach $ab (@aboneler) {
     $ind++;
      &processAbone($ab);
     if ($mode ne "txt"){       &printAboneHTML;}
     else { &printAbone;}
 }

 print  "Fountain  : $counter_f \n";
 print  "Yeni Umit : $counter_yu\n";
 print  "Sizinti   : $counter_szn\n";
 print  "Aksiyon   : $counter_aks\n";
 print  "Total     : $counter_total\n";
 print  "Paid      : $counter_paid \n";
    $cost_f=$counter_f*$dlfountain;
    $cost_szn=$counter_szn*$dlsizinti;
    $cost_yu=$counter_yu*$dlyeniumit;
    $cost_aks=$counter_aks*$dlaksiyon;
 $total= $cost_f+$cost_szn+$cost_yu+ $cost_aks;
 if ( $total !=  $counter_total ) {
     print  "Warning: Check Totals!\n";
 }

 if ($mode ne "txt"){ &printFooterHTML} else {&printFooter}

 close(OUT);

}
sub printAbone(){

    print OUT <<EOF;
 --------------------------------------------------------  
<abone>
    <name>$name</name>
	<adres>$adres</adres>
	    <f>$f</f>
		<yu>$yu</yu>
		    <szn>$szn</szn>
			<aks>$aks</aks>
			    <total>\$$total</total>
				<paid>\$$paid</paid>
				    <bitis>$bitis</bitis>
</abone>
 --------------------------------------------------------  
EOF
}
sub printAboneHTML(){
    if ( $color eq "#FFFF00")
	{$color="#fffacd"; }
    else {$color="#FFFF00";}
    print OUT <<EOF;


<tr BGCOLOR="$color">
<td><font size=-1>$ind</font></td>
<abone>
<td><font color="#000000" size=-1><name>$name</name></font></td>
<td><center><font color="#000000" size=-1><adres>$adres</adres></font></center></td>
EOF
    if ($FOUNTAIN) {
    print OUT <<EOF;
<td><center><font color="#000000" size=-1><f>$f</f></font></center></td>
EOF
}
    if ($YSA) {
    print OUT <<EOF;
<td><center><font color="#000000" size=-1><yu>$yu</yu></font></center></td>
<td><center><font color="#000000" size=-1><szn>$szn</szn></font></center></td>
<td><center><font color="#000000" size=-1><aks>$aks</aks></font></center></td>
EOF
}
    if ($YSA &&$FOUNTAIN ) {
    print OUT <<EOF;
<td><center><font color="#000000" size=-1><total>$total</total></font></center></td>
<td><center><font color="#000000" size=-1><paid>$paid</paid></font></center></td>
EOF
}
   print OUT <<EOF;
<td><center><font color="#000000" size=-1><bitis>$bitis</bitis></font></center></td>
</abone>

</tr>

<!-- --------------------------------------------------------   -->




EOF
}

sub printHeader(){

    print OUT <<EOF;
<html>
<head>
   <title>YENI ABONELER</title>
</head>
<body  bgcolor="#ccffff" >
<b>YENI ABONELER</b>
<p><i><font size=-1>Sizinti \$30 Yeni Umit \$12 Fountain \$12 Aksiyon \$100(1 sene) **** <font color="#0000FF"> as of 
<script Language="JavaScript">
document.writeln(document.lastModified);
</script>
</font></font></i>
<br>
<table BORDER=3 CELLSPACING=3 CELLPADDING=0  >
<tr>
<td>*</td>

<td ALIGN=CENTER VALIGN=CENTER><b><font color="#0000FF">ISIM SOYISIM</font></b></td>

<td>
<center><b><font color="#0000FF">ADRES</font></b></center>
</td>


EOF
    if ($FOUNTAIN) {
    print OUT <<EOF;
	
<td>
<center><font color="#0000FF"><b>F</b><font size=-2>ountain</font></font></center>
</td>

EOF
}

    if ($YSA) {
    print OUT <<EOF;

<td>
<center><font color="#0000FF"><b>Y</b><font size=-2>eni</font> <b>U</b><font size=-2>mit</font></font></center>
</td>

<td>
<center><font color="#0000FF"><b>S</b><font size=-2>izinti</font></font></center>
</td>

<td>
<center><font color="#0000FF"><b>A</b><font size=-2>ks</font></font></center>
</td>
EOF
}

    if ($YSA &&$FOUNTAIN ) {
    print OUT <<EOF;
<td>
<center><b><font color="#0000FF"><font size=-1>Total \$</font></font></b></center>
</td>

<td>
<center><b><font color="#0000FF"><font size=-1>Paid \$</font></font></b></center>
</td>
EOF
}
    print OUT <<EOF;

<td>
<center><b><font color="#0000FF">B<font size=-1>itis</font></font></b></center>
</td>
</tr>



EOF
}
sub printFooterHTML(){

    print OUT <<EOF;

<tr>
<td COLSPAN="2">
<br><b><font face="Courier New,Courier"><font color="#CC0000"><font size=-1>TOTALS</font></font></font></b></td>

<td>
<center><b><font color="#000000"><font size=-1>Syracuse, NY 132xx</font></font></b></center>
</td>
EOF
    if ($FOUNTAIN) {
    print OUT <<EOF;

<td>
<center><b><font color="#000000"><font size=-1>$counter_f</font></font></b>
<br><b><font color="#0000ff"><font size=-1>\$$cost_f</font></font></b></center>
</td>
EOF
}
    if ($YSA) {
    print OUT <<EOF;

<td>
<center><b><font color="#000000"><font size=-1>$counter_yu</font></font></b>
<br><b><font color="#0000ff"><font size=-1>\$$cost_yu</font></font></b></center>
</td>

<td>
<center><b><font color="#000000"><font size=-1>$counter_szn</font></font></b>
<br><b><font color="#0000ff"><font size=-1>\$$cost_szn</font></font></b></center>
</td>

<td>
<center><b><font color="#000000"><font size=-1>$counter_aks</font></font></b>
<br><b><font color="#0000ff"><font size=-1>\$$cost_aks</font></font></b></center>
</td>
EOF
}
    if ($YSA &&$FOUNTAIN ) {
    print OUT <<EOF;

<td>
<center><b><font color="#CC33CC"><font size=-1>\$$total</font></font></b></center>
</td>

<td>
<center><b><font color="#000000"><font size=-1></font></font></b>
<br><b><font color="red"><font size=-1>\$$counter_paid</font></font></b></center>
</td>
EOF
}
    print OUT <<EOF;

<td>
<center><b><font size=-1>1 yillik</font></b></center>
</td>
</tr>
</table>
<notes>$notes</notes>
</body>
</html>

EOF
}

sub printFooter(){

    print OUT <<EOF;
<notes>$notes</notes>

EOF
}
