//-----------------------------------------------------------------------------
// Copyright © 2004 - 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/ctools
// 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 "io_lib.h"

__FMACRO_BEGIN__

#define put_var_bash(f,e,p,n,s,v) put_var_shell((f),0,(e),(p),(n),(s),(v))
#define put_var_ksh(f,e,p,n,s,v) put_var_shell((f),0,(e),(p),(n),(s),(v))
#define put_var_csh(f,e,p,n,s,v) put_var_shell((f),1,(e),(p),(n),(s),(v))
#define put_var_tcsh(f,e,p,n,s,v) put_var_shell((f),1,(e),(p),(n),(s),(v))

__FMACRO_END__

__PROTO_BEGIN__
//-----------------------------------------------------------------------------
// function	put_var_shell
//
// purpose	Output name and value strings in a form for shell variable
//		assignment.
//
// arguments	1 (FILE *) which file to output to
//		2 (int) code for which shell type:
//			0, 'b' bourne, bash, ksh shells
//			1, 'c' csh, tcsh shells
//		3 (int) 0 for normal variable, 1 for environment variable
//		4 (const char *) name prefix
//		5 (const char *) name
//		6 (const char *) name suffix
//		7 (const char *) value
//
// returns	(void)
//-----------------------------------------------------------------------------
void
put_var_shell (
    FILE *		arg_file
    ,
    int			arg_shell
    ,
    int			arg_env
    ,
    const char *	arg_prefix
    ,
    const char *	arg_name
    ,
    const char *	arg_suffix
    ,
    const char *	arg_value
    )
__PROTO_END__
{
    if ( ! arg_file ) return;
    if ( ! arg_name ) return;

    if ( arg_shell == 0 || arg_shell == 'b' ) {
	if ( arg_env ) fputs( "export ", arg_file );
	if ( arg_prefix ) fputs( arg_prefix, arg_file );
	fputs( arg_name, arg_file );
	if ( arg_suffix ) fputs( arg_suffix, arg_file );
	fputs( "='", arg_file );
	if ( arg_value ) {
	    while ( * arg_value ) {
		if ( * arg_value == '\'' ) {
		    fputs( "'\"'\"'", arg_file );
		}
		else {
		    fputc( * arg_value, arg_file );
		}
		++ arg_value;
	    }
	}
	fputs( "'\n", arg_file );
    }

    else if ( arg_shell == 1 || arg_shell == 'c' ) {
	fputs( arg_env ? "setenv " : "set ", arg_file );
	if ( arg_prefix ) fputs( arg_prefix, arg_file );
	fputs( arg_name, arg_file );
	if ( arg_suffix ) fputs( arg_suffix, arg_file );
	fputs( arg_env ? " '" : " = '", arg_file );
	if ( arg_value ) {
	    while ( * arg_value ) {
		if ( * arg_value == '\'' ) {
		    fputs( "'\"'\"'", arg_file );
		}
		else if ( * arg_value == '!' ) {
		    fputs( "\\!", arg_file );
		}
		else {
		    fputc( * arg_value, arg_file );
		}
		++ arg_value;
	    }
	}
	fputs( "'\n", arg_file );
    }

    return;
}

