/*****************************************************************************\
 *  tres_functions.c - functions dealing with TRES in the
 *                        accounting system.
 *****************************************************************************
 *  Copyright (C) 2015 SchedMD LLC.
 *  Written by David Bigagli <david@schedmd.com>
 *
 *  This file is part of SLURM, a resource management program.
 *  For details, see <http://slurm.schedmd.com/>.
 *  Please also read the included file: DISCLAIMER.
 *
 *  SLURM is free software; you can redistribute it and/or modify it under
 *  the terms of the GNU General Public License as published by the Free
 *  Software Foundation; either version 2 of the License, or (at your option)
 *  any later version.
 *
 *  In addition, as a special exception, the copyright holders give permission
 *  to link the code of portions of this program with the OpenSSL library under
 *  certain conditions as described in each individual source file, and
 *  distribute linked combinations including the two. You must obey the GNU
 *  General Public License in all respects for all of the code used other than
 *  OpenSSL. If you modify file(s) with this exception, you may extend this
 *  exception to your version of the file(s), but you are not obligated to do
 *  so. If you do not wish to do so, delete this exception statement from your
 *  version.  If you delete this exception statement from all source files in
 *  the program, then also delete it here.
 *
 *  SLURM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 *  FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *  details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with SLURM; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
\*****************************************************************************/

#include "src/sacctmgr/sacctmgr.h"
#include "src/common/assoc_mgr.h"

/* sacctmgr_list_tres()
 */
int
sacctmgr_list_tres(int argc, char **argv)
{
        List tres_list;
        ListIterator itr;
	ListIterator itr2;
	List format_list;
	List print_fields_list;
        slurmdb_tres_cond_t cond;
        slurmdb_tres_rec_t *tres;
	int field_count;
	print_field_t *field;

	format_list = list_create(slurm_destroy_char);
	/* Append to the format list the fields
	 * we want to print, these are the data structure
	 * members of the type returned by slurmdbd
	 */
	slurm_addto_char_list(format_list, "Type,Name%15,ID");

	if (exit_code) {
		FREE_NULL_LIST(format_list);
		return SLURM_ERROR;
	}

	/* Process the format list creating a list of
	 * print field_t structures
	 */
	print_fields_list = sacctmgr_process_format_list(format_list);
	FREE_NULL_LIST(format_list);

	/* Call slurmdbd to get all tres with not
	 * condition
	 */
        memset(&cond, 0, sizeof(slurmdb_tres_cond_t));
        tres_list = acct_storage_g_get_tres(db_conn, my_uid, &cond);

        itr = list_iterator_create(tres_list);
	itr2 = list_iterator_create(print_fields_list);
	print_fields_header(print_fields_list);
	field_count = list_count(print_fields_list);

	/* For each tres prints the data structure members
	 */
        while ((tres = list_next(itr))) {
		while ((field = list_next(itr2))) {
			switch (field->type) {
				case PRINT_NAME:
					field->print_routine(field,
							     tres->name,
							     field_count);
					break;
				case PRINT_ID:
					field->print_routine(field,
							     tres->id,
							     field_count);
					break;
				case PRINT_TYPE:
					field->print_routine(field,
							     tres->type,
							     field_count);
					break;
			}
		}
		list_iterator_reset(itr2);
		printf("\n");
        }
	list_iterator_destroy(itr);
	list_iterator_destroy(itr2);
	FREE_NULL_LIST(tres_list);
	FREE_NULL_LIST(print_fields_list);

        return 0;
}
