#!/usr/bin/env python

__author__  = 'Gouichi Iisaka <iisaka51@hotmail.com>'
__author2__ = 'Yuusuke Saito(zhaiteng_yujie@hotmail.com)'
__version__ = '3.0'

import os
import sys
sys.path.insert(0,'/usr/local/lib/python')
import string
import time
import scatter.cmd

class App(scatter.cmd.Application):
    def __init__(self,argv=sys.argv):
        self.usage_msg = "Usage: %prog [options] commands"
        self.default_option = [
            scatter.cmd.Option("-W", "--wait","--interval",action="store",
                    type="int", dest="interval",
		    default=0,
		    help="Wait interval in specified seconds"),
	    scatter.cmd.Option("-L","--loopmax",action="store",type="int",
		    dest="loopmax",
		    default=1,
		    help="Frequence for execute command"),
	    scatter.cmd.Option("-C","--copy",action="store_true",
		    dest="do_copy",
		    default=False,
		    help="Copy restult into filename of node"),
	    scatter.cmd.Option("-m","--mono",action="store_true",
		    dest="do_mono",
		   default=False,
		    help="Print nodename as mono color"),
	    scatter.cmd.Option("-n","--noexec","--dryrun",action="store_true",
		    dest="do_dryrun",
		    default=False,
		    help="Do not execute command"),
	    scatter.cmd.Option("-s","--silent",action="store_true",
		    dest="do_silent",
		    default=False,
		    help="Do not print nodename"),
	    scatter.cmd.Option("-q","--quiet",action="store_true",
		    dest="do_quiet",
		    default=False,
		    help="Do not print anymore"),
	    scatter.cmd.Option("-e","--execcmd",action="store_const",
		    dest="execcmd",
		    default='/usr/local/bin/trsh',
		    help="Command name for execution. default is ssh"),
	    scatter.cmd.Option("-t","--type",action="store",
		    type="choice", choices=['broad','seq', 'intract', 'eval'],
		    dest="type",
		    default="seq",
		    help="Execution mode. type=<broad|seq|intract|eval>"),
	    scatter.cmd.Option("-c","--comment",action="store",
		    dest="comment",
		    help="Print string as comment"),
	    scatter.cmd.Option("-u","--user",action="store",
		    dest="user",
		    help="Set the username for remote host"),
	    ]

        scatter.cmd.Application.__init__(self,argv)

    def usageHeader(self):
        sys.stderr.write(
	"%s -- Executes the specified command(s) at the remote host.\n"
	"Version: %s \n"
	"Copyright (C) 2002-2007 %s\n\n"
	% (self.prog, __version__, __author__)
        )

    def run(self):
        self.batch = scatter.shell.ScatterRun(self.options.execcmd,
			self.options.user, self.options.type)
        self.batch.setColorMode(self.options.do_mono)
        self.batch.setPrintmode(self.options.do_silent, self.options.do_quiet)
        self.batch.setVerbose(self.options.do_verbose)
        self.batch.setComment(self.options.comment)

        self.batch.buildScript(self.nodes.nodes, string.join(self.args))
        self.batch.writeOut()

        for l in range(self.options.loopmax):
            if self.options.do_dryrun:
                self.batch.showScript()
                break
            else:
                self.batch.doScript()
		if self.options.interval != 0:
                    time.sleep( self.options.interval )

        if self.options.do_copy:
            self.batch.copyFiles( os.getcwd() )
        if not self.options.do_debug:
            self.batch.close()


if __name__ == '__main__':
    app = App()
    app.run()
