#!/usr/bin/perl
# run this script in directory ~/.fvwmconf
sub do_sys;

# YOU MAY WANT TO EDIT THE THREE LINES DESCRIBED BELOW
#--------------------------------------------------------------------------
# directory to put all fvwmconf support files (not user files)
# The file FvwmConf is the module which is called by fvwm2 .
# It goes in a different directory. The directory given here 
# contains support files which will be called by FvwmConf .
# The most common , best options are given first. Leave all but the
# one you choose commented out.
# The file FvwmConf.in will be edited to reflect this choice and 
# FvwmConf will be written.

#$SITE_LIB_B = '/usr/local/lib';
$SITE_LIB_B = '/usr/lib';

# you probably dont need to change this, you just change the above .
$SITE_LIB = $SITE_LIB_B . '/fvwmconf';
# $SITE_LIB = '/userdir/.fvwmconf';

#  directory for fvwm2 modules
# This is a directory where your fvwm2 modules  are commonly stored.
# This directory (with the /fvwm2 appended) must either be a standard
# path for fvmw2 modules or it must appear in the module path line in
# your .fvwm2rc file .
# The directory 'fvwm2' will be appended.
# uncomment the appropriate one, and comment out all the others,
# or add you own. The best, most common options are listed first.

#$SITE_MOD_D = '/usr/local/lib/X11';
$SITE_MOD_D = '/usr/lib/X11';


# Man page directory
#$MANINST = '/usr/local/man';
$MANINST = '/usr/man';

#---------------------------------------------------------------------------


# Don't change this. This says to look at the above directory under fvmw2
$SITE_MOD = $SITE_MOD_D .  '/fvwm2';
# $SITE_MOD = '/userdir/.fvwmconf';

#---- Don't need to change anything below here.



@PATHS = split /:/,$ENV{PATH};

# find where perl and fvwm2 are
sub find_programs {
    my @progs = (
		 'perl','fvwm2'
		 );	
    my($pr,$pa,$f) ;
    foreach $pr (@progs) {
	$f = 0;
	foreach $pa (@PATHS,perl_config_paths()) {
	    if (-e "$pa/$pr" ) {
		$WHERE{$pr} = "$pa/$pr";
		print "$pa/$pr\n";
		$f = 1;
		last;
	    }
	}
    }
}

# ripped off from perl configuration script
# This is overkill since they would not get this far without perl in path
sub perl_config_paths {
    my ($paths);
    $paths='/bin /usr/bin /usr/local/bin /usr/ucb /usr/local /usr/lbin';
    $paths="$paths /opt/bin /opt/local/bin /opt/local /opt/lbin";
    $paths="$paths /usr/5bin /etc /usr/gnu/bin /usr/new /usr/new/bin /usr/nbin";
    $paths="$paths /opt/gnu/bin /opt/new /opt/new/bin /opt/nbin";
    $paths="$paths /sys5.3/bin /sys5.3/usr/bin /bsd4.3/bin /bsd4.3/usr/ucb";
    $paths="$paths /bsd4.3/usr/bin /usr/bsd /bsd43/bin /usr/ccs/bin";
    return grep( -e, split( /\s+/,$paths));
}


find_programs();

# removed cp -rvf because flags don't work on Sun
$CP = 'cp -r ';

$com = $ARGV[0];

if ($com eq 'rc') {
    if ( -e "$ENV{HOME}/.fvwm2rc.hold" ) {
	die "You already have a .fvwm2rc.hold . You must save it to ".
	    "another name before running this script.";
    }
    if (-e "$ENV{HOME}/.fvwm2rc"  ) {
	system("/bin/mv -f ~/.fvwm2rc ~/.fvwm2rc.hold");
	die "Can't move .fvwm2rc" unless -e "$ENV{HOME}/.fvwm2rc.hold";
	print STDERR "Moved .fvwm2rc to .fvwm2rc.hold\n";
    }
    else { 
	print STDERR "No exisiting .fvwm2rc file found.\n";
    }
    
    open HAND, "<.fvwm2rc.example";
    
    @lines = <HAND>;
    close HAND;

    if (-e "$ENV{HOME}/.fvwm2rc") {die ".fvwm2rc is still there"};
    open OHAND, ">$ENV{HOME}/.fvwm2rc"  || die "Can't open .fvwm2rc for writing";
    foreach $one (@lines) {
	$one =~ s/USERHOME/$ENV{HOME}/;
	print OHAND $one;
    }
    close OHAND;
    print STDERR "New .fvwm2rc created at $ENV{HOME}/.fvwm2rc\n";
}
  
elsif ($com eq 'build' or not defined $com) {

    @binperl = ('changexpm', 'makegiftoxpm', 'mygiftoxpm');
#  Change a few lines in FvwmConf.in -> FvwmConf 
    foreach $outf ( 'FvwmConf' ,@binperl) {
	$inf= $outf . '.in';
	if ( -e $inf) {
            if (not open IHAND, "<$inf") {
		warn "Can't open $inf !";
		next;
	    }
	    print STDERR "Opened $inf\n";
	    @lines = <IHAND>;
	    close IHAND;
	    open OHAND, ">$outf" or die "Can't write to $outf";
	    foreach (@lines) {
		if (/^#INSTLIB/) {
		    print OHAND;
		    print OHAND "use lib '$SITE_LIB/lib';\n";
		}
		elsif (/^#VINSTLIB/ ) {
		       print OHAND;
		       print OHAND "\$instlibdir = '$SITE_LIB';";
		   }
		elsif (m,#!/usr/bin/perl,) {
		       if (exists $WHERE{perl} ) {
			   print OHAND "#!$WHERE{perl}\n" ;
		       }
		       else {
			   print OHAND;
		       }
		   }
		else {
		    print OHAND;
		}
	    }
	    close OHAND;  
	    print STDERR "Wrote $outf\n";
	    do_sys("chmod +x $outf");
	    
	}
	else {
	    die "I can't find FvwmConf.in ! \n It should be in the distribution";
	}
	
    }
    foreach (@binperl ) {
	do_sys "mv $_ bin/$_";
    }
}


if (not  defined $com) {
    do_sys("mkdir $SITE_LIB") unless -e $SITE_LIB;
    die "*** $SITE_LIB is not a directory" unless -d  $SITE_LIB;
    do_sys("mkdir $SITE_MOD_D") unless -e $SITE_MOD_D;
    do_sys("mkdir $SITE_MOD") unless -e $SITE_MOD;
    die "*** $SITE_MOD is not a directory" unless -d  $SITE_MOD;
    do_sys("$CP ./lib $SITE_LIB");
    do_sys("$CP ./bin $SITE_LIB");
    do_sys("$CP ./help $SITE_LIB");
    do_sys("$CP ./confimages $SITE_LIB");
    do_sys("$CP ./user $SITE_LIB");
    do_sys("$CP ./FvwmConf $SITE_MOD");
    do_sys "$CP ./man/man1/* $MANINST/man1";
    do_sys( "cd $MANINST/man1; ln -s FvwmConf.1x fvwmconf.1x");
}

#else {
#    print STDERR "Usage: 'install site' or 'install rc'\n";
#}

sub do_sys {
    my $string = shift;
    print STDERR "$string\n";
    system "$string";
}









