#!python

import sys, os, re

skipwords = "cd rm ls mv cp \
exit source echo function set unset let export \
man printenv \
conda mamba pip apt \
more less time \
code nano vi emacs gedit"

skipwords = skipwords.split()

nonskipwords = "r"

nonskipwords = nonskipwords.split()

# print(sys.argv,file=sys.stderr)

SENTINEL = "@@@"

args = sys.argv[1:]
seamless_run_opts = ""
for n in range(len(args) - 1, -1, -1):
    if args[n] == SENTINEL:
        words = args[:n]
        seamless_run_opts = " ".join(args[n + 1 :])
        break
else:
    print(" ".join(args))
    exit(0)

if len(seamless_run_opts):
    seamless_run_opts = " " + seamless_run_opts

if len(words) == 0:
    print("")
    exit(0)

line = " ".join(words)

if words[0].startswith("seamless"):
    print(line)
    exit(0)

if words[0] in skipwords:
    print(line)
    exit(0)

if len(words[0]) <= 2 and words[0] not in nonskipwords:
    print(line)
    exit(0)

if len(words) == 1:
    ww = words[0].split("=")
    if len(ww) == 2 and ww[0].isalnum() and ww[1].isalnum():
        print(line)
        exit(0)

for n, word in enumerate(words):
    if word.endswith(".CHECKSUM"):
        words[n] = word[: -len(".CHECKSUM")]
    elif word.endswith(".INDEX"):
        words[n] = word[: -len(".INDEX")]


for n in range(len(words) - 1, 1, -1):
    sep = words[n].strip()
    if sep in ("\n", ";", "&&", "||"):
        command = words
        break
else:
    command = words

command = " ".join(command)
command = command.replace("'", "'\\''")
quote = "'"

if command[-1].endswith("'"):
    command += " "
result = "seamless-run" + seamless_run_opts + " -c " + quote + command + quote
print(result)
