This file is indexed.

/usr/bin/surfraw-update-path is in surfraw 2.2.8-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/usr/bin/perl -w
# $Id$
# Ian Beckwith <ianb@erislabs.net> 20030927
# adds surfraw's elvi directory to your PATH in your shell's config file
# see end of file for POD documentation

use strict;
use Getopt::Long;
use Fcntl qw(SEEK_END);

use vars qw($me $elvidir);
$me=($0=~/(?:.*\/)?(.*)/)[0];

$elvidir="/usr/lib/surfraw";

use vars qw($starttag $endtag);
$starttag="### Added by surfraw.";
$endtag  ="### End surfraw addition.";

use vars qw(%shells);
%shells=(
	"bash" 	=> \&sh_bash,
	"sh" 	=> \&sh_posix,
	"ash" 	=> \&sh_posix,
	"dash" 	=> \&sh_posix,
	"csh" 	=> \&sh_csh,
	"tcsh" 	=> \&sh_csh,
	"pdksh"	=> \&sh_posix,
	"ksh"	=> \&sh_posix,
	"zsh"	=> \&sh_zsh,
	"rc"	=> \&sh_rc,
	"es"	=> \&sh_es,
    );

use vars qw($ADD $CHECK $REMOVE);
$ADD	= 0;
$CHECK	= 1;
$REMOVE	= 2;

use vars qw($OK $NOTFOUND $ERROR);
$OK=0;
$NOTFOUND=1;
$ERROR=2;

use vars qw($POSIX $CSH $RC @syntax);
$POSIX = 0;
$CSH   = 1;
$RC    = 2;

@syntax=();
$syntax[$POSIX]="\texport PATH=\$PATH:$elvidir\n";
$syntax[$CSH]="\tsetenv PATH \"\${PATH}:$elvidir\"\n";
$syntax[$RC]="\tpath=(\$path $elvidir)\n";

my $sys=0;
my $allshells=0;
my $help=0;
my $action=$CHECK; #default
my $shell=$ENV{SHELL};

GetOptions('add'	 => sub { $action=$ADD;    },
		   'check'	 => sub { $action=$CHECK;  },
		   'remove'	 => sub { $action=$REMOVE; },
		   'sys'	 => \$sys,
		   'shell=s' => \$shell,
		   'all'	 => \$allshells,
		   'help'    => \$help);

if($help)      { usage(); exit 0; }

if($allshells)
{
	my $ret=0;
	my @shells=qw(sh csh zsh);
	# rc and es only have user startup files
	# bash sys startup file is same as sh
	unless($sys) { @shells=(@shells, qw(bash es rc)); }
	for my $s (@shells)
	{
		my $thisret=$shells{$s}($action,$sys);
		$ret=max($thisret,$ret);
	}
	exit $ret;
}
else
{
	if(defined($shell))
	{
		$shell=~s/.*\/\-?//; # strip path
	}
	else
	{
		print STDERR "$me: Cannot determine shell. Use -shell=shellname\n";
		exit $ERROR;
	}

	unless(exists($shells{$shell}))
	{
		print STDERR "Shell $shell not supported, please manually add $elvidir to your \$PATH\n";
		exit $ERROR;
	}
	
	exit($shells{$shell}($action,$sys));
}

sub sh_posix
{
	my($action,$sys)=@_;
	if($sys) { return scan("/etc/profile",$action,$sys,$POSIX); }
	elsif(exists($ENV{ENV}))
	{
		return scan($ENV{ENV},$action,$sys,$POSIX);
	}
	elsif(exists($ENV{HOME}))
	{
		return scan($ENV{HOME}."/.profile",$action,$sys,$POSIX);
	}
	else
	{
		print STDERR "$me: Cannot find home directory (\$HOME is not set)\n";
		return $ERROR;
	}
}

sub sh_csh
{
	my($action,$sys)=@_;
	if($sys) { return scan("/etc/csh.cshrc",$action,$sys,$CSH); }
	elsif(exists($ENV{HOME}))
	{
		return scan($ENV{HOME}."/.cshrc",$action,$sys,$CSH);
	}
	else
	{
		print STDERR "$me: Cannot find home directory (\$HOME is not set)\n";
		return $ERROR;
	}
}

sub sh_bash
{
	my($action,$sys)=@_;
	if($sys) { return scan("/etc/profile",$action,$sys,$POSIX); }
	elsif(exists($ENV{HOME}))
	{
		my $home=$ENV{HOME};
		my $file;
		if(-r $home."/.bash_profile") { $file=$home."/.bash_profile"; }
		elsif(-r $home."/.bash_login") { $file=$home."/.bash_login"; }
		else { $file=$home."/.profile"; } #  whether it exists or not
		# for interactive login shells
		my $ret1=scan($file,$action,$sys,$POSIX);
		# for interactive non-login shells
		my $ret2=scan($home."/.bashrc",$action,$sys,$POSIX);
		return(max($ret1,$ret2));
	}
	else
	{
		print STDERR "$me: Cannot find home directory (\$HOME is not set)\n";
		return $ERROR;
	}
}

sub sh_zsh 
{
	my($action,$sys)=@_;
	if($sys)
	{
		if(-d "/etc/zsh")
		{
			return scan("/etc/zsh/zshenv",$action,$sys,$POSIX);
		}
		else
		{
			return scan("/etc/zshenv",$action,$sys,$POSIX);
		}
	}
	elsif(exists($ENV{ZDOTDIR}))
	{
		return scan($ENV{ZDOTDIR}."/.zshenv",$action,$sys,$POSIX);
	}
	elsif(exists($ENV{HOME}))
	{
		return scan($ENV{HOME}."/.zshenv",$action,$sys,$POSIX);
	}
	else
	{
		print STDERR "$me: Cannot find home directory (\$HOME is not set)\n";
		return $ERROR;
	}
}

sub sh_rc
{
	my($action,$sys)=@_;
	if($sys)
	{
		print STDERR "$me: Shell rc has no system config file\n";
		return $ERROR;
	}
	elsif(exists($ENV{HOME}))
	{
		return scan($ENV{HOME}."/.rcrc",$action,$sys,$RC);
	}
	else
	{
		print STDERR "$me: Cannot find home directory (\$HOME is not set)\n";
		return $ERROR;
	}
}

sub sh_es
{
	my($action,$sys)=@_;
	if($sys)
	{
		print STDERR "$me: Shell es has no system config file\n";
		return $ERROR;
	}
	elsif(exists($ENV{HOME}))
	{
		return scan($ENV{HOME}."/.esrc",$action,$sys,$RC);
	}
	else
	{
		print STDERR "$me: Cannot find home directory (\$HOME is not set)\n";
		return $ERROR;
	}
}

sub scan
{
	my($file,$action,$sys,$type)=@_;
	if(-e $file) 
	{
		# file exists
		my $mode="<";
		if(($action==$ADD) || ($action==$REMOVE)) { $mode="+<"; }
		unless(open(F,"$mode$file"))
		{
			print STDERR "$me: Cannot open $file: $!\n";
			return $ERROR;
		}
		my $found=0;
		my $startoffset=0;
		my $aftertag=0;
		my $rest='';
		my $offset=tell(F);
		while(<F>)
		{
			if(/$starttag/)
			{
				$found=1;
				$startoffset=$offset;
			}
			elsif($found && /$endtag/)
			{
				$aftertag=1;
			}
			elsif($aftertag)
			{
				$rest.=$_;
			}
			$offset=tell(F);
		}
		if($found)
		{
			if($action==$ADD)
			{
				print STDERR "$me: Surfraw config already present in $file\n";
				return $OK;
			}
			elsif($action==$CHECK)
			{
				print("$me: Surfraw config present in $file\n");
				return $OK;
			}
			else # remove
			{
				# move rest of file up to startoffset
				unless(truncate(F,$startoffset))
				{
					print STDERR "$me: Cannot truncate $file: $!\n";
					return $ERROR;
				}
				# just in case, seek to new end
				unless(seek(F,0, SEEK_END))
				{
					print STDERR "$me: $file: Cannot seek: $!\n";
					return $ERROR;
				}
				print F $rest;
				print "$me: Removed surfraw config code from $file\n";
				return $OK;
			}
		}
		else # tag not found
		{
			if($action==$ADD)
			{
				# seek to end just in case
				unless(seek(F,0,SEEK_END))
				{
					print STDERR "$me: $file: Cannot seek: $!\n";
					return $ERROR;
				}
				print F maketag($sys,$type);
				print "$me: Added surfraw config code to $file\n";
				return $OK;
			}
			elsif($action==$CHECK)
			{
				print "$me: surfraw config code not found in $file\n";
				return $NOTFOUND;
			}
			else # remove
			{
				print STDERR "$me: cannot remove surfraw code: not found\n";
				return $ERROR;
			}
		}
		unless(close(F))
		{
			print STDERR "$me: Cannot close $file: $!\n";
			return $ERROR;
		}
	}
	else # file doesnt exist
	{
		if($action==$CHECK)
		{
			print "$me: surfraw config code not found (file $file not found)\n";
			return $NOTFOUND;
		}
		elsif($action==$REMOVE)
		{
			print STDERR "$me: Cannot remove surfraw config (file $file not found)\n";
			return $ERROR;
		}
		else
		{
			# create file
			unless(open(F,">$file"))
			{
				print STDERR "$me: Cannot open $file for writing: $!\n";
				return $ERROR;
			}
			print F maketag($sys,$type);
			unless(close(F))
			{
				print STDERR "$me: Error closing $file: $!\n";
				return $ERROR;
			}
			chmod(0644,$file) or warn("$me: Could not set permissions on $file: $!\n");	
			print "$me: Added surfraw config code to $file\n";
			return $OK;
		}
	}
}

sub maketag
{
	my ($sys,$type)=@_;
	my $remove=" To remove use $me -remove";
	my $tag=$starttag;
	$tag .= $remove;
	if($sys) { $tag .= " -sys"; }
	$tag .= "\n";
	$tag .= $syntax[$type];
	$tag .= $endtag . "\n";
	return $tag;
}

sub max
{
	my($a,$b)=@_;
	return(($a>$b)?$a:$b);
}

sub usage
{
	print "Usage: $me [-add] [-remove] [-check] [-sys] [-all] [-help] [-shell=SHELL]\n";
	print " Adds code to your shell config to add $elvidir to your PATH.\n";
	print "Options:\n";
	print "\t-add          Add surfraw code.\n";
	print "\t-check        Check if surfraw code is installed (Default).\n";
	print "\t-remove       Remove Surfraw code.\n";
	print "\t-sys          Alter system files rather than user files.\n";
	print "\t-shell=SHELL  Specify shell to configure (defaults to \$SHELL).\n";
	print "\t-all          Configure all known shells.\n";
	print "\t-help         This help.\n";
	print "Supported shells: sh, ash, bash, dash, csh, tcsh, ksh, pdksh, zsh, rc, es\n";
}


__DATA__

=head1 NAME

surfraw-update-path - updates PATH in shell config files

=head1 SYNOPSIS

surfraw-update-path [B<-add>] [B<-remove>] [B<-check>] [B<-sys>] [B<-all>] [B<-help>] [B<-shell>=I<SHELL>] 

=head1 DESCRIPTION

surfraw-update-path adds the surfraw elvi directory (/usr/lib/surfraw) to
your PATH in your shell's config file.

Currently it supports B<bash>, B<sh>, B<csh>, B<tcsh>, B<ash>,
B<dash>, B<ksh>, B<pdksh>, B<zsh>, B<rc>, and B<es>

Don't forget to login again or source your login files for it to take
effect.

=head1 OPTIONS

=over 4

=item B<-check>

Checks to see if the surfraw config code is present.
This is the default.

=item B<-add>

Adds the surfraw config code.

=item B<-remove>

Removes the surfraw config code

=item B<-sys>

Updates the system-wide shell config instead of the user.
Must be done as root.

=item B<-shell>=I<SHELL>

Selects the shell to configure. Defaults to the value of the $SHELL
environment variable.

Currently supported shells are:

B<sh>, B<ash>, B<bash>, B<dash>, B<csh>, B<tcsh>, B<ksh>, B<pdksh>,
B<zsh>, B<rc>, and B<es>.

=item B<-all>

Attempts to configure the startup files for all known shells

=item B<-help>

Gives a usage message

=back

=head1 RETURN VALUE

-check returns 0 if the surfraw code is present in the file, 1 if it
is not found, or 2 on error.

All other options return 0 on success, or 2 on error.

=head1 ENVIRONMENT

=over 4

=item SHELL

Used to determine which shell to configure, if B<-shell> is not given.

=item HOME

Used to find users config files.

=item ENV

Used by posix-compliant shells to specify a startup rc file.

=item ZDOTDIR

Used to find user config files for zsh. If not set, defaults to HOME.

=back

=head1 SEE ALSO

surfraw(1), sh(1), ash(1), bash(1), dash(1), csh(1), tcsh(1), ksh(1),
pdksh(1), zsh(1), rc(1), es(1)

=head1 AUTHOR

Ian Beckwith <ianb@erislabs.net>

=cut