#!/usr/bin/perl

#############################################################################
#                                                                           #
# Copyright (C) 2006 Pete Phillips                                          #
#                                                                           #
# This program is free software; you can redistribute it and/or modify it   #
# under the terms of the GNU General Public License as published by the     #
# Free Software Foundation; either version 2 of the License, or (at your    #
# option) any later version.                                                #
#                                                                           #
# This program is distributed in the hope that it will be useful, but       #
# WITHOUT ANY WARRANTY; without even the implied warranty of                #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General #
# Public License for more details.                                          #
#                                                                           #
# You should have received a copy of the GNU General Public License along   #
# with this program; if not, write to:                                      #
#                                                                           #
#      Free Software Foundation, Inc.                                       #
#      59 Temple Place - Suite 330                                          #
#      Boston, MA 02111-1307, USA.                                          #
#                                                                           #
# Or you can find the full GNU GPL online at: http://www.gnu.org            #
#                                                                           #
# Please send your comments, updates, improvements, wishes and bug reports  #
# to:                                                                       #
#                                                                           #
#      Pete Phillips           pete '@' smtl.co.uk                          #
#                                                                           #
#############################################################################

#############################################################################
#                                                                           #
# File:  org-na                                                             #
#                                                                           #
# $Id: org-na.pl,v 1.1 2006/03/05 22:57:50 pete Exp pete $                   #
#                                                                           #
# Usage: org-na [-h?] -t tag [orgfile1 orgfile2 ....]                       #
#                                                                           #
# Description:                                                              #
#        Used to print next actions from an org-mode file.                  #
#                                                                           #
#        The program searches through your org-mode files for tags          #
#        (strings between pairs of colons, eg: :Home:) specified on         #
#        the command line and and prints them out.                          #
#        The aim of this first version is to print                          # 
#        using LaTeX to 3x5" index cards (for the Hipster PDA).             #
#                                                                           #
# HOW TO USE IT                                                             #
# -------------                                                             #
#                                                                           #
#      Edit the variable below to set the location of your org-mode file    #
#                                                                           #
#      Assuming you have some next actions (NA's) in that file tagged       #
#      with :PhoneH: (i.e. phonecalls to make at home) then you can         #
#      create a latex file with all these calls in as follows:              #
#                                                                           #
#         org-na.pl -t PhoneH > PhoneH.tex                                  #
#                                                                           #
#      Then print to your printer as follows (remember this prints to       #
#      3x5" cards only at present):                                         #
#                                                                           #
#         latex PhoneH.tex                                                  #
#         dvips PhoneH.dvi                                                  #
#                                                                           #
#      That's about it!                                                     #
#                                                                           #
# Options:                                                                  #
#        -h      print out help                                             #
#        -t tag  create latex output with list of NA's tagged by 'tag'      #
#                                                                           #
#                                                                           #
# Author: Pete Phillips                                                     #
#                                                                           #
#############################################################################
#############################################################################
#                                                                           #
#  TODO                                                                     #
#                                                                           #
#     1 - allow selection of the org-mode file on comand line               #
#     2 - make the program create the latex file itself without requiring   #
#         redirection                                                       #
#     3 - Allow user to tune font sizes etc.                                #
########################################### ##################################

#############################################################################
#                                                                           #
#                 User modifiable variable definitions:                     #
#                                                                           #
# I am hard coding some variables here for the early versions while I get   #
# the basic code sorted.                                                    #
# Please nset ORGFILE to the location of your  org-mode file                #
#############################################################################
#
# Uncomment the function you wish to use.
#$CRCFunc = "/usr/bin/sum";
#$CRCFunc = "/usr/bin/cksum";
$ORGFILE = "/home/pete/TODO/TODO.org";
#$TMPDIR=/tmp/lyxorg.$$;


#############################################################################
# &Help;                                                                    #
# This routine explains brief usage syntax to STDOUT. The program is then   #
#  terminated.                                                              #
#############################################################################
#
sub Help
  {
  printf("Usage:\torg-na *** TO BE WRITTEN ****\n");
  printf("\tPrint out next action tags from an org-mode file.\n\n");
  printf("\tOptions:\n");
  printf("\t-l\tPrint card for this next action\n");
  exit;
  }


#############################################################################
# Main routine begins.                                                      #
#############################################################################
#

if($#ARGV==-1) { &Help; } # Help the user with the syntax
if(($#ARGV==0) && (@ARGV[0] !~ /^-/))
        { $dir = shift(@ARGV); }
else
        {
        foreach $arg (@ARGV)
                {
                if ($arg =~ /^-/)
                        {
                        if ($arg =~ /t/) { shift; $NAtag = shift(@ARGV);}
                        if ($arg =~ /\?/){ &Help; }
                        if ($arg =~ /\h/){ &Help; }
                        }
                }
#        shift;
#        $dir = shift(@ARGV);
        }

# open up the TODO.org file
open(ORGREAD,"<$ORGFILE") || die "Can't open log file: $ORGFILE";

&preamble;

    while (<ORGREAD>) {
	chop($_);
        if (/SEQ_TODO/) {next;}
	# replace the lines of stars
	s/\*+//;
	s/^\s+//; #strip whitespace
        s/\s+$//; #strip whitespace
        s/\s+/ /; #strip whitespace elsewhere leaving single space
        s/\t+//; #strip whitespace
        s/\&/\\&/g; #comnvert &
	if (/:$NAtag:/) {
	    s/:$NAtag://;
	    # replace the Tag with nothing also
	    print "\\item $_\n
\\vspace{-.3cm}\\hrulefill\\vspace{-0.3cm}\n";
	    }
    }
&tailtex;

exit;

sub preamble {
print <<PREAMBLE
\\documentclass{article}
\\usepackage{pslatex}
\\usepackage[T1]{fontenc}
\\usepackage[latin1]{inputenc}
\\usepackage{geometry}
\\geometry{verbose,paperwidth=3in,paperheight=5in,tmargin=0.8cm,bmargin=0.4cm,lmargin=0.3cm,rmargin=0.3cm,headsep=0.2cm}
\\makeatletter
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\\usepackage{multirow}
\\usepackage{supertabular}
\\usepackage{fancyheadings}
\\pagestyle{fancy}
\\lhead{\\large\\bfseries Next Actions:}
\\rhead{\\large\\bfseries $NAtag}
\\makeatother
\\begin{document}
\\begin{sloppypar}
\\begin{enumerate}
PREAMBLE
}

sub tailtex {
print <<TAILTEX
\\end{enumerate}
\\end{sloppypar}
\\end{document}
TAILTEX
}
