/*
 *  nwbocreate.c
 *
 *  Create a bindery object on a NetWare server
 *
 *  Copyright (C) 1996 by Volker Lendecke
 *
 */

#include "ncplib.h"
#include <unistd.h>
#include <stdlib.h>

static char *progname;

static void
usage(void)
{
	fprintf(stderr, "usage: %s [options]\n", progname);
}

static void
help(void)
{
        printf("\n");
        printf("usage: %s [options]\n", progname);
        printf("\n"
	       "-h             Print this help text\n"
	       "-S server      Server name to be used\n"
               "-U username    Username sent to server\n"
               "-P password    Use this password\n"
               "-n             Do not use any password\n"
               "-C             Don't convert password to uppercase\n"
	       "\n"
	       "-o object_name Name of created object\n"
	       "-t type        Object type (decimal value)\n"
	       "-r read-flag   Read security\n"
	       "-w write-flag  Write security\n"
	       "\n");
}

static int
parse_security(const char *security)
{
	if (strcasecmp(security, "anyone") == 0)
	{
		return 0;
	}
	if (strcasecmp(security, "logged") == 0)
	{
		return 1;
	}
	if (strcasecmp(security, "object") == 0)
	{
		return 2;
	}
	if (strcasecmp(security, "supervisor") == 0)
	{
		return 3;
	}
	if (strcasecmp(security, "netware") == 0)
	{
		return 4;
	}
	return -1;
}		
	
int
main(int argc, char *argv[])
{
	struct ncp_conn *conn;
	char *object_name = NULL;
	int object_type = -1;
	long err;

	int read_sec = 1;	/* logged read */
	int write_sec = 3;	/* supervisor write */
	int result = 1;
	
	int opt;

	progname = argv[0];

	if ((conn = ncp_initialize(&argc, argv, 1, &err)) == NULL)
	{
		com_err(argv[0], err, "when initializing");
		goto finished;
	}

	while ((opt = getopt(argc, argv, "h?o:t:r:w:")) != EOF)
	{
		switch(opt) {
		case 'o':
			object_name = optarg;
			str_upper(object_name);
			break;
		case 't':
			object_type = atoi(optarg);
			break;
		case 'r':
			read_sec = parse_security(optarg);
			if (read_sec < 0)
			{
				fprintf(stderr,
					"%s: Wrong read security\n"
					"Must be one of anyone, logged, "
					"object, supervisor or netware\n",
					argv[0]);
				goto finished;
			}
			break;
		case 'w':
			write_sec = parse_security(optarg);
			if (write_sec < 0)
			{
				fprintf(stderr,
					"%s: Wrong write security\n"
					"Must be one of anyone, logged, "
					"object, supervisor or netware\n",
					argv[0]);
				goto finished;
			}
			break;
		case 'h':
		case '?':
			help();
			goto finished;
		default:
			usage();
			goto finished;
		}
	}

	if (object_type < 0)
	{
		fprintf(stderr, "%s: You must specify an object type\n",
			argv[0]);
		goto finished;
	}

	if (object_name == NULL)
	{
		fprintf(stderr, "%s: You must specify an object name\n",
			argv[0]);
		goto finished;
	}

	if (ncp_create_bindery_object(conn, object_type, object_name,
				      (write_sec << 4) + read_sec, 0) != 0)
	{
		fprintf(stderr, "%s: Could not create the object\n", argv[0]);
	}
	else
	{
		result = 0;
	}

 finished:
	ncp_close(conn);
	return result;
}
