/*
 * Copyright (c) 2022, Umut İnan Erdoğan <umutinanerdogan@pm.me>
 * Copyright (c) 2022, able <abl3theabove@gmail.com>
 *
 * SPDX-License-Identifier: MPL-2.0
 */

use std::{fs::{self, File}, process::Command};

use colored::*;

struct Options {
    pub subcommand: Subcommand,
    pub arguments: Vec<String>,
}

enum Subcommand {
    BuildImage,
    Doc,
    Help,
    Run,
    Empty,
    /// Run all tests for all architectures
    Test,
    Unknown(String),
}

impl Subcommand {
    fn from_str<S: AsRef<str>>(str: S) -> Subcommand {
        match str.as_ref() {
            "build-image" => Subcommand::BuildImage,
            "doc" => Subcommand::Doc,
            "help" => Subcommand::Help,
            "run" | "r" => Subcommand::Run,
            "test" | "t" => Subcommand::Test,
            "" => Subcommand::Empty,
            unknown => Subcommand::Unknown(unknown.to_string()),
        }
    }
}

enum MachineType {
    X86_64,
    RiscV64,
    AArch64,
    Unknown(String),
}

fn main() {
    let options = options();

    match options.subcommand {
        Subcommand::BuildImage => {
            let machine_text = options.arguments.get(0).cloned().unwrap_or_default();

            match machine(machine_text) {
                MachineType::X86_64 => {
                    // Cleanup
                    // NOTE: we are not unwrapping these, as we don't want this to fail if they
                    //       don't exist yet, probably not the best idea tho.
                    // FIXME: figure out a better way to ignore errors about these not existing
                    #[allow(unused_must_use)]
                    {
                        fs::remove_dir_all("./limine");
                        fs::remove_dir_all("./disk");
                        fs::remove_file("./disk.img");
                    }

                    // Build ableOS in release mode
                    Command::new("cargo")
                        .args(["build", "--release"])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();

                    // Create disk directory
                    fs::create_dir("./disk").unwrap();

                    // Clone limine 3.X binaries
                    Command::new("git")
                        .arg("clone")
                        .arg("https://github.com/limine-bootloader/limine.git")
                        .arg("--branch=v3.0-branch-binary")
                        .arg("--depth=1")
                        .status()
                        .unwrap();

                    println!("{}", "Building limine".bold());
                    Command::new("make")
                        .args(["-C", "limine"])
                        .status()
                        .unwrap();

                    println!("{}", "Allocating new disk image".bold());
                    Command::new("dd")
                        .args(["if=/dev/zero", "of=disk.img", "count=1", "bs=256MiB"])
                        .status()
                        .unwrap();

                    println!("{}", "Partitioning disk image".bold());
                    Command::new("sudo")
                        .args(["sfdisk", "disk.img"])
                        .stdin(File::open("./sfdisk-config").unwrap())
                        .status()
                        .unwrap();

                    // Setup loopback device for disk.img, with partitions
                    // FIXME: don't assume that /dev/loop0 is empty
                    Command::new("sudo")
                        .arg("losetup")
                        .args(["-P", "/dev/loop0", "disk.img"])
                        .status()
                        .unwrap();

                    println!("{}", "Creating ext2 filesystem on /dev/loop0p1".bold());
                    Command::new("sudo")
                        .arg("mkfs.ext2")
                        .arg("/dev/loop0p1")
                        .status()
                        .unwrap();

                    println!("{}", "Mounting filesystem".bold());
                    Command::new("sudo")
                        .arg("mount")
                        .args(["/dev/loop0p1", "./disk"])
                        .status()
                        .unwrap();

                    // copy ./base/* over to ./disk
                    Command::new("sudo")
                        .args(["sh", "-c"])
                        .arg("cp -r ./base/* ./disk")
                        .status()
                        .unwrap();

                    // copy ./limine/limine.sys over to ./disk/boot
                    Command::new("sudo")
                        .arg("cp")
                        .args(["./limine/limine.sys", "./disk/boot"])
                        .status()
                        .unwrap();

                    // copy the kernel over to ./disk/boot/kernel
                    Command::new("sudo")
                        .arg("cp")
                        .arg("./target/x86_64-ableos/release/ableos")
                        .arg("./disk/boot/kernel")
                        .status()
                        .unwrap();

                    // TODO: build the userspace programs and copy them over

                    // unmount the fs
                    Command::new("sudo")
                        .arg("umount")
                        .arg("./disk")
                        .status()
                        .unwrap();

                    // remove the loopback device
                    Command::new("sudo")
                        .arg("losetup")
                        .args(["-d", "/dev/loop0"])
                        .status()
                        .unwrap();

                    println!("{}", "Deploying limine".bold());
                    Command::new("./limine/limine-deploy")
                        .arg("./disk.img")
                        .status()
                        .unwrap();
                }
                MachineType::Unknown(unknown) => {
                    eprintln!(
                        "{}: unknown machine type `{}`",
                        "error".red().bold(),
                        unknown.bold(),
                    );
                    eprintln!("expected one of x86_64, riscv64 or aarch64");
                }
                _ => {
                    eprintln!(
                        "{}: build-image not implemented for this machine type",
                        "error".red().bold(),
                    );
                }
            }
        }
        Subcommand::Test => {
            Command::new("cargo")
                .args(["test", "--target=json_targets/x86_64-ableos.json"])
                .current_dir(fs::canonicalize("./ableos").unwrap())
                .status()
                .unwrap();

            // panic!("Test Infrastructure missing");
        }
        Subcommand::Doc => {
            let machine_text = options.arguments.get(0).cloned().unwrap_or_default();

            match machine(machine_text) {
                MachineType::X86_64 => {
                    Command::new("cargo")
                        .args(["doc", "--open"])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();
                }
                MachineType::RiscV64 => {
                    Command::new("cargo")
                        .args(["doc", "--open", "--target=riscv64gc-unknown-none-elf"])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();
                }
                MachineType::AArch64 => {
                    Command::new("cargo")
                        .args(["doc", "--open", "--target=json_targets/aarch64-ableos.json"])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();
                }
                MachineType::Unknown(unknown) => {
                    eprintln!(
                        "{}: unknown machine type `{}`",
                        "error".red().bold(),
                        unknown.bold(),
                    );
                    eprintln!("expected one of x86_64, riscv64 or aarch64");
                }
            }
        }
        Subcommand::Help => help(),
        Subcommand::Run => {
            let machine_text = options.arguments.get(0).cloned().unwrap_or_default();
            let debug = options.arguments.get(1).cloned().unwrap_or_default();
            let debug = matches!(debug.as_str(), "--debug" | "--dbg" | "-d");

            match machine(machine_text) {
                MachineType::X86_64 if debug => {
                    // Build ableOS
                    Command::new("cargo")
                        .arg("build")
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();

                    // Setup loopback device for disk.img, with partitions
                    // FIXME: don't do ths if running without changes
                    Command::new("sudo")
                        .arg("losetup")
                        .args(["-P", "/dev/loop0", "disk.img"])
                        .status()
                        .unwrap();

                    // mount the root fs to ./disk
                    Command::new("sudo")
                        .arg("mount")
                        .args(["/dev/loop0p1", "./disk"])
                        .status()
                        .unwrap();
                    
                     // copy the kernel over to ./disk/boot/kernel
                     Command::new("sudo")
                        .arg("cp")
                        .arg("./target/x86_64-ableos/debug/ableos")
                        .arg("./disk/boot/kernel")
                        .status()
                        .unwrap();

                    // unmount the root fs
                    Command::new("sudo")
                        .arg("umount")
                        .arg("./disk")
                        .status()
                        .unwrap();

                    // remove loopback device
                    Command::new("sudo")
                        .arg("losetup")
                        .args(["-d", "/dev/loop0"])
                        .status()
                        .unwrap();

                    // run qemu with "-S", "-gdb", "tcp:9000"
                    Command::new("qemu-system-x86_64")
                        .args(["-device", "piix4-ide,id=ide"])
                        .arg("-drive")
                        .arg("file=./disk.img,format=raw,if=none,id=disk")
                        .args(["-device", "ide-hd,drive=disk,bus=ide.0"])
                        // .arg("--nodefaults")
                        .args(["-cpu", "Broadwell-v3"])
                        .args(["-m", "4G"])
                        .args(["-serial", "stdio"])
                        .args(["-smp", "cores=2"])
                        .args(["-soundhw", "pcspk"])
                        // .args(["-device", "VGA"])
                        // .args(["-device", "virtio-gpu-pci"])
                        .args(["-device", "vmware-svga"])
                        .args(["-device", "sb16"])
                        // .args(["-machine", "pcspk-audiodev=0"])
                        // .args(["-qmp", "unix:../qmp-sock,server,nowait"])
                        .args(["-S", "-gdb", "tcp:9000"])
                        .status()
                        .unwrap();
                }
                MachineType::X86_64 => {
                    // Build ableOS
                    Command::new("cargo")
                        .args(["build", "--release"])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();

                    // Setup loopback device for disk.img, with partitions
                    // FIXME: don't do ths if running without changes
                    Command::new("sudo")
                        .arg("losetup")
                        .args(["-P", "/dev/loop0", "disk.img"])
                        .status()
                        .unwrap();

                    // mount the root fs to ./disk
                    Command::new("sudo")
                        .arg("mount")
                        .args(["/dev/loop0p1", "./disk"])
                        .status()
                        .unwrap();
                    
                     // copy the kernel over to ./disk/boot/kernel
                     Command::new("sudo")
                        .arg("cp")
                        .arg("./target/x86_64-ableos/release/ableos")
                        .arg("./disk/boot/kernel")
                        .status()
                        .unwrap();

                    // unmount the root fs
                    Command::new("sudo")
                        .arg("umount")
                        .arg("./disk")
                        .status()
                        .unwrap();

                    // remove loopback device
                    Command::new("sudo")
                        .arg("losetup")
                        .args(["-d", "/dev/loop0"])
                        .status()
                        .unwrap();

                    // run qemu
                    Command::new("qemu-system-x86_64")
                        .args(["-device", "piix4-ide,id=ide"])
                        .arg("-drive")
                        .arg("file=./disk.img,format=raw,if=none,id=disk")
                        .args(["-device", "ide-hd,drive=disk,bus=ide.0"])
                        // .arg("--nodefaults")
                        .args(["-cpu", "Broadwell-v3"])
                        .args(["-m", "4G"])
                        .args(["-serial", "stdio"])
                        .args(["-smp", "cores=2"])
                        .args(["-soundhw", "pcspk"])
                        // .args(["-device", "VGA"])
                        // .args(["-device", "virtio-gpu-pci"])
                        .args(["-device", "vmware-svga"])
                        .args(["-device", "sb16"])
                        // .args(["-machine", "pcspk-audiodev=0"])
                        // .args(["-qmp", "unix:../qmp-sock,server,nowait"])
                        .status()
                        .unwrap();
                }
                MachineType::RiscV64 if debug => {
                    eprintln!(
                        "{}: debug is not implemented for riscv64",
                        "error".red().bold()
                    );
                }
                MachineType::RiscV64 => {
                    Command::new("cargo")
                        .args(["build", "--release", "--target=riscv64gc-unknown-none-elf"])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();

                    Command::new("qemu-system-riscv64")
                        .args(["-machine", "virt"])
                        .args(["-cpu", "rv64"])
                        .args(["-smp", "8"])
                        .args(["-m", "128M"])
                        .arg("-bios")
                        .arg("src/arch/riscv/firmwear/opensbi-riscv64-generic-fw_jump.bin")
                        .arg("-kernel")
                        .arg("target/riscv64gc-unknown-none-elf/release/ableos")
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();
                }
                MachineType::AArch64 if debug => {
                    eprintln!(
                        "{}: debug is not implemented for aarch64",
                        "error".red().bold()
                    );
                }
                MachineType::AArch64 => {
                    Command::new("cargo")
                        .args([
                            "build",
                            "--release",
                            "--target=json_targets/aarch64-ableos.json",
                        ])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();

                    Command::new("qemu-system-aarch64")
                        .args(["-machine", "virt"])
                        .args(["-m", "1024M"])
                        .args(["-cpu", "cortex-a53"])
                        .args(["-kernel", "target/aarch64-ableos/release/ableos"])
                        .args(["-device", "virtio-keyboard"])
                        .current_dir(fs::canonicalize("./ableos").unwrap())
                        .status()
                        .unwrap();
                }
                MachineType::Unknown(unknown) => {
                    eprintln!(
                        "{}: unknown machine type `{}`",
                        "error".red().bold(),
                        unknown.bold(),
                    );
                    eprintln!("expected one of x86_64, riscv64 or aarch64");
                }
            }
        }
        Subcommand::Empty => {
            eprintln!("{}: no subcommand passed", "error".red().bold());
            help();
        }
        Subcommand::Unknown(unknown) => {
            eprintln!(
                "{}: unknown subcommand `{}`",
                "error".red().bold(),
                unknown.bold()
            );
            help();
        }
    }
}

fn options() -> Options {
    let subcommand = std::env::args().nth(1).unwrap_or_default();
    let arguments = std::env::args().skip(2).collect();

    Options {
        subcommand: Subcommand::from_str(subcommand),
        arguments,
    }
}

fn machine<S: AsRef<str>>(text: S) -> MachineType {
    match text.as_ref() {
        "x86" | "x86_64" => MachineType::X86_64,
        "riscv" | "riscv64" => MachineType::RiscV64,
        "arm" | "arm64" | "aarch64" => MachineType::AArch64,
        "" => {
            eprintln!(
                "{}: no machine type passed, defaulting to x86_64",
                "warning".yellow().bold()
            );
            MachineType::X86_64
        }
        unknown => MachineType::Unknown(unknown.to_string()),
    }
}

fn help() {
    todo!("`help`")
}