#!/usr/bin/perl -w
#
# Origami CD Case Maker
# Version 0.1
# Written by David Helder (dhelder@umich.edu)
# Copyright (C) 2001 David A. Helder
# See license below.
#
# Inspired by Tom Hull's Origami CD case.
# See http://web.merrimack.edu/~thull/ for folding instructions
#
# origamicdcase creates postscript file which you can print
#   and fold to make an origami CD case.
#
# This script takes input (from file or stdin) like this:
#
# Artist
# Title
# Track 1 name
# Track 2 name
# [...]
#
# It will skip blank lines.  "Artist" is the first line on the
# case, "Title" is the second.  These do not necessarily need
# to be the actual title or artist.
#
# The script will use Latex to create a postscript file called
# "Artist - Title.ps".  
#
# Hints:
#   - Use '' and `` to make double quotes instead of ".  
#   - Do \o: to make an o with a thingy (e.g. Bj\o:rk).  Use
#       ' or ` instead of : to make accents.  Use a,e,i,o, or u.
#       Also \n~ makes an n with a thingy (e.g. Se\n~ior 
#       Coconut).
#   - Hack up the file to get it to do what you want.  It's just
#       Perl and Latex.  Mail me improvements and I'll include
#       them in future versions.
#
# Todo:
#   - Track numbers don't match up with titles right.  Fix this
#       somehow.
#   - Add option to create PDF file
#   - Make this into a CGI (need to fix \ escaping)
#   - Add option to use other fonts
#   - Add a spine label as Tom suggests in his directions
#   - Add folding directions and dotted-lines (maybe on 
#       reverse side?
#   - Add background pictures/tiles on non-cover parts
#
#
# 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 the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA




my $header = 
    '\documentclass[letterpaper,10pt]{article}' .
    '' .
    '\pagestyle{empty}'.
    '\setlength{\topmargin}{2.5in}'.
    '\setlength{\headheight}{0pt}'.
    '\setlength{\headsep}{0pt}'.
    '\setlength{\footskip}{0pt}'.
    '\setlength{\oddsidemargin}{0.5in}'.
    '\setlength{\evensidemargin}{0.75in}'.
    '\setlength{\textwidth}{8.5in}'.
    '' .
    '\begin{document}' .
    '\begin{sffamily}' .
    '' .
    '\setlength{\unitlength}{1in}' .
    '\thinlines'.
    '\begin{picture}(5,5)'.
    '\put(0,0){\framebox(5,5)[lt]{}}';

my $footer = 
    '\end{picture}' .
    '\end{sffamily}' .
    '\end{document}';

my $index = -2;
my $artist = 'Artist';
my $title = 'Title';
my @tracks;

while(<>)
{
    chomp;
    next if ($_ eq '');

    # FIX \'s
#    s/\\/\\backslash/g;           # \

    # Escape command characters
    s/\$/\\\$/g;           # $
    s/&/\\&/g;             # &
    s/%/\\%/g;             # %
    s/\#/\\\#/g;           # #
    s/_/\\_/g;             # _
    s/{/\\{/g;             # {
    s/}/\\}/g;             # {

    # Fix accents ', `, "
    s/\\([aeiou])\'/\\\'{$1}/g;
    s/\\([aeiou])\`/\\\`{$1}/g;
    s/\\([aeiou])\"/\\\"{$1}/g;
    s/\\([n])~/\\~{$1}/g;


    if ($index == -2)
    {
	$artist = $_;
    }
    elsif ($index == -1)
    {
	$title = $_;
    }
    else
    {
	push (@tracks, $_);
    }

    $index++;
}

open (LATEX, "|latex") or die "Can't find latex\n";

print LATEX $header;

print LATEX "\\put(.20,4.70){\\makebox(0,0)[l]{{\\large \\textbf{$artist}}}}";
print LATEX "\\put(.20,4.50){\\makebox(0,0)[l]{{\\large \\textbf{$title}}}}";

$index = 1;
foreach $track (@tracks)
{
    $place = 4.40 - ($index * .20);

    print LATEX "\\put(.20,$place){\\makebox(0,0)[l]{$index}}";
    print LATEX "\\put(.42,$place){\\makebox(0,0)[l]{$track}}";
    $index++;
}

print LATEX $footer;

close (LATEX);

system ("dvips -o \"$artist - $title.ps\" article.dvi");
system ("rm -f article.tex article.dvi article.log article.aux");
