#!/bin/python3
# -*- coding: utf-8 -*-
#
# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
# more information about the licensing of this file.

import os
import sys
import shutil
import argparse

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, 
                                 description='Adds or deletes files from the output archive.',
                                 epilog='If specified whith --remove, it prefixes the filepath')
parser.add_argument('-o', '--outsubdir', help="put the file in the specified sub-directory in the output archive", default="")
parser.add_argument('-a', '--add', help="add the given filepath to archive", default="")
parser.add_argument('-r', '--remove', help="remove the given filepath from the archive", default="")
args = parser.parse_args()

print("The usage of the 'archive' command is deprecated. Use the /archive folder directly.")
addfile = args.add
rmfile = args.remove
outsubdir = args.outsubdir

if addfile:
    try:
        os.makedirs('/archive/' + outsubdir)
    except OSError as e:
        pass
    
    try:    
        shutil.copy(addfile, '/archive/' + outsubdir + '/' + os.path.basename(addfile))
    except IOError as e:
        print(e)
        sys.exit(2)

if rmfile:
    try:
        os.remove('/archive/' + outsubdir + '/' + rmfile)
    except IOError as e:
        print(e)
        sys.exit(2)
