#!/usr/bin/perl -w
#############################################################################
# This software is distributed under the terms of the General Public License.
#
# Program : kas2latex
# Authors : Christophe Gros
# E-Mail  : Llyra@altavista.com
############################################################################# 

$mois    = 0;
$annee   = 0;
$first   = 0;
$flag    = 0;
$flag1   = 0;
$dec     = "19";
$compte  = 0;
$january = 1;

@nommois = ("", "January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
@fields = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
@retro = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
@posi   = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
@name = ("", "\\Ari", "\\Tau", "\\Gem", "\\Can", "\\Leo",
	 "\\Vir", "\\Lib", "\\Sco", "\\Sag", "\\Cap", "\\Aqu", "\\Pis");

%plan = (
Ar => 1,
Ta => 2,
Ge => 3,
Cn => 4,
Le => 5,
Vi => 6,
Li => 7,
Sc => 8,
Sg => 9,
Cp => 10,
Aq => 11,
Pi => 12
);

sub retro($){
    local(*ligne) = @_;
    for($i = 0; $i <= $#ligne; $i++)
    {
	if($ligne[$i] =~ /\./)
	{
	    if($retro[$i] == 1)
	    {
		$ligne[$i] =~ s/\./*/;
	    }
	    else
	    {
		$ligne[$i]  =~ s/\./R/;
		$retro[$i] = 1;
	     }
	 }
	 else
	 {
	     if($retro[$i] == 1)
	     {
		 $ligne[$i] .= "D";
		 $retro[$i] = 0;
	     }
	     else
	     {
		 $ligne[$i] .= "*";
	     }
	 }
    }
}

sub jour($){
    local(*ligne) = @_;
    local($j, $m, $a) = split('-', $ligne[0]);
    local $test;
    $annee++ if(substr($annee, 2, 2) != $a);
    if($m != $mois)
    {
	$mois = $m;
	if($flag1 == 0)
	{
	    $flag1 = 1;
	}
	else
	{
print TEX <<EOF
\\hspace{-.33cm}
\\vspace{.2cm}
\\\\
\\hline
\\end{tabular}
\\vfill
EOF
}
$flag = 1;
	$a = (length($a) < 2) ? '0'.$a : $a;
if($january)
{
    $january = 0;
}
else
{
    print TEX "\\hspace {-.65cm} ";
}
print TEX <<EOF
{\\large \\textbf{$nommois[$mois] $annee}} \\nopagebreak \\vspace{0.1cm} \\nopagebreak \\\\
\\begin{tabular}{|r|r|r|r|r|r|r|r|r|r|r|r|r|}
\\hline
Date & \\Sun & \\Moo & \\Mer & \\Mar & \\Ven & \\Jup & \\Sat & \\Ura & \\Nep & \\Plu & \\Asc \\\\
\\hline
EOF
}
else
{
    print TEX "\\\\\n";
}
    retro(*ligne);
    $y = 0;
    print TEX "\\vs \\s " . $j . " & ";
for($i = 1; $i <= 11; $i++)
    {
	$ligne[$i] =~ /\D\D/;
	$test = $&;
	if(($plan{$test} != $posi[$i]) || ($flag == 1))
	{
	    $posi[$i] = $plan{$&} if($plan{$test} != $posi[$i]);
	    $ligne[$i] =~ s/\D\D/\{$name[$posi[$i]]\}/g;
	}
	else
	{
	    $ligne[$i] =~ s/\D\D/\\hspace\{.28cm\}/g;
	}
        $ligne[$i] =~ s/\*/\\hspace\{.22cm\}/g;
	print TEX "\\s " . $ligne[$i];
	print TEX " & " if($i < 11);
     }
$flag = 0 if($flag == 1);
}

sub setretro($)
{
    local(*ligne) = @_;
    for($i = 0; $i <= $#ligne; $i++)
    {
	    $retro[$i] = 1 if($ligne[$i] =~ /\./);
    }
}
print <<EOF
*********************************************************************
kas2latex

Copyright 2000 Christophe Gros <Llyra\@altavista.com>

kas2latex is an utility which produces an astrological ephemeris for
the 10 planets plus north node, in DVI format and for the legal paper
size.
In order to print the resulting ephem.dvi, you need to use the dvips
or the dvilj4 utilities. Don\'t forget to install a tex/latex package,
also containing the wasy font.
The file ephem.tex must be present in the same directory as kas2latex 
*********************************************************************


EOF
    ;
if($ARGV[0] eq "") {
    print "Year of beginning = ";chomp($annee=<STDIN>);
    print "Number of years   = ";chomp($nombre=<STDIN>);
}
else {
    $annee = $ARGV[0];
    $nombre = $ARGV[1];
};
die("Year must be a 4 digits number.\n") if(length($annee) < 4);
$anneed = $annee;
$anneef = $annee+$nombre-1;
$tmpastr = "/tmp/tmpastr.$$";
$tmpastr1 = "/tmp/tmpastr.tex";
die("Can't run astrolog !\n") if(system "kastrolog b qy $annee EY $nombre os $tmpastr");
open(EPHEM, $tmpastr) or die("Can't open ephemeris file.\n");
open(TEX, ">$tmpastr1") or die("Can't open tex file.\n");
LOOP: while(<EPHEM>)
{
    chomp;
    next LOOP if /^Dy/ || /^\r$/ || ($_ eq "");
    s/\./\. /g;
    s/- /-/g;
    @fields = split(' ', $_);
    if($first == 0) { $first = 1; setretro(*fields);}
    jour(*fields);
};
print TEX <<EOF
\\hspace{-.3cm}
\\vspace{.2cm}
\\\\
\\hline
\\end{tabular}
EOF
;
close EPHEM;
close TEX;
unlink $tmpastr;
system "echo $anneed-$anneef >/tmp/years";
die("Can't execute latex.\n"), if(system "latex $ENV{KDEDIR}/share/apps/kastrolog/ephem");
unlink $tmpastr1;
unlink "/tmp/years";





