#!/bin/bash

#######################################################################
# 
# pstricks - compile LaTeX sniplet
# Copyright (C) 2009 Bjørn Bäuchle
# 
# 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 3 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, see <http://www.gnu.org/licenses/>.
# 
########################################################################


of=eps
view=true
viewer=""
output=""
texfile=pstricks_angucken_temp.tex
pstricks=""
documentclass="\documentclass{article}"
config=""
config1=~/.pstricks
config2=~/.config/pstricks

c_prog="\033[4;34m"
c_valu="\033[0;33m"
c_optn="\033[0;32m"
c_head="\033[1;31m"
c_file="\033[0;31m"
creset="\033[0m"

TEMP=$(getopt -o o: --long article:,of:,view,noview,viewer:,output:,package:,\
packopt:,documentclass:,documentclopt:,texfile:,pstricks:,help,h,version,\
config:,license -n $0 -- "$@")
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true; do
    case "$1" in
        --of) echo "of: $2"; of=$2; shift 2;;
        --view)   echo " view"; view=true; shift;;
        --noview) echo "!view"; view=false; shift;;
        --viewer) echo "viewer: $2"; viewer=$2; shift 2;;
        -o|--output) echo "output: $2"; output=$2; shift 2;;
        --texfile) echo "texfile: $2"; texfile=$2; shift 2;;
        --pstricks) echo "pstricks: $2"; pstricks=$2; shift 2;;
        --package) echo "package: $2"; load="$load\\usepackage{$2}"; shift 2;;
        --packopt) echo "package: $2"; pack=${2/:*/}; packopt=${2/*:/};
                   load="$load\\usepackage[$packopt]{$pack}"; shift 2;;
        --documentclass) echo "documentclass: $2"; documentclass="\\documentclass{$2}"; shift 2;;
        --documentclopt) echo "documentclass: $2"; class=${2/:*/}; classopt=${2/*:/};
                   documentclass="\\documentclass[$classopt]{$class}"; shift 2;;
        --version) echo "pstricks v0.9.5"; exit 0;;
        --license) echo "
pstricks - compile LaTeX sniplet
Copyright (C) 2009 Bjørn Bäuchle

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 3 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, see <http://www.gnu.org/licenses/>."
             exit 0;;
        -h|--help)
echo -e "
${c_prog}pstricks$creset - show a pstricks-picture

This program compiles pstricks (or any other LaTeX-sniplett) and shows the
results in a suitable viewer. By standard, all files created during this
process are deleted.

${c_head}SYNOPSIS:$creset

        ${c_prog}pstricks$creset [${c_valu}OPTIONS$creset] ${c_file}file.tex$creset

${c_head}OPTIONS:$creset

    $c_optn--help$creset Print this message
    $c_optn--version$creset Shows the version number
    $c_optn--license$creset Shows short information on the license
    $c_optn--of ${c_valu}FORM$creset   ${c_valu}Output format${creset} (${c_valu}eps${creset}).
       Due to the limitations of ${c_prog}dvips -E${creset}, not the whole picture may be within
       the bounding box. If so, use ${c_optn}--of ${c_valu}ps${creset}.
       (${c_valu}eps${creset}|${c_valu}ps${creset}|${c_valu}dvi${creset}|${c_valu}pdf${creset})
    $c_optn--(no)view${creset}   (${c_valu}view${creset}).
       Toggles viewing of output. If output is not viewed, it is saved, not 
       deleted.
    $c_optn--viewer ${c_prog}PROG${creset}   (${c_valu}gv${creset}|${c_valu}xpdf${creset}|${c_valu}xdvi${creset})
       Application used for viewing output. The default depends on the output
       format chosen (see ${c_optn}--of${creset})
    $c_optn--output${creset}, ${c_optn}-o ${c_file}filename$creset
       Name for output file. If given, it is not deleted in the end
       (independent of ${c_optn}--noview${creset}).
    $c_optn--texfile ${c_file}temp$creset   ${c_valu}Temporary file name$creset (${c_valu}pstricks_angucken_temp.tex$creset)
       The file into which the temporary LaTeX-Code is written. Must be
       non-existent, fails otherwise.
    $c_optn--pstricks ${c_file}file$creset
       Input ${c_file}file$creset instead of the first 'loose' argument
    $c_optn--package ${c_valu}PACKAGE$creset   ${c_valu}Load additional package$creset
       Load package ${c_valu}PACKAGE$creset. If you need to give options to this package,
       use ${c_optn}--packopt${creset}.
       '\\usepackage{${c_valu}PACKAGE$creset}'
    $c_optn--packopt ${c_valu}PACKAGE:${c_file}OPTION$creset   ${c_valu}Load additional package with option$creset
       Like $c_optn--package$creset, only with options. Be sure to have a colon (${c_valu}:$creset)
       between package name and option
       '\\usepackage[${c_file}OPTION$creset]{${c_valu}PACKAGE$creset}'
    $c_optn--documentclass ${c_valu}CLASS$creset   ${c_valu}Load special class$creset (${c_valu}article$creset)
       Use this documentclass for the LaTeX document. Useful if you are e.g.
       trying to view pstricks with in-built overlays for ${c_prog}beamer$creset-presentations.
       If you need to a documentclass with additional options,
       use ${c_optn}--documentclopt${creset}.
       '\\documentclass{${c_valu}CLASS$creset}'
    $c_optn--documentclopt ${c_valu}CLASS:${c_file}OPTION$creset   ${c_valu}Use special class with options$creset
        Like ${c_optn}--documentclass${creset}, only with options. Be sure to have a colon (${c_valu}:$creset)
        between classname and option
        '\\documentclass[${c_file}OPTION$creset]{${c_valu}CLASS$creset}'
    $c_optn--config ${c_file} file$creset   ${c_valu}Use configuration file$creset
        Configuration file to be read in. There are three sections that may be
        specified in the file: ${c_valu}load$creset, ${c_valu}before$creset and ${c_valu}after$creset.
        The format is as follows: 
             1.) A single line containing the name of the section and a colon.
                 No Whitespace.
             2.) An Arbitrary number of lines with the content. Be aware that
                 line breaks are not read and therefore discarded; therefore,
                 do not use LaTeX-style "$c_valu%$creset"-comments in the file.
             3.) An empty line (may be omitted at end of file). May contain
                 whitespaces.
             ${c_valu}load:
             ${c_valu}This is for the section load.
             ${c_valu}So is this.

             ${c_valu}This is not.$creset
        The content of the sections are added to the LaTeX code in different
        positions:
       $c_valu load$creset is added in the document header, i.e. before \\\\begin{document}.
       $c_valu before$creset is added in the document, but before the included file, and
       $c_valu after$creset is added after the included file, but before \\\\end{document}.

${c_head}ENVIRONMENT:$creset
    Reads the environment variables \$load, \$before and \$after and uses them
    as specified in the coniguration file (see $c_optn--config$creset). Text specified in the
    config file is appended to existing values of these variables.

${c_head}KNOWN BUGS:$creset
    None.

${c_head}AUTHOR, LICENSE:$creset
    Copyright (C) ${c_prog}Bjørn Bäuchle$creset <${c_optn}baeuchle at th.physik.uni-frankfurt.de$creset>

    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 3 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, see <http://www.gnu.org/licenses/>.
" | less -R
        exit 1;;
        --config) echo "Configuration file: $2"; config=$2; shift 2;;
        --) shift; break;;
        *) echo "Unknown option: $1"; shift; exit 1;;
    esac
done

# choosing configuration file
configfile=""
if [ "$config" != "" ] && [ -e $config ]; then
    configfile=$config
elif [ -e $config1 ]; then configfile=$config1;
elif [ -e $config2 ]; then configfile=$config2;
fi

# reading configuration file:
if [ "$configfile" != "" ]; then
    load=$load"$(perl -ne 'print if $a == 1; $a = 1 if /^load:$/; $a=0 if /^\s*$/' $configfile)"
    before=$before"$(perl -ne 'print if $b == 1; $b = 1 if /^before:$/; $b=0 if /^\s*$/' $configfile)"
    after=$after"$(perl -ne 'print if $c == 1; $c = 1 if /^before:$/; $c=0 if /^\s*$/' $configfile)"
fi;

dvifile=${texfile/.tex/}
if [ "$pstricks" == "" ] && [ "$1" != "" ]; then pstricks=$1; fi
if [ "$output" == "" ]; then output=${texfile/.tex/.$of}; else nodelete=1; fi
if [ "$viewer" == "" ]; then
    case "$of" in
        dvi) viewer=xdvi;;
        *ps) viewer=gv;;
        pdf) viewer=xpdf;;
    esac
fi
if [ -e $texfile ]; then echo "File $texfile already exists" >&2; exit 1; fi
echo "$documentclass\pagestyle{empty}\usepackage{pstricks}$load\begin{document}$before\input{$pstricks}$after\end{document}" > $texfile

echo $output

latex --interaction batchmode $texfile
case "$of" in
    dvi) ;; # nothing to be done for dvi: We have a dvi anyway.
    eps) dvips $dvifile -E -o $output;;
     ps) dvips $dvifile    -o $output;;
    pdf) dvipdf $dvifile -E -o $output;;
esac

if [ "$view" == "true" ]; then
    $viewer $output
    if [ "$nodelete" != "1" ]; then rm $output; fi
fi;
rm $texfile ${texfile/.tex/.aux} ${texfile/.tex/.log}
if [ "$of" != "dvi" ]; then
   rm ${texfile/.tex/.dvi}
fi

exit 0;
