#!/usr/bin/env python
#

"""
gscript: graphterm scripting
Actions:
 - Save all previous commands to a file
 - Load commands from file into script buffer for future execution
   (Use Control-Enter key sequence to execute next command from script)
 - Clear script buffer
 - Edit script buffer
"""

from __future__ import absolute_import, print_function

import mimetypes
import os
import sys
import tty
import termios

from optparse import OptionParser

try:
    import gterm
except ImportError:
    import graphterm.bin.gterm as gterm

Work_dir = os.getcwd()

usage = "usage: %prog --buffer --clear --edit [<filename>]"
parser = OptionParser(usage=usage)
parser.add_option("-b", "--buffer",
                  action="store_true", dest="buffer", default=False,
                  help="Save in script buffer")

parser.add_option("-c", "--clear",
                  action="store_true", dest="clear", default=False,
                  help="Clear script buffer")

parser.add_option("-e", "--edit",
                  action="store_true", dest="edit", default=False,
                  help="Edit script buffer")

(options, args) = parser.parse_args()

if options.clear:
    options.buffer = True

if not args and not (options.clear or options.edit):
    print(parser.get_usage(), file=sys.stderr)
    sys.exit(1)

filepath = os.path.abspath(os.path.normpath(args[0])) if args else ""

params = {"action": "buffer" if options.buffer else "save", "filepath": filepath,
          "editor": "", "modify": options.edit, "command":"", "current_directory": os.getcwd()}

html_headers = {"content_type": "text/plain",
                "x_gterm_response": "script_op",
                "x_gterm_parameters": params
                }

content = ""
if options.buffer and not (options.clear or options.edit):
    with open(filepath) as fp:
        content = fp.read()
        
gterm.wrap_write(content, headers=html_headers)
