#!/usr/bin/env python3

# Copyright (c) 2017-2023 California Institute of Technology ("Caltech"). U.S.
# Government sponsorship acknowledged. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0


r'''Converts a .cahvor-formatted camera model to the .cameramodel file format

SYNOPSIS

  $ mrcal-from-cahvor model1.cahvor model2.cahvor
  Wrote model1.cameramodel
  Wrote model2.cameramodel

File formats supported by mrcal are described at
http://mrcal.secretsauce.net/cameramodels.html#cameramodel-file-formats

This tool converts the given model(s) to the cameramodel file format. No changes
to the content are made; this is purely a format converter (the
mrcal-convert-lensmodel tool fits different lens models instead). Model
filenames are given on the commandline. Output is written to the same directory,
with the same filename, but with a .cameramodel extension.

If the model is omitted or given as "-", the input is read from standard input,
and the output is written to standard output

'''

import sys
import argparse
import re
import os

def parse_args():

    parser = \
        argparse.ArgumentParser(description = __doc__,
                                formatter_class=argparse.RawDescriptionHelpFormatter)

    parser.add_argument('--force', '-f',
                        action='store_true',
                        default=False,
                        help='''By default existing files are not overwritten. Pass --force to overwrite them
                        without complaint''')
    parser.add_argument('--outdir',
                        required=False,
                        type=lambda d: d if os.path.isdir(d) else \
                                parser.error("--outdir requires an existing directory as the arg, but got '{}'".format(d)),
                        help='''Directory to write the output models into. If omitted, we write the output
                        models to the same directory as the input models''')
    parser.add_argument('model',
                        default=['-'],
                        nargs='*',
                        type=str,
                        help='''Input camera model''')

    return parser.parse_args()

args = parse_args()

# arg-parsing is done before the imports so that --help works without building
# stuff, so that I can generate the manpages and README

Nstdin = sum(1 for m in args.model if m=='-')
if Nstdin > 1:
    print(f"At most one model can be read from standard input ('-'), but I got {Nstdin}", file=sys.stderr)
    sys.exit(1)

import mrcal

for model in args.model:
    if model == '-':
        try:
            m = mrcal.cameramodel(model)
        except KeyboardInterrupt:
            sys.exit(1)
        except Exception as e:
            print(e, file=sys.stderr)
            sys.exit(1)
        m.write(sys.stdout)
    else:
        base,extension = os.path.splitext(model)
        if extension.lower() == '.cameramodel':
            print("Input file is already in the cameramodel format (judging from the filename). Doing nothing",
                  file=sys.stderr)
            sys.exit(0)

        if args.outdir is not None:
            base = args.outdir + '/' + os.path.split(base)[1]
        filename_out = base + '.cameramodel'
        if not args.force and os.path.exists(filename_out):
            print(f"Target model '{filename_out}' already exists. Doing nothing with this model. Pass -f to overwrite",
                  file=sys.stderr)
        else:
            m = mrcal.cameramodel(model)
            m.write(filename_out)
            print("Wrote " + filename_out)
