//! Steamless - SteamStub DRM Remover (CLI)
//!
//! Command-line tool to unpack SteamStub DRM-protected PE executable files.
//! Uses libsteamless for the core unpacking logic.

use clap::Parser;
use libsteamless::{process_file, SteamlessOptions, LogLevel};

/// Steamless - SteamStub DRM Remover
///
/// Removes SteamStub DRM protection from 32-bit Windows PE executable files.
/// Supports variants 1.0, 2.0, 2.1, 3.0, and 3.1 (x86).
#[derive(Parser, Debug)]
#[command(name = "steamless")]
#[command(version = "0.1.0")]
#[command(about = "Steamless - SteamStub DRM Remover", long_about = None)]
struct Cli {
    /// Path to the packed executable file
    file: String,

    /// Disable verbose/debug output
    #[arg(long, short = 'q')]
    quiet: bool,

    /// Keep the .bind section in the unpacked file
    #[arg(long)]
    keepbind: bool,

    /// Keep the DOS stub in the unpacked file (don't zero it)
    #[arg(long)]
    keepstub: bool,

    /// Dump the stub payload data to disk
    #[arg(long)]
    dumppayload: bool,

    /// Dump the SteamDRMP.dll to disk
    #[arg(long)]
    dumpdrmp: bool,

    /// Realign the unpacked file sections
    #[arg(long)]
    realign: bool,

    /// Recalculate the unpacked file checksum
    #[arg(long)]
    recalcchecksum: bool,

    /// Use experimental features
    #[arg(long)]
    exp: bool,
}

fn main() -> anyhow::Result<()> {
    let cli = Cli::parse();

    let mut options = SteamlessOptions::default();
    options.verbose_output = !cli.quiet;
    options.keep_bind_section = cli.keepbind;
    options.dump_payload_to_disk = cli.dumppayload;
    options.dump_steam_drmp_to_disk = cli.dumpdrmp;
    options.use_experimental_features = cli.exp;
    options.dont_realign_sections = !cli.realign;
    options.zero_dos_stub_data = !cli.keepstub;
    options.recalculate_file_checksum = cli.recalcchecksum;

    // Print header
    print_header();

    // Set up logging
    let log = |level: LogLevel, msg: &str| {
        let prefix = match level {
            LogLevel::Error => "[!]",
            LogLevel::Warning => "[-]",
            LogLevel::Success => "[+]",
            LogLevel::Information => "[*]",
            LogLevel::Debug => "[#]",
        };

        // Skip debug messages if not verbose
        if !options.verbose_output && matches!(level, LogLevel::Debug) {
            return;
        }

        println!("{} {}", prefix, msg);
    };

    // Process the file
    process_file(&cli.file, &options, &log)
}

/// Print the Steamless banner.
fn print_header() {
    println!("  _________ __                        .__                        ");
    println!(" /   _____//  |_  ____ _____    _____ |  |   ____   ______ ______");
    println!(" \\_____  \\\\   __\\/ __ \\\\__  \\  /     \\|  | _/ __ \\ /  ___//  ___/");
    println!(" /        \\|  | \\  ___/ / __ \\|  Y Y  \\  |_\\  ___/ \\___ \\ \\___ \\ ");
    println!("/_______  /|__|  \\___  >____  /__|_|  /____/\\___  >____  >____  >");
    println!("        \\/           \\/     \\/      \\/          \\/     \\/     \\/ ");
    println!();
    println!("Steamless - SteamStub DRM Remover");
    println!("by atom0s");
    println!();
    println!("GitHub    : https://github.com/atom0s/Steamless");
    println!();
}

