#!/usr/bin/python3

import argparse
import os
import shutil

import filer

desc = 'fix gz in files that have .gz'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('-v', action='store_true', help='raise verbosity')
parser.add_argument('files', metavar='bnp', type=str, nargs='+', help='files')
args = parser.parse_args()
files = args.files

do_verbose = args.v
for the_file in files:
    if not os.path.isfile(the_file):
        if do_verbose:
            print(f"I don't see {the_file}")
        continue
    if not the_file.endswith('.gz'):
        if do_verbose:
            print(f"I skip {the_file}")
        continue
    if filer.is_gz(the_file):
        if do_verbose:
            print(f"{the_file} is gzipped.")
        continue
    ## looks like we need to rename the file
    the_file_without_gz = the_file[:-3]
    if os.path.isfile(the_file_without_gz):
        print(f"I have {the_file_without_gz} and {the_file}")
        continue
    shutil.copy2(the_file, the_file_without_gz)
    with open(the_file_without_gz, "r") as read_file:
        string = read_file.read()
    filer.srite(the_file, string, do_change_check=False,
                do_preserve_time=True)
    os.remove(the_file_without_gz)
quit()
