On Wed, Jul 29, 1998 at 11:58:21PM -0400, Shaleh wrote:

> Enclosed is my script.  All functionality but the --ask (or noask) is
> there.  Please send comments/questions/complaints my way.  Sorry Marcelo
> I did it in Perl (-:

 Hey, nice! I enclose a modified version (with --ask). Don't be sorry,
 I'm "Learning Perl" :-)


                                        Marcelo
#! /usr/bin/perl
###
#
# written by Shaleh <[EMAIL PROTECTED]>
#
###

while ($ARGV[0] =~ m/^--/) {
        $_ = shift(@ARGV);
        if (/--add/) {
                $mode="add";
        } elsif (/--remove/) {
                $mode="remove";
        } elsif (/--default/) {
                $mode="default";
        } elsif (/--ask/) {
                $ask="true";
        } else {
                &usage;
        }
}

unless( $#ARGV == 0 ) { 
        &usage;
}

if( $ARGV[0] !~ m/^\// ) {
        $WM = '/usr/X11R6/bin/' . $ARGV[0];
}
else {
        $WM = $ARGV[0];
}

if ( $ask eq "true") {
        &ask($mode, $WM);
}

if( $mode eq 'add' ) {
        &add_to_file($WM);
} 
elsif( $mode eq 'default' ) {
        &make_default($WM);
} 
elsif( $mode eq 'remove' ) {
        &rm_from_file($WM);
} 


###
#
# Usage information
#
###
sub usage {
        print <<EOT;
Usage: $0 [--ask] {--add|--default|--remove} <wm>

Options:
  --ask            Asks for confimation of the requested action

Actions:
  --add            Adds the specified window manager to the bottom of the
                   /etc/X11/window-managers file
  --remove         Removes the specified window manager
  --default        Adds the specified window manager to the top of the
                   /etc/X11/window-managers file, thus making it the system
                   default

<wm> can the the full path to the window manager excecutable, or just the
filename (/usr/X11R6/bin/ will be prepended to it).

EOT
        exit(0);
}

###
#
# Asks for confirmation of the requested action
#
###
sub ask {
        ($mode) = shift(@_);
        ($WM)   = @_;

        if (open(WMFILE, '/etc/X11/window-managers')) {
                @WMS = <WMFILE>;
                close(WMFILE);
                print "The following window managers are listed in 
/etc/X11/window-managers:\n\n";
                foreach (@WMS) {
                        if (m/\//) {
                                print "\t" . $_;
                        }
                }
                print "\n";
        }

        if ($mode eq "add") {
                $prompt = "Do you want to add $WM to the list";
        } elsif ($mode eq "remove") {
                $prompt = "Do you want to remove $WM from the list";
        } elsif ($mode eq "default") {
                $prompt = "Do you want make $WM the default";
        }

        &ask_n($prompt) || exit(0);
}

###
#
# Asks a question, "No" is the default
#
###
sub ask_n {
        my $answer;
        print @_,"? [No] ";
        $answer=<STDIN>;
        return ( $answer =~ /^\s*y/i );
}

###
#
# Adds window manager passed to the end of window-managers file
#
###
sub add_to_file {
        ($wm) = @_;

        return unless( &ifexists($wm) eq 'false' );
        
        open(WMFILE, '>>/etc/X11/window-managers') || 
                die("Could not open /etc/X11/window-managers for writing\n");
        print WMFILE "$wm\n";
        close(WMFILE);
}

###
#
# Makes window manager passed the default by putting it first
#
###
sub make_default {
        ($wm) = @_;
        my $found = 0;

        if( &ifexists($wm) eq 'true' ) {
                &rm_from_file($wm);
        }

        open(WMFILE, '/etc/X11/window-managers') || 
                die("Could not open /etc/X11/window-managers\n");
        @WMS = <WMFILE>;
        close(WMFILE);
        open(WMFILE, '>/etc/X11/window-managers') || 
                die("Could not open /etc/X11/window-managers for writing\n");
        foreach (@WMS) {
                if( m/^\// && $found == 0 ) {
                        print WMFILE "$wm\n";
                        $found = 1;
                }
                print WMFILE;
        }
        close(WMFILE);
}

###
#
# Removes passed window manager from window-managers file
#
###
sub rm_from_file {
        ($wm) = @_;

        return unless( &ifexists($wm) eq 'true' );

        open(WMFILE, '/etc/X11/window-managers') ||
                die("Could not open /etc/X11/window-managers\n");
        @WMS = <WMFILE>;
        close(WMFILE);
        open(WMFILE, '>/etc/X11/window-managers') ||
                die("Could not open /etc/X11/window-managers for writing\n");

        foreach (@WMS) {
                print WMFILE unless( m/$wm/ );
        }
}

###
#
# returns 'true' if window manager exists in the file, 'false' otherwise
#
###
sub ifexists {
        ($wm) = @_;

        open(WMFILE, '/etc/X11/window-managers') || 
                die("Could not open /etc/X11/window-managers\n");
        
        @WMS = <WMFILE>;
        close WMFILE;
        foreach (@WMS) {
                unless( m/^#/ ) {
                        if( m/$wm/ ) {
                                return 'true';
                        }
                }
        }
        return 'false';
}

Reply via email to