/* SPDX-License-Identifier: BSD-3-Clause */

#include <stdio.h>
#include <unistd.h>

#include "atoi/a2i.h"
#include "io/fprintf.h"
#include "getdef.h"
#include "subid.h"
#include "stdlib.h"


/* Test program for the subid creation routine */

static const char Prog[] = "new_subid_range";

static void usage(void)
{
	eprintf("Usage: %s [-g] [-n] user count\n", Prog);
	eprintf("    Find a subuid (or with -g, subgid) range for user\n");
	eprintf("    If -n is given, a new range will be created even if one exists\n");
	eprintf("    count defaults to 65536\n");
	exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
	int c;
	struct subordinate_range range;
	bool makenew = false; // reuse existing by default
	bool group = false;   // get subuids by default
	bool ok;

	if (!subid_init(Prog, stderr))
		eprinte("subid_init");
	while ((c = getopt(argc, argv, "gn")) != EOF) {
		switch(c) {
		case 'n': makenew = true; break;
		case 'g': group = true; break;
		default: usage();
		}
	}
	argv = &argv[optind];
	argc = argc - optind;
	if (argc == 0)
		usage();
	range.owner = argv[0];
	range.start = getdef_ulong ("SUB_UID_MIN", 100000UL);
	range.count = getdef_ulong ("SUB_UID_COUNT", 65536);
	if (argc > 1)
		str2ul(&range.count, argv[1]);
	if (group)
		ok = subid_grant_gid_range(&range, !makenew);
	else
		ok = subid_grant_uid_range(&range, !makenew);

	if (!ok) {
		eprintf("Failed creating new id range\n");
		exit(EXIT_FAILURE);
	}
	printf("Subuid range %lu:%lu\n", range.start, range.count);

	return 0;
}
