main(
    int		argc,
    char **	argv)
{
    int c;
    char *command;
    application_argument_t argument;
    int i;

#ifdef BSDTAR
    bsdtar_path = g_strdup(BSDTAR);
#else
    bsdtar_path = NULL;
#endif
    state_dir = NULL;
    bsdtar_directory = NULL;
    bsdtar_onefilesystem = 1;
    exit_handling = NULL;

    /* initialize */

    /*
     * Configure program for internationalization:
     *   1) Only set the message locale for now.
     *   2) Set textdomain for all amanda related programs to "amanda"
     *      We don't want to be forced to support dozens of message catalogs.
     */
    setlocale(LC_MESSAGES, "C");
    textdomain("amanda");

    if (argc < 2) {
        printf("ERROR no command given to ambsdtar\n");
        error(_("No command given to ambsdtar"));
    }

    /* drop root privileges */
    if (!set_root_privs(0)) {
	if (g_str_equal(argv[1], "selfcheck")) {
	    printf("ERROR ambsdtar must be run setuid root\n");
	}
	error(_("ambsdtar must be run setuid root"));
    }

    safe_fd(3, 2);

    set_pname("ambsdtar");
    set_pcomponent("application");
    set_pmodule("ambsdtar");

    /* Don't die when child closes pipe */
    signal(SIGPIPE, SIG_IGN);

#if defined(USE_DBMALLOC)
    malloc_size_1 = malloc_inuse(&malloc_hist_1);
#endif

    add_amanda_log_handler(amanda_log_stderr);
    add_amanda_log_handler(amanda_log_syslog);
    dbopen(DBG_SUBDIR_CLIENT);
    startclock();
    g_debug(_("version %s"), VERSION);

    config_init(CONFIG_INIT_CLIENT|CONFIG_INIT_GLOBAL, NULL);

    //check_running_as(RUNNING_AS_DUMPUSER_PREFERRED);
    //root for amrecover
    //RUNNING_AS_CLIENT_LOGIN from selfcheck, sendsize, sendbackup

    /* parse argument */
    command = argv[1];

    argument.config     = NULL;
    argument.host       = NULL;
    argument.message    = 0;
    argument.collection = 0;
    argument.calcsize = 0;
    argument.tar_blocksize = NULL;
    argument.level      = NULL;
    argument.command_options = NULL;
    argument.verbose = 0;
    init_dle(&argument.dle);
    argument.dle.record = 0;

    while (1) {
	int option_index = 0;
	c = getopt_long (argc, argv, "", long_options, &option_index);
	if (c == -1) {
	    break;
	}
	switch (c) {
	case 1: amfree(argument.config);
		argument.config = g_strdup(optarg);
		break;
	case 2: amfree(argument.host);
		argument.host = g_strdup(optarg);
		break;
	case 3: amfree(argument.dle.disk);
		argument.dle.disk = g_strdup(optarg);
		break;
	case 4: amfree(argument.dle.device);
		argument.dle.device = g_strdup(optarg);
		break;
	case 5: argument.level = g_slist_append(argument.level,
					        GINT_TO_POINTER(atoi(optarg)));
		break;
	case 6: argument.dle.create_index = 1;
		break;
	case 7: argument.message = 1;
		break;
	case 8: argument.collection = 1;
		break;
	case 9: argument.dle.record = 1;
		break;
	case 10: amfree(bsdtar_path);
		 bsdtar_path = g_strdup(optarg);
		 break;
	case 11: amfree(state_dir);
		 state_dir = g_strdup(optarg);
		 break;
	case 12: if (strcasecmp(optarg, "NO") == 0)
		     bsdtar_onefilesystem = 0;
		 else if (strcasecmp(optarg, "YES") == 0)
		     bsdtar_onefilesystem = 1;
		 else if (strcasecmp(command, "selfcheck") == 0)
		     printf(_("ERROR [%s: bad ONE-FILE-SYSTEM property value (%s)]\n"), get_pname(), optarg);
		 break;
	case 16: argument.dle.include_file =
			 append_sl(argument.dle.include_file, optarg);
		 break;
	case 17: argument.dle.include_list =
			 append_sl(argument.dle.include_list, optarg);
		 break;
	case 18: argument.dle.include_optional = 1;
		 break;
	case 19: argument.dle.exclude_file =
			 append_sl(argument.dle.exclude_file, optarg);
		 break;
	case 20: argument.dle.exclude_list =
			 append_sl(argument.dle.exclude_list, optarg);
		 break;
	case 21: argument.dle.exclude_optional = 1;
		 break;
	case 22: amfree(bsdtar_directory);
		 bsdtar_directory = g_strdup(optarg);
		 break;
	case 23: normal_message =
			 g_slist_append(normal_message, optarg);
		 break;
	case 24: ignore_message =
			 g_slist_append(ignore_message, optarg);
		 break;
	case 25: strange_message =
			 g_slist_append(strange_message, optarg);
		 break;
	case 26: amfree(exit_handling);
		 exit_handling = g_strdup(optarg);
		 break;
	case 27: argument.calcsize = 1;
		 break;
	case 28: amfree(argument.tar_blocksize);
		 argument.tar_blocksize = g_strdup(optarg);
		 gblocksize = atoi(argument.tar_blocksize);
		 break;
	case 33: argument.command_options =
			g_slist_append(argument.command_options,
				       g_strdup(optarg));
		 break;
	case 36: if (strcasecmp(optarg, "YES") == 0)
		     argument.verbose = 1;
		 break;
	case ':':
	case '?':
		break;
	}
    }

    if (!argument.dle.disk && argument.dle.device)
	argument.dle.disk = g_strdup(argument.dle.device);
    if (!argument.dle.device && argument.dle.disk)
	argument.dle.device = g_strdup(argument.dle.disk);

    argument.argc = argc - optind;
    argument.argv = argv + optind;

    if (argument.config) {
	config_init(CONFIG_INIT_CLIENT | CONFIG_INIT_EXPLICIT_NAME | CONFIG_INIT_OVERLAY,
		    argument.config);
	dbrename(get_config_name(), DBG_SUBDIR_CLIENT);
    }

    if (config_errors(NULL) >= CFGERR_ERRORS) {
	g_critical(_("errors processing config file"));
    }

    if (state_dir && strlen(state_dir) == 0)
	amfree(state_dir);
    if (!state_dir) {
	state_dir = g_strdup_printf("%s/%s", amdatadir, "bsdtar");
    }

    re_table = build_re_table(init_re_table, normal_message, ignore_message,
			      strange_message);

    for(i=0;i<256;i++)
	exit_value[i] = 1; /* BAD  */
    exit_value[0] = 0;     /* GOOD */
    if (exit_handling) {
	char *s = exit_handling;
	while (s) {
	    char *r = strchr(s, '=');
	    if (r) {
		int j = atoi(s);
		if (j >= 0 && j < 256) {
		    r++;
		    if (strncasecmp(r, "GOOD", 4) == 0) {
			exit_value[j] = 0;
		    }
		}
	    }
	    s = strchr(s+1, ' ');
	}
    }

    if (bsdtar_path) {
	g_debug("BSDTAR-PATH %s", bsdtar_path);
    } else {
	g_debug("BSDTAR-PATH is not set");
    }
    if (state_dir) {
	    g_debug("STATE-DIR %s", state_dir);
    } else {
	g_debug("STATE-DIR is not set");
    }
    if (bsdtar_directory) {
	g_debug("DIRECTORY %s", bsdtar_directory);
    }
    g_debug("ONE-FILE-SYSTEM %s", bsdtar_onefilesystem? "yes":"no");
    {
	amregex_t *rp;
	for (rp = re_table; rp->regex != NULL; rp++) {
	    switch (rp->typ) {
		case DMP_NORMAL : g_debug("NORMAL %s", rp->regex); break;
		case DMP_IGNORE : g_debug("IGNORE %s", rp->regex); break;
		case DMP_STRANGE: g_debug("STRANGE %s", rp->regex); break;
		case DMP_SIZE   : g_debug("SIZE %s", rp->regex); break;
		case DMP_ERROR  : g_debug("ERROR %s", rp->regex); break;
	    }
	}
    }

    if (g_str_equal(command, "support")) {
	ambsdtar_support(&argument);
    } else if (g_str_equal(command, "selfcheck")) {
	ambsdtar_selfcheck(&argument);
    } else if (g_str_equal(command, "estimate")) {
	ambsdtar_estimate(&argument);
    } else if (g_str_equal(command, "backup")) {
	ambsdtar_backup(&argument);
    } else if (g_str_equal(command, "restore")) {
	ambsdtar_restore(&argument);
    } else if (g_str_equal(command, "validate")) {
	ambsdtar_validate(&argument);
    } else if (g_str_equal(command, "index")) {
	ambsdtar_index(&argument);
    } else {
	g_debug("Unknown command `%s'.", command);
	fprintf(stderr, "Unknown command `%s'.\n", command);
	exit (1);
    }

    g_free(argument.config);
    g_free(argument.host);
    g_free(argument.dle.disk);
    g_free(argument.dle.device);
    g_free(argument.tar_blocksize);
    g_slist_free(argument.level);

    dbclose();

    return exit_status;
}