/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "apr.h"
#include "apr_lib.h"
#include "apr_getopt.h"
#include "apr_strings.h"
#include "apr_file_io.h"
#include "apr_file_xattr.h"

#if APR_HAVE_STDLIB_H
#include <stdlib.h>
#endif

#if !APR_HAS_XATTR
#error The APR you are using does not have extended attribute support
#endif

static apr_file_t *errfile;
static const char *shortname = "apxattr";

static void usage(void)
{
    apr_file_printf(errfile,
    "%s -- Utility to view/modify extended attributes on files." APR_EOL_STR
                                                                 APR_EOL_STR
    "Usage: %s -l FILENAME"                                      APR_EOL_STR
    "       %s -g attr FILENAME"                                 APR_EOL_STR
    "       %s -s attr -v value FILENAME"                        APR_EOL_STR
    "       %s -r attr FILENAME"                                 APR_EOL_STR
                                                                 APR_EOL_STR
    "Options:"                                                   APR_EOL_STR
    "  -l   List attributes"                                     APR_EOL_STR
    "  -g   Get attribute"                                       APR_EOL_STR
    "  -s   Set attribute"                                       APR_EOL_STR
    "  -r   Remove attributes"                                   APR_EOL_STR,
    shortname, shortname, shortname, shortname, shortname);
    exit(1);
}

int main(int argc, const char * const argv[])
{
    apr_file_t * outfile;
    apr_file_t * infile;
    apr_file_t * thefile;
    apr_pool_t * pool;
    apr_getopt_t * o;
    apr_status_t rv;
    const char * arg;
    char opt;
    int opt_list =  0;
    char errmsg[120];
    const char * opt_get = NULL, * opt_set = NULL, * opt_value = NULL,
               * opt_remove = NULL, * filename = NULL;

    if (apr_app_initialize(&argc, &argv, NULL) != APR_SUCCESS) {
        return 1;
    }
    atexit(apr_terminate);

    if (argc) {
        shortname = apr_filepath_name_get(argv[0]);
    }

    if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
        return 1;
    }
    apr_file_open_stderr(&errfile, pool);
    apr_getopt_init(&o, pool, argc, argv);

    while (1) {
        rv = apr_getopt(o, "lg:s:r:v:", &opt, &arg);
        if (rv == APR_EOF) {
            break;
        }
        else if (rv != APR_SUCCESS) {
            usage();
        }
        else {
            switch (opt) {
            case 'l':
                opt_list = 1;
                break;
	    case 's':
                if (opt_set || opt_get || opt_list || opt_remove) {
                    usage();
                }
                opt_set = apr_pstrdup(pool, arg);
                break;
	    case 'v':
                if (opt_value || opt_get || opt_list || opt_remove) {
                    usage();
                }
                opt_value = apr_pstrdup(pool, arg);
                break;
	    case 'g':
                if (opt_set || opt_get || opt_list || opt_remove) {
                    usage();
                }
                opt_get = apr_pstrdup(pool, arg);
                break;
	    case 'r':
                if (opt_set || opt_get || opt_list || opt_remove) {
                    usage();
                }
                opt_remove = apr_pstrdup(pool, arg);
                break;
	    }
        }
    }
    if ((opt_set && !opt_value) || o->ind != argc - 1) {
	usage();
    }
    filename = argv[o->ind];

    apr_file_open_stdout(&outfile, pool);
    apr_file_open_stdin(&infile, pool);

    rv = apr_file_open(&thefile, filename, APR_READ, 0, pool);
    if(rv != APR_SUCCESS) {
	apr_file_printf(errfile, "*** Couldn't open %s: %s\n",
			filename, apr_strerror(rv, errmsg, sizeof errmsg));
	exit(1);
    }

    if (opt_get) {
	void *value;
	char *value_str;
	apr_size_t size;
	rv = apr_file_xattr_get(thefile, opt_get, &value, &size, 0, pool);
	if(rv != APR_SUCCESS) {
	    apr_file_printf(errfile, "*** Couldn't get attribute: %s\n",
			    apr_strerror(rv, errmsg, sizeof errmsg));
	    exit(1);
	}
	else {
	    value_str = apr_pstrmemdup(pool, value, size);
	    apr_file_printf(outfile, "%s\n", value_str);
	}
    }
    else if (opt_set) {
	apr_size_t size;
	rv = apr_file_xattr_set(thefile, opt_set, opt_value,
				strlen(opt_value), 0, pool);
	if(rv != APR_SUCCESS) {
	    apr_file_printf(errfile, "*** Couldn't set attribute: %s\n",
			    apr_strerror(rv, errmsg, sizeof errmsg));
	    exit(1);
	}
    }
    else if (opt_list) {
	apr_array_header_t *list = NULL;
	rv = apr_file_xattr_list(thefile, &list, 0, pool);
	if(rv != APR_SUCCESS) {
	    apr_file_printf(errfile, "*** Couldn't list attributes: %s\n",
			    apr_strerror(rv, errmsg, sizeof errmsg));
	    exit(1);
	}
	else {
	    int i;
	    for(i = 0; i < list->nelts; i++) {
		apr_file_printf(outfile, "%s\n", APR_ARRAY_IDX(list, i, char*));
	    }
	}
    }
    else if (opt_remove) {
	rv = apr_file_xattr_remove(thefile, opt_remove, 0, pool);
	if(rv != APR_SUCCESS) {
	    apr_file_printf(errfile, "*** Couldn't remove attribute: %s\n",
			    apr_strerror(rv, errmsg, sizeof errmsg));
	    exit(1);
	}
    }

    apr_file_close(thefile);

    return 0;
}