#!/usr/bin/python3

from __future__ import print_function

import cmd
import collections
import os
import sys

import pycdlib


class PyCdlibCmdLoop(cmd.Cmd):
    def __init__(self, iso):
        cmd.Cmd.__init__(self)
        self.iso = iso
        self.cwd = '/'
        self.rrcwd = '/'
        self.jolietcwd = '/'
        self.udfcwd = '/'
        self.print_mode = 'iso9660'

    prompt = '(pycdlib) '

    def help_exit(self):  # pylint: disable=no-self-use
        print("> exit")
        print("Exit the program.")

    def do_exit(self, line):  # pylint: disable=no-self-use
        if line:
            print("No parameters allowed for exit")
            return False
        return True

    def help_quit(self):  # pylint: disable=no-self-use
        print("> quit")
        print("Exit the program.")

    def do_quit(self, line):  # pylint: disable=no-self-use
        if line:
            print("No parameters allowed for quit")
            return False
        return True

    def do_EOF(self, line):  # pylint: disable=unused-argument,no-self-use
        print()
        return True

    def help_print_mode(self):  # pylint: disable=no-self-use
        print("> print_mode [iso9660|rr|joliet|udf]")
        print("Change which 'mode' of filenames are printed out.  There are four main\n"
              "modes: ISO9660 (iso9660, the default), Rock Ridge (rr), Joliet (joliet), and\n"
              "UDF (udf).  The original iso9660 mode only allows filenames of 8 characters,\n"
              "plus 3 for the extension.  The Rock Ridge extensions allow much longer\n"
              "filenames and much deeper directory structures.  The Joliet extensions also\n"
              "allow longer filenames and deeper directory structures, but in an entirely\n"
              "different namespace (though in most circumstances, the Joliet namespace will\n"
              "mirror the ISO9660/Rock Ridge namespace).  The UDF Bridge extensions add an\n"
              "entirely parallel UDF namespace to the ISO as well.  Any given ISO will always\n"
              "have ISO9660 mode, but may have any combination of Rock Ridge, Joliet, and UDF\n"
              "(including none of them).  Running this command with no arguments prints out\n"
              "the current mode.  Passing 'iso9660' as an argument sets it to the original\n"
              "ISO9660 mode.  Passing 'rr' as an argument sets it to Rock Ridge mode.  Passing\n"
              "'joliet' as an argument sets it to Joliet mode.  Passing 'udf' as an argument\n"
              "sets it to UDF mode.")

    def do_print_mode(self, line):  # pylint: disable=no-self-use
        split = line.split()
        splitlen = len(split)
        if splitlen == 0:
            print(self.print_mode)
            return False
        elif splitlen != 1:
            print("Only a single parameter allowed for print_mode")
            return False

        if split[0] not in ['iso9660', 'rr', 'joliet', 'udf']:
            print("Parameter for print_mode must be one of 'iso9660', 'rr', 'joliet', 'udf'")
            return False

        if split[0] == 'rr' and not self.iso.rock_ridge:
            print("Can only enable Rock Ridge names for Rock Ridge ISOs")
            return False

        if split[0] == 'joliet' and self.iso.joliet_vd is None:
            print("Can only enable Joliet names for Joliet ISOs")
            return False

        if split[0] == 'udf' and self.iso.udf_pvd is None:
            print("Can only enable UDF names for UDF ISOs")
            return False

        self.print_mode = split[0]

        return False

    def help_ls(self):  # pylint: disable=no-self-use
        print("> ls")
        print("Show the contents of the current working directory. The format of the output is:\n")
        print("TYPE(F=file, D=directory) NAME")

    def do_ls(self, line):  # pylint: disable=no-self-use
        if line:
            print("No parameters allowed for ls")
            return

        if self.print_mode == 'joliet':
            diriter = self.iso.list_children(joliet_path=self.jolietcwd)
        elif self.print_mode == 'rr':
            diriter = self.iso.list_children(rr_path=self.rrcwd)
        elif self.print_mode == 'udf':
            diriter = self.iso.list_children(udf_path=self.udfcwd)
        else:
            diriter = self.iso.list_children(iso_path=self.cwd)

        for child in diriter:
            if child is None:
                prefix = 'D'
                name = '..'
            else:
                prefix = "F"
                if child.is_dir():
                    prefix = "D"

                name = child.file_identifier().decode('utf-8')
                if self.print_mode == 'rr':
                    name = ""
                    if child.is_dot():
                        name = "."
                    elif child.is_dotdot():
                        name = ".."
                    else:
                        if child.rock_ridge is not None and child.rock_ridge.name() != "":
                            name = child.rock_ridge.name().decode('utf-8')
                            if child.rock_ridge.is_symlink():
                                name += " -> %s" % (child.rock_ridge.symlink_path())
                                prefix += "S"

            print("%2s %s" % (prefix, name))

        return False

    def help_cd(self):  # pylint: disable=no-self-use
        print("> cd <iso_dir>")
        print("Change directory to <iso_dir> on the ISO.")

    def do_cd(self, line):  # pylint: disable=no-self-use
        split = line.split()
        if len(split) != 1:
            print("The cd command supports one and only one parameter")
            return False

        directory = split[0]

        if directory == '/':
            tmp = '/'
        if self.print_mode == 'joliet':
            if directory != '/':
                tmp = os.path.normpath(os.path.join(self.jolietcwd, directory))
            rec = self.iso.get_record(joliet_path=tmp)
            if not rec.is_dir():
                print("Entry %s is not a directory" % (directory))
                return False
            self.jolietcwd = tmp
        elif self.print_mode == 'rr':
            if directory != '/':
                tmp = os.path.normpath(os.path.join(self.rrcwd, directory))
            rec = self.iso.get_record(rr_path=tmp)
            if not rec.is_dir():
                print("Entry %s is not a directory" % (directory))
                return False
            self.rrcwd = tmp
            self.cwd = os.path.normpath(os.path.join(self.cwd, rec.file_identifier().decode('utf-8')))
        elif self.print_mode == 'udf':
            if directory != '/':
                tmp = os.path.normpath(os.path.join(self.udfcwd, directory))
            rec = self.iso.get_record(udf_path=tmp)
            if not rec.is_dir():
                print("Entry %s is not a directory" % (directory))
                return False
            self.udfcwd = tmp
        else:
            if directory != '/':
                tmp = os.path.normpath(os.path.join(self.cwd, directory))
            rec = self.iso.get_record(iso_path=tmp)
            if not rec.is_dir():
                print("Entry %s is not a directory" % (directory))
                return False
            self.cwd = tmp
            if rec.rock_ridge is not None:
                self.rrcwd = os.path.normpath(os.path.join(self.rrcwd, rec.rock_ridge.name().decode('utf-8')))

        return False

    def help_get(self):  # pylint: disable=no-self-use
        print("> get <iso_file> <out_file>")
        print("Get the contents of <iso_file> from the ISO and write them to <out_file>.")

    def do_get(self, line):  # pylint: disable=no-self-use
        split = line.split()
        if len(split) != 2:
            print("The get command must be passed two parameters.")
            return False

        iso_file = split[0]
        outfile = split[1]

        if self.print_mode == 'joliet':
            if iso_file[0] == '/':
                path = iso_file
            else:
                path = os.path.join(self.jolietcwd, iso_file)
            self.iso.get_file_from_iso(outfile, joliet_path=path)
        elif self.print_mode == 'rr':
            if iso_file[0] == '/':
                path = iso_file
            else:
                path = os.path.join(self.rrcwd, iso_file)
            self.iso.get_file_from_iso(outfile, rr_path=path)
        elif self.print_mode == 'udf':
            if iso_file[0] == '/':
                path = iso_file
            else:
                path = os.path.join(self.udfcwd, iso_file)
            self.iso.get_file_from_iso(outfile, udf_path=path)
        else:
            if iso_file[0] == '/':
                path = iso_file
            else:
                path = os.path.join(self.cwd, iso_file)
            self.iso.get_file_from_iso(outfile, iso_path=path)

        return False

    def help_cwd(self):  # pylint: disable=no-self-use
        print("> cwd")
        print("Show the current working directory.")

    def do_cwd(self, line):  # pylint: disable=no-self-use
        if line:
            print("No parameters allowed for cwd")
            return False

        if self.print_mode == 'joliet':
            print(self.jolietcwd)
        elif self.print_mode == 'rr':
            print(self.rrcwd)
        elif self.print_mode == 'udf':
            print(self.udfcwd)
        else:
            print(self.cwd)

        return False

    def help_tree(self):  # pylint: disable=no-self-use
        print("> tree")
        print("Print all files and subdirectories below the current directory (similar to the Unix 'tree' command).")

    def do_tree(self, line):  # pylint: disable=no-self-use
        if line:
            print("No parameters allowed for tree")
            return False

        utf8_corner = "└──"
        utf8_middlebar = "├──"
        utf8_vertical_line = "│"

        if self.print_mode == 'joliet':
            entry = self.iso.get_record(joliet_path=self.jolietcwd)
        elif self.print_mode == 'rr':
            entry = self.iso.get_record(rr_path=self.rrcwd)
        elif self.print_mode == 'udf':
            entry = self.iso.get_record(udf_path=self.udfcwd)
        else:
            entry = self.iso.get_record(iso_path=self.cwd)

        dirs = collections.deque([(entry, [])])
        while dirs:
            dir_record, lasts = dirs.popleft()
            prefix = ""
            for index, last in enumerate(lasts):
                if last:
                    if index == (len(lasts) - 1):
                        prefix += utf8_corner + " "
                    else:
                        prefix += "    "
                else:
                    if index == (len(lasts) - 1):
                        prefix += utf8_middlebar + " "
                    else:
                        prefix += utf8_vertical_line + " "

            name = dir_record.file_identifier()
            if self.print_mode == 'rr':
                if dir_record.rock_ridge is not None and dir_record.rock_ridge.name() != "":
                    name = dir_record.rock_ridge.name()

            print("%s%s" % (prefix, name.decode('utf-8')))

            if dir_record.is_dir():
                tmp = []
                if self.print_mode == 'udf':
                    for d in dir_record.fi_descs:
                        child = d.file_entry
                        if child is None:
                            continue
                        tmp.append((child, lasts + [False]))
                else:
                    for child in dir_record.children:
                        if child.is_dot() or child.is_dotdot():
                            continue
                        tmp.append((child, lasts + [False]))
                tmp.pop()
                tmp.append((child, lasts + [True]))
                dirs.extendleft(reversed(tmp))

        return False

    def help_write(self):  # pylint: disable=no-self-use
        print("> write <out_file>")
        print("Write the current ISO contents to <out_file>.")

    def do_write(self, line):  # pylint: disable=no-self-use
        split = line.split()
        if len(split) != 1:
            print("The write command supports one and only one parameter.")
            return False

        out_name = split[0]

        self.iso.write(out_name)

        return False

    def help_add_file(self):  # pylint: disable=no-self-use
        print("> add_file <iso_path> <src_filename> [rr_name=<rr_name>] [joliet_path=<joliet_path>]")
        print("Add the contents of <src_filename> to the ISO at the location specified in <iso_path>.")
        print("If the ISO is a Rock Ridge ISO, <rr_name> must be specified; otherwise, it must not be.")
        print("If the ISO is not a Joliet ISO, <joliet_path> must not be specified.  If the ISO is a")
        print("Joliet ISO, <joliet_path> is optional, but highly recommended to supply.")

    def do_add_file(self, line):  # pylint: disable=no-self-use
        split = line.split()

        if len(split) < 2 or len(split) > 4:
            self.help_add_file()
            return False

        iso_path = split[0]
        src_path = split[1]
        rr_name = None
        joliet_path = None

        for arg in split[2:]:
            keyval = arg.split('=')
            if len(keyval) != 2:
                print("Invalid key/val pair, must be rr_name=<rr_name> or joliet_path=<joliet_path>")
                return False

            key = keyval[0]
            val = keyval[1]

            if key == 'rr_name':
                rr_name = val
            elif key == 'joliet_path':
                joliet_path = val
            else:
                print("Unknown key, must be rr_name=<rr_name> or joliet_path=<joliet_path>")
                return False

        if self.iso.rock_ridge and rr_name is None:
            print("The ISO is Rock Ridge, so a <rr_name> parameter must be specified.")
            return False

        if iso_path[0] != '/':
            iso_path = os.path.join(self.cwd, iso_path)

        self.iso.add_file(src_path, iso_path, rr_name=rr_name, joliet_path=joliet_path)

        return False

    def help_rm_file(self):  # pylint: disable=no-self-use
        print("> rm_file <iso_path>")
        print("Remove the contents of <iso_path> from the ISO.")

    def do_rm_file(self, line):  # pylint: disable=no-self-use
        split = line.split()
        if len(split) != 1:
            print("The rm_file command takes one and only one parameter (iso path).")
            return False

        iso_path = split[0]

        if iso_path[0] != '/':
            iso_path = os.path.join(self.cwd, iso_path)

        self.iso.rm_file(iso_path)

        return False

    def help_mkdir(self):  # pylint: disable=no-self-use
        print("> mkdir <iso_path> [rr_name=<rr_name>] [joliet_path=<joliet_path>]")
        print("Make a new directory called <iso_path>.")
        print("If the ISO is a Rock Ridge ISO, <rr_name> must be specified; otherwise, it must not be.")
        print("If the ISO is not a Joliet ISO, <joliet_path> must not be specified.  If the ISO is a")
        print("Joliet ISO, <joliet_path> is optional, but highly recommended to supply.")

    def do_mkdir(self, line):  # pylint: disable=no-self-use
        split = line.split()

        if len(split) < 1 or len(split) > 3:
            self.help_mkdir()
            return False

        iso_path = split[0]
        rr_name = None
        joliet_path = None

        for arg in split[1:]:
            keyval = arg.split('=')
            if len(keyval) != 2:
                print("Invalid key/val pair, must be rr_name=<rr_name> or joliet_path=<joliet_path>")
                return False

            key = keyval[0]
            val = keyval[1]

            if key == 'rr_name':
                rr_name = val
            elif key == 'joliet_path':
                joliet_path = val
            else:
                print("Unknown key, must be rr_name=<rr_name> or joliet_path=<joliet_path>")
                return False

        if self.iso.rock_ridge and rr_name is None:
            print("The ISO is Rock Ridge, so a <rr_name> parameter must be specified.")
            return False

        if iso_path[0] != '/':
            iso_path = os.path.join(self.cwd, iso_path)

        self.iso.add_directory(iso_path, rr_name=rr_name, joliet_path=joliet_path)

        return False

    def help_rmdir(self):  # pylint: disable=no-self-use
        print("> rmdir <iso_path>")
        print("Remove the directory at <iso_path>.  Note that the directory must be empty for the command to succeed.")

    def do_rmdir(self, line):  # pylint: disable=no-self-use
        split = line.split()
        if len(split) != 1:
            print("The rmdir command takes one and only one parameter (iso path).")
            return False

        iso_path = split[0]

        if iso_path[0] != '/':
            iso_path = os.path.join(self.cwd, iso_path)

        self.iso.rm_directory(iso_path)

        return False


def main():
    if len(sys.argv) != 2:
        print("Usage: %s <isofile>" % (sys.argv[0]))
        sys.exit(1)

    iso = pycdlib.PyCdlib()
    fp = open(sys.argv[1], 'rb')
    iso.open_fp(fp)

    done = False
    cmdloop = PyCdlibCmdLoop(iso)
    while not done:
        try:
            cmdloop.cmdloop()
            done = True
        except Exception as e:  # pylint: disable=broad-except
            print(e)

    iso.close()
    fp.close()


if __name__ == "__main__":
    main()
