#!/usr/bin/python3

# ## {R2D2919B742E} ##
# ###########################################################################
# What if magic existed?
# What if a place existed where your every thought and dream come to life.
# There is only one catch: it has to be written down.
# Such a place exists, it is called programming.
#    - Scott Taylor Reph, RightThumb.com
# ###########################################################################
# ## {C3P0D40fAe8B} ##
##################################################
import sys, time
##################################################
import _rightThumb._construct as __
appDBA=__.clearFocus(__name__,__file__);__.appReg=appDBA;
def focus(parentApp='',childApp='',reg=True):
    global appDBA;f=__.appName(appDBA,parentApp,childApp);
    if reg:__.appReg=f;
    return f
import _rightThumb._base3 as _
fieldSet=_.l.vars(focus(),__name__,__file__,appDBA)
_.load()
##################################################
_v = __.imp('_rightThumb._vars')
_str = __.imp('_rightThumb._string')
##################################################
def sw():
    pass
    _.switches.register( 'Local', '-l,-local' )

# __.setting('require-list',['Pipe','Files','Plus'])
__.setting('require-list',[])
__.setting('receipt-log')
__.setting('receipt-file')
__.setting('myFileLocations-skip-validation',False)
__.setting('require-pipe',False)
__.setting('require-pipe||file',False)
__.setting('pre-error',False)
__.setting('switch-raw',[])


_.appInfo[focus()] = {
    # 'app': '8facG-jo0Cxk',
    'file': 'ssh-keys.py',
    'liveAppName': __.thisApp( __file__ ),
    'description': 'Changes the world',
        # _.ail(1,'subject')+
        # _.aib('one')+
    'categories': [
                        'DEFAULT',
                ],
    'usage': [
                        # 'epy another',
                        # 'e nmap',
                        # '',
    ],
    'relatedapps': [
                        # 'p another -file file.txt',
                        # '',
    ],
    'prerequisite': [
                        # 'p another -file file.txt',
                        # '',
    ],
    'examples': [
                        _.hp('p thisApp -file file.txt'),
                        _.linePrint(label='simple',p=0),
                        '',
    ],
    'columns': [
                       # { 'name': 'name', 'abbreviation': 'n' },
                       # { 'name': '{1}', 'abbreviation': '{0}', 'sort': '{2}' },
    ],
    'aliases': [
                       # 'this',
                       # 'app',
    ],
    'notes': [
                       # {},
    ],
}

_.appData[focus()] = {
        'start': __.startTime,
        'uuid': '',
        'audit': [],
        'pipe': False,
        'data': {
                    'field': {'sent': [], 'received': [] }, # { 'label': '', 'context': [],  }
                    'table': {'sent': [], 'received': [] },
        },
    }


def triggers():
    _.switches.trigger( 'Files', _.myFileLocations, vs=True )
    _.switches.trigger( 'Ago', _.timeAgo )
    _.switches.trigger( 'Folder', _.myFolderLocations )
    _.switches.trigger( 'URL', _.urlTrigger )
    _.switches.trigger( 'Duration', _.timeFuture )

_.l.conf('clean-pipe',True)
_.l.sw.register( triggers, sw )

########################################################################################


########################################################################################
# START

def action():
    file=''
    if _.switches.isActive('Local'):
        file='/mnt/c/Users/Scott/.ssh/id_rsa.pub'
        subprocess.Popen( 'sudo rm -rf /mnt/c/Users/Scott/.ssh'.replace('  ',' ').split(' ') ,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE)
        subprocess.Popen( 'sudo mkdir /mnt/c/Users/Scott/.ssh'.replace('  ',' ').split(' ') ,
                                     stdin=subprocess.PIPE,
                                     stdout=subprocess.PIPE,
                                     stderr=subprocess.PIPE)
        
    else:
        file='~/.ssh/id_rsa.pub'
    subprocess.Popen( 'rm -rf ~/.ssh'.replace('  ',' ').split(' ') ,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)

    subprocess.Popen( 'mkdir ~/.ssh'.replace('  ',' ').split(' ') ,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)

    # dic={}
    # kk=process.__dict__.keys()
    # for k in kk:
    #     dic[str(k)]=str(process.__dict__[k])
    # _.pv(dic)
    # sys.exit()
    process2=subprocess.Popen( 'ssh-keygen -t rsa'.replace('  ',' ').split(' ') ,
                             stdin=subprocess.PIPE,
                             stdout=subprocess.PIPE,
                             stderr=subprocess.PIPE)
    
    # _.pr(dir(process2))
    inputdata=file; stdoutdata,stderrdata=process2.communicate(input=inputdata.encode('utf-8'));
    # _.pr(stdoutdata,stderrdata)
    inputdata=''; stdoutdata,stderrdata=process2.communicate(input=inputdata.encode('utf-8'));
    inputdata=''; stdoutdata,stderrdata=process2.communicate(input=inputdata.encode('utf-8'));
    inputdata=''; stdoutdata,stderrdata=process2.communicate(input=inputdata.encode('utf-8'));

    users = {
                'admin@hoth.m-eta.app': _vault.imp.s.de('ZvPPCMvS/YCXyf5bX4hZJQ=='),
                'scott@hoth.m-eta.app': _vault.imp.s.de('ctdRBs9pXfVz/WTQV5dr9A=='),
                'root@hoth.m-eta.app': _vault.imp.s.de('ctdRBs9pXfVz/WTQV5dr9A=='),

                'scott@coruscant.m-eta.app': _vault.imp.s.de('ctdRBs9pXfVz/WTQV5dr9A=='),
                'root@coruscant.m-eta.app': _vault.imp.s.de('ctdRBs9pXfVz/WTQV5dr9A=='),

    }

    dat={}
    for us in users:
        time.sleep(.5)
        uuid=_.UUID()
        cmd='ssh-copy-id -i '+file+' '+us
        dat[uuid]=subprocess.Popen( cmd.split(' ') ,
                                 stdin=subprocess.PIPE,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE)
        inputdata=users[us]; stdoutdata,stderrdata=dat[uuid].communicate(input=inputdata.encode('utf-8'));
    # _.pr(stderrdata)

import subprocess
_vault = _.regImp( __.appReg, '_rightThumb._vault' )

########################################################################################
if __name__ == '__main__':
    action()
    __.isExit()




