import sys
import shutil
import subprocess
import argparse

def install_package(cmd):
    # try to install the command using pip
    print(f"'{cmd}' not found, installing via pip...")
    subprocess.run([sys.executable, "-m", "pip", "install", cmd], check=True)

def main():
    parser = argparse.ArgumentParser(description="Auto-installer CLI wrapper")
    parser.add_argument("command", nargs="?", help="Command to run")
    parser.add_argument("args", nargs=argparse.REMAINDER, help="Arguments for the command")
    parser.add_argument("-v", "--version", action="version", version="auto 0.1.0")
    opts = parser.parse_args()

    if not opts.command:
        parser.print_help()
        return

    cmd = opts.command
    args = opts.args

    # check if command exists
    if shutil.which(cmd) is None:
        install_package(cmd)

    # run the command with all arguments
    subprocess.run([cmd] + args)

