#!/usr/bin/env python

import sys
import shlex
from subprocess import run, STDOUT
from time import perf_counter
from textwrap import wrap

args = sys.argv[1:]

bkt_args = []
cmd_args = args

if '--' in args:
   at = args.index('--')
   bkt_args, cmd_args = args[:at], args[at+1:]

bkt_args = ["--ttl", "1y", "--cache-dir", ".cache"] + bkt_args

def wrap_bash_command(cmd):
    """Given args as a list of string, wraps
    the args to look like a bash cmd."""

    cmd = [arg.replace(" ", " ") for arg in cmd]
    lines = wrap(shlex.join(cmd), width=60)
    lines = [line.replace(" ", " ") for line in lines]
    return " \\\n> ".join(lines)



before = perf_counter()
print(f"$ {shlex.join(cmd_args)}", file=sys.stderr)
print(f"$ {wrap_bash_command(cmd_args)}", flush=True)
run(["bkt"] + bkt_args + ["--"] + cmd_args, stderr=STDOUT)
after = perf_counter()

print(f"$ {shlex.join(cmd_args)}  # took {after-before:.2f}s\n", file=sys.stderr)
