#!/usr/bin/env python

import argparse
import logging
import os
import sys

logging.basicConfig(format="%(levelname)s:%(message)s", level=logging.DEBUG)


parser = argparse.ArgumentParser()
parser.add_argument("-i", dest="fileno", required=True)
parser.add_argument("-j", dest="ptypy_configfile", required=True)
parser.add_argument("-p", dest="propagate", required=True)
args, remainder = parser.parse_known_args()

print("", file=sys.stderr)
print("conda activate stuff", file=sys.stderr)

print("", file=sys.stderr)
print("ptypy setup stuff", file=sys.stderr)

# Test wants an error in the log file?
if args.propagate == "bad_propagate":
    # Emit a realistic message that can appear in the stdout.
    print(
        "FileNotFoundError: [Errno 2] Unable to open file (unable to open file: name = '//dls/i14/data/2022/cm31135-3/scan/i14-177938999.nxs', errno = 2, error message = 'No such file or directory', flags = 0, o_flags = 0)",
        file=sys.stderr,
    )
    # Emit 0 return code even though there was an error.
    sys.exit(0)

print("other stuff", file=sys.stderr)

with open("ptypy.log", "w") as stream:
    stream.write("this is ptypy.log")

os.mkdir(f"scan_{args.fileno}")

with open(f"scan_{args.fileno}/scan_{args.fileno}.ptyr", "w") as stream:
    stream.write("-")

with open(f"scan_{args.fileno}/scan_{args.fileno}_propagated.nxs", "w") as stream:
    stream.write("-")

sys.exit(0)
