//-----------------------------------------------------------------------------
// Copyright © 2003 - Philip Howard - All rights reserved
//
// This program 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.
//
// This program 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 this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//-----------------------------------------------------------------------------
// package      libh/string
// homepage http://libh.slashusr.org/
//-----------------------------------------------------------------------------
// author       Philip Howard
// email        libh at ipal dot org
// homepage     http://phil.ipal.org/
//-----------------------------------------------------------------------------
// This file is best viewed using a fixed spaced font such as Courier
// and in a display at least 120 columns wide.
//-----------------------------------------------------------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <libh/string.h>

static const char *	var_prefix	= NULL;
static const char *	var_suffix	= NULL;
static int		shell_type	= 0;
static int		all_vars	= 0;
static int		env_vars	= 0;


//-----------------------------------------------------------------------------
// function	output_variable
//
// purpose	Output a variable appropriate for the specified shell type.
//
// arguments	1 (const char *) variable name
//		2 (const char *) data value
//
// returns	(void)
//-----------------------------------------------------------------------------
void
output_variable (
    const char *	arg_var_name
    ,
    const char *	arg_data_value
    )
{
    if ( ! arg_var_name ) return;

    if ( shell_type == 0 ) {
	if ( env_vars ) fputs( "export ", stdout );
	if ( var_prefix ) fputs( var_prefix, stdout );
	fputs( arg_var_name, stdout );
	if ( var_suffix ) fputs( var_suffix, stdout );
	fputs( "='", stdout );
	if ( arg_data_value ) {
	    while ( * arg_data_value ) {
		if ( * arg_data_value == '\'' ) {
		    fputs( "'\"'\"'", stdout );
		} else {
		    fputc( * arg_data_value, stdout );
		}
		++ arg_data_value;
	    }
	}
	fputs( "'\n", stdout );
    }

    else if ( shell_type == 1 ) {
	fputs( env_vars ? "setenv " : "set ", stdout );
	if ( var_prefix ) fputs( var_prefix, stdout );
	fputs( arg_var_name, stdout );
	if ( var_suffix ) fputs( var_suffix, stdout );
	fputs( env_vars ? " '" : " = '", stdout );
	if ( arg_data_value ) {
	    while ( * arg_data_value ) {
		if ( * arg_data_value == '\'' ) {
		    fputs( "'\"'\"'", stdout );
		} else {
		    fputc( * arg_data_value, stdout );
		}
		++ arg_data_value;
	    }
	}
	fputs( "'\n", stdout );
    }

    return;
}

//-----------------------------------------------------------------------------
// program	spliturl
//
// purpose	Split a URL into components suitable for shell scripts.
//-----------------------------------------------------------------------------
int
main (
    int		argc
    ,
    char * *	argv
    )
{
    char *		prot_p	;
    char *		user_p	;
    char *		pass_p	;
    char *		host_p	;
    char *		port_p	;
    char *		path_p	;
    char *		qstr_p	;

    shell_type = 0;
    all_vars = 0;
    env_vars = 0;

    -- argc;
    ++ argv;

    while ( argc && * argv && * * argv == '-' ) {
	if ( strcmp( * argv, "-a" ) == 0 ||
	     strcmp( * argv, "--all" ) == 0 ) {
	    all_vars = 1;
	}
	else if ( strcmp( * argv, "-e" ) == 0 ||
		  strcmp( * argv, "--env" ) == 0 ) {
	    env_vars = 1;
	}
	else if ( strcmp( * argv, "-p" ) == 0 ||
		  strcmp( * argv, "--prefix" ) == 0 ) {
	    if ( -- argc && ++ argv ) {
		var_prefix = * argv;
	    }
	}
	else if ( strcmp( * argv, "-s" ) == 0 ||
		  strcmp( * argv, "--suffix" ) == 0 ) {
	    if ( -- argc && ++ argv ) {
		var_suffix = * argv;
	    }
	}
	else if ( strcmp( * argv, "-b" ) == 0 ||
	     strcmp( * argv, "-k" ) == 0 ||
	     strcmp( * argv, "--sh" ) == 0 ||
	     strcmp( * argv, "--ksh" ) == 0 ||
	     strcmp( * argv, "--bash" ) == 0 ) {
	    shell_type = 0;
	}
	else if ( strcmp( * argv, "-c" ) == 0 ||
	     strcmp( * argv, "-t" ) == 0 ||
	     strcmp( * argv, "--csh" ) == 0 ||
	     strcmp( * argv, "--tcsh" ) == 0 ) {
	    shell_type = 1;
	}
	-- argc;
	++ argv;
    }

    while ( argc && * argv ) {
	char * url;
	url = str_dup( * argv );
	if ( str_split_url( url, & prot_p, & user_p, & pass_p, & host_p, & port_p, & path_p, & qstr_p ) == 0 ) {
	    output_variable( "prot", prot_p );
	    output_variable( "user", user_p );
	    output_variable( "pass", pass_p );
	    output_variable( "host", host_p );
	    output_variable( "port", port_p );
	    output_variable( "path", path_p );
	    output_variable( "qstr", qstr_p );
	    return 0;
	}
	-- argc;
	++ argv;
    }

    return 1;
}

