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

# Songwrite 2
# Copyright (C) 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 songwrite2.globdef as globdef
import songwrite2.plugins as plugins


class ExportPlugin(plugins.ExportPlugin):
  def __init__(self):
    plugins.ExportPlugin.__init__(self, "PostScript", ["ps"], 1, 1)
    
  def export_to_string(self, song):
    import songwrite2.plugins.ps.postscript as postscript
    return postscript.export(song)
    
  def print_preview(self, song):
    ps = self.export_to_string(song)
    if "%s" in globdef.config.PREVIEW_COMMAND:
      import tempfile, popen2
      fid, filename = tempfile.mkstemp(suffix = ".ps", text = 0)
      open(filename, "w").write(ps)
      command = globdef.config.PREVIEW_COMMAND % filename + " ; rm %s" % filename
      print "Running '%s'" % command
      popen2.popen4(command)
    else:
      import popen2
      print "Running '%s'" % globdef.config.PREVIEW_COMMAND
      output, input = popen2.popen4(globdef.config.PREVIEW_COMMAND)
      input.write(ps)
      input.close()
          
ExportPlugin()
