# -*- coding: utf-8 -*-

# Songwrite 2
# Copyright (C) 2001-2007 Jean-Baptiste LAMY -- jibalamy@free.fr
#
# 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

import sys, os, array, re, cStringIO as StringIO, tempfile, shutil, popen2
import string as string_module # not a guitar string !

import songwrite2.globdef  as globdef
import songwrite2.model    as model
import songwrite2.plugins  as plugins


def export(song):
  temp_dir     = tempfile.mktemp()
  temp_latex   = os.path.join(temp_dir, "songwrite.latex")
  temp_tex     = os.path.join(temp_dir, "songwrite.tex")
  temp_dvi     = os.path.join(temp_dir, "songwrite.dvi")
  temp_ps      = os.path.join(temp_dir, "songwrite.ps")
  temp_psfonts = os.path.join(temp_dir, "songwrite.psfonts")
  
  os.mkdir(temp_dir)
  
  try:
    plugins.get_exporter("latex").export_to(song, temp_latex)
    
    run_lily (temp_latex, temp_tex)
    run_latex(temp_tex  , temp_dvi)
    if isinstance(song, model.Songbook):
      run_latex(temp_tex, temp_dvi)
      
    run_dvips(temp_dvi, temp_psfonts, temp_ps)
    return open(temp_ps).read()
  
  finally:
    try: shutil.rmtree(temp_dir) # Clean all temp files
    except: pass
    
  
def run_lily(filename, output):
  print >> sys.stderr
  print >> sys.stderr
  command = "cd %s; lilypond-book --psfonts -o %s %s" % (os.path.dirname(filename), os.path.dirname(output), filename)
  print >> sys.stderr, "Running '%s'" % command
  #os.system(command)
  print >> sys.stderr, popen2.popen4(command)[0].read()
  
def run_latex(filename, output):
  print >> sys.stderr
  print >> sys.stderr
  latex = open(filename).read()
  if not u"\\usepackage{graphicx}" in latex:
    print >> sys.stderr, u"Warning, '\\usepackage{graphicx}' is missing in latex code, i'll add it..."
    i = latex.find(u"\\usepackage")
    latex = latex[:i] + u"\\usepackage{graphicx}\\n" + latex[i:]
    open(filename, "w").write(latex)
    
  command = "cd %s; latex %s" % (os.path.dirname(filename), os.path.basename(filename))
  print >> sys.stderr, "Running '%s'" % command
  print >> sys.stderr, popen2.popen4(command)[0].read()
  
  os.rename(filename[:-3] + "dvi", output)
  
def run_dvips(filename, psfonts, ps):
  print >> sys.stderr
  print >> sys.stderr
  
  # dvips does not support a complete path for PS font file ! => Need to cd in PS font file directory.
  command = "cd %s; dvips -h %s -o %s %s" % (os.path.dirname(psfonts), os.path.basename(psfonts), ps, filename)
  print >> sys.stderr, "Running '%s'" % command
  print >> sys.stderr, popen2.popen4(command)[0].read()
