/** @file censorArguments.c
 *
 *  @brief
 *
 *  @ingroup censor
 *
 *  @author IfA
 *  @version $Revision: 1.34.2.1 $ $Name: not supported by cvs2svn $
 *  @date $Date: 2009-02-19 17:59:50 $
 *  Copyright 2009 Institute for Astronomy, University of Hawaii
 */

# include "censor.h"

pmConfig *censorArguments (int argc, char **argv) {

    bool status;
    int N;

    if (argc == 1) {
        psError(CENSOR_ERR_ARGUMENTS, true, "No arguments supplied");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    // load config data from default locations
    pmConfig *config = pmConfigRead(&argc, argv, CENSOR_RECIPE);
    if (config == NULL) {
        psError(CENSOR_ERR_CONFIG, false, "Can't read site configuration");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT", "-file", "-list");
    if (!status) {
        psError(CENSOR_ERR_ARGUMENTS, true, "Missing -file (input) or -list (input)");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    if ((N = psArgumentGet (argc, argv, "-checkinputonly"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddBool(config->arguments, PS_LIST_TAIL, "CHECK.INPUT.ONLY", PS_META_REPLACE, "do not censor just check input sources", true);
        return config;
    }
        
    // define the input mask
    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "INPUT.MASK", "-mask", "-masklist");

    // dump the configuration to a file?
    if ((N = psArgumentGet (argc, argv, "-dumpconfig"))) {
        psArgumentRemove (N, &argc, argv);
        psMetadataAddStr (config->arguments, PS_LIST_TAIL, "DUMP_CONFIG", PS_META_REPLACE, "", argv[N]);
        psArgumentRemove (N, &argc, argv);
    }


    if (argc != 2) {
        psError(CENSOR_ERR_ARGUMENTS, true, "Incorrect arguments supplied");
        psErrorStackPrint(stderr, "exit");
        return NULL;
    }

    // output positions is fixed
    psMetadataAddStr (config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "", argv[1]);

    psTrace("censor", 1, "Done with censorArguments...\n");
    return (config);
}
