#!/usr/bin/python3

import sys
import argparse
from SystemLib import ogHelp
from FileLib import ogCopyFile

if 2 == len (sys.argv) and 'help' == sys.argv[1]:
    #parser.print_help() sale en inglés aunque la locale indique otra cosa
    ogHelp ('ogCopyFile', 'ogCopyFile [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target', ['ogCopyFile REPO newfile.txt 1 2 /tmp/newfile.txt'])
    sys.exit (0)

parser = argparse.ArgumentParser (add_help=False)
if 5 == len (sys.argv):
    parser.add_argument ('src_container')
    parser.add_argument ('src_file')
    parser.add_argument ('dst_container')
    parser.add_argument ('dst_file')
    args = parser.parse_args()
    src = { 'container': args.src_container, 'file': args.src_file }
    dst = { 'container': args.dst_container, 'file': args.dst_file }
elif 7 == len (sys.argv):
    parser.add_argument ('src_disk')
    parser.add_argument ('src_par')
    parser.add_argument ('src_file')
    parser.add_argument ('dst_disk')
    parser.add_argument ('dst_par')
    parser.add_argument ('dst_file')
    args = parser.parse_args()
    src = { 'disk': args.src_disk, 'par': args.src_par, 'file': args.src_file }
    dst = { 'disk': args.dst_disk, 'par': args.dst_par, 'file': args.dst_file }
elif 6 == len (sys.argv):
    ## can be either:
    ## - src_disk src_par src_file    dst_container dst_file
    ## - src_container src_file       dst_disk dst_par dst_file

    try:
        num = int (sys.argv[1])     ## raises ValueError if sys.argv[1] doesn't look like a number
        ## "src_disk src_par src_file    dst_container dst_file"
        parser.add_argument ('src_disk')
        parser.add_argument ('src_par')
        parser.add_argument ('src_file')
        parser.add_argument ('dst_container')
        parser.add_argument ('dst_file')
        args = parser.parse_args()
        src = { 'disk': args.src_disk, 'par': args.src_par, 'file': args.src_file }
        dst = { 'container': args.dst_container, 'file': args.dst_file }
    except:
        ## "src_container src_file       dst_disk dst_par dst_file"
        parser.add_argument ('src_container')
        parser.add_argument ('src_file')
        parser.add_argument ('dst_disk')
        parser.add_argument ('dst_par')
        parser.add_argument ('dst_file')
        args = parser.parse_args()
        src = { 'container': args.src_container, 'file': args.src_file }
        dst = { 'disk': args.dst_disk, 'par': args.dst_par, 'file': args.dst_file }
else:
    ogHelp ('ogCopyFile', 'ogCopyFile [ str_repo | int_ndisk int_npartition ] path_source [ str_repo | int_ndisk int_npartition ] path_target', ['ogCopyFile REPO newfile.txt 1 2 /tmp/newfile.txt'])
    sys.exit (1)

ret = ogCopyFile (src, dst)
if ret is not None:
    if   ret == True: sys.exit (0)
    elif ret == False: sys.exit (1)
    else: print (ret)
