#!/usr/bin/python
#    Terminator - multiple gnome terminals in one window
#    Copyright (C) 2006-2008  cmsj@tenshu.net
#
#    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, version 2 only.
#
#    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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

"""Terminator by Chris Jones <cmsj@tenshu.net>"""

# import standard python libs
import os, sys, string, time, math, subprocess
from optparse import OptionParser

#import version details
from terminatorlib.version import *

try:
  import gettext
  gettext.install (APP_NAME)
except:
  import __builtin__
  def _t (text):
    return text
  __builtin__.__dict__['_'] = _t

# import unix-lib
import pwd

# import some useful functions
from terminatorlib.config import dbg, err, debug
import terminatorlib.config

# import gtk libs
# check just in case anyone runs it on a non-gnome system.
try:
  import pygtk
  pygtk.require ("2.0")

  import gobject, gtk, pango
except:
  err (_("You need to install the python bindings for " \
    "gobject, gtk and pango to run Terminator."))
  sys.exit(1)

#import Terminator
from terminatorlib.terminator import Terminator

if __name__ == '__main__':
  def execute_cb (option, opt, value, parser):
    assert value is None
    value = []
    while parser.rargs:
      arg = parser.rargs[0]
      value.append (arg)
      del (parser.rargs[0])
    setattr(parser.values, option.dest, value)

  def profile_cb (option, opt, value, parser):
    assert value is None
    value = ''
    while parser.rargs:
      arg = parser.rargs[0]
      if arg[0] != '-':
        if len (value) > 0:
          value = '%s %s'%(value, arg)
        else:
          value = '%s'%arg
        del (parser.rargs[0])
      else:
        break
    setattr (parser.values, option.dest, value)

  usage = "usage: %prog [options]"
  parser = OptionParser (usage)
  parser.add_option ("-v", "--version", action="store_true", dest="version", help="Display program version")
  parser.add_option ("-d", "--debug", action="store_true", dest="debug", help="Enable debugging information")
  parser.add_option ("-m", "--maximise", action="store_true", dest="maximise", help="Open the %s window maximised"%APP_NAME.capitalize())
  parser.add_option ("-f", "--fullscreen", action="store_true", dest="fullscreen", help="Set the window into fullscreen mode")
  parser.add_option ("-b", "--borderless", action="store_true", dest="borderless", help="Turn off the window's borders")
  parser.add_option ("-p", "--profile", dest="profile", action="callback", callback=profile_cb, help="Specify a GNOME Terminal profile to emulate")
  parser.add_option ("-e", "--command", dest="command", help="Execute the argument to this option inside the terminal")
  parser.add_option ("-x", "--execute", dest="execute", action="callback", callback=execute_cb, help="Execute the remainder of the command line inside the terminal")

  (options, args) = parser.parse_args ()
  if len (args) != 0:
    parser.error("Expecting zero additional arguments, found: %d"%len (args))

  if options.version:
    print "%s %s"%(APP_NAME, APP_VERSION)
    sys.exit (0)
  
  if options.debug:
    terminatorlib.config.debug = True

  command = []
  if (options.command):
    command.append (options.command)
  if (options.execute):
    command = options.execute
  
  if gtk.gdk.display_get_default() == None:
    err (_("You need to run terminator in an X environment. " \
    "Make sure DISPLAY is properly set"))
    sys.exit(1)

  try:
    open (os.path.expanduser ('~/.config/terminator/config'))
  except:
    try:
      open (os.path.expanduser ('~/.terminatorrc'))
      error = gtk.MessageDialog (None, gtk.DIALOG_MODAL, gtk.MESSAGE_ERROR, gtk.BUTTONS_OK,
      ('''You have a configuration file:

      ~/.terminatorrc.

Please be aware that this file needs to be moved to:

      ~/.config/terminator/config.

See the following bug report for more details:

      https://bugs.launchpad.net/bugs/238070'''))
      error.run ()
      error.destroy ()
    except:
      pass

  dbg ('profile_cb: settled on profile: "%s"'%options.profile)
  term = Terminator (options.profile, command, options.fullscreen, options.maximise, options.borderless)

  gtk.main ()

