This file is indexed.

/usr/bin/ireminder is in itools 1.0-5+b1.

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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
#!/usr/bin/perl
# -*-Perl-*-

#---
# $Id: ireminder 1939 2006-10-11 20:52:09Z nadim $
#
# ------------
# Description:
# ------------
#  Copyright (c) 2006, Arabeyes, Nadim Shaikli
#
#  This is a prayer (and other Islamic events) reminder program.
#  Its a wrapper that uses Arabeyes.org's ITL package (ipraytime
#  to be specific) to attain all its values.  The script is meant
#  to be light, simple and flexible.
#
#  Based on a concept/idea from
#  - Ahmed El-Mahmoudy (aelmahmoudy -at- users sf net)
#
# -----------------
# Revision Details:     (Updated by Revision Control System)
# -----------------
#  $Date: 2006-10-11 23:52:09 +0300 (Wed, 11 Oct 2006) $
#  $Author: nadim $
#  $Revision: 1939 $
#  $Source$
#
#  (www.arabeyes.org - under GPL license)
#---

# TODO:
# - have it fork in the background if a similar process doesn't already
#   exists else kill the other process post a confirm (optionally)
# - have it print to the console not stdout (optionally)

# SAMPLES:
# - to engage a GUI:
#   ireminder -execr "xmessage %m -nearmouse -timeout 30"
#   ireminder -execr "xmessage %m" -execs "xmessage %m"
#   ireminder -execr "xmessage Next salat is %s"

use Getopt::Long;

use POSIX;
use FileHandle;
STDOUT->autoflush(1);

##
# Specify global variable values and init some Variables
$in_script	= $0;
$in_script	=~ s|^.*/([^/]*)|$1|;

##
# Find how many spaces the scripts name takes up (nicety)
$in_spaces	= $in_script;
$in_spaces	=~ s/\S/ /g;

$version	= " (v - %I%)";
$ID		= "[$in_script]";

# Process the command line
&get_args();

# Make it simple, does user need help ?
if ($opt{help}) { &help(); }

# Account for the various ways a user might enter a list
%opt		= %{&listify_input(\%opt, "skip")};
%opt		= %{&listify_input(\%opt, "reminder")};

# Account for various places the ipraytime binary could reside-in
@bin_paths	= ("/usr/bin/", "/usr/local/bin/", "");
foreach my $entry (@bin_paths)
{
    $bin_path = "${entry}ipraytime";
    if (-x $bin_path)
    {
	# OK, found something I can use
	last;
    }
}
$ipraytime	= $opt{ipraytime}	|| $bin_path;

# Set some default reminder settings
@reminders	= @{$opt{reminder}} ? @{$opt{reminder}} : (1, 5, 10, 20);

# Enumerate the events we'll track and report on
$DAY_TOT_SEC	= (24 * 60 * 60);
@EVENTS		= (qw/imsaak fajr shorooq zuhr asr maghrib isha/);

# Set some defaults
$doing_2moro	= 0;
$time_to_use	= time();

if (!-x $ipraytime)
{
    &usage(1, "can't find '$ipraytime'");
}

# Continuously cycle through
for (;;)
{
    # Get current start time
    $ref_time_used	= &get_time($time_to_use);
    %time_used		= %{$ref_time_used};

    # Specify date to get info on
    $date_to_use	= sprintf( "%d%02d%02d",
				   $time_used{year},
				   $time_used{month},
				   $time_used{day} );

    # Run the actual program executable and process its output
    $ref_today_events	= &get_ipraytime($date_to_use);
    %today_events	= %{$ref_today_events};

    # Cycle through all the prayers (and events)
    for ($i = 0; $i <= $#EVENTS; $i++)
    {
	# Skip any events a user might have noted on the command-line
 	if ( defined $opt{skip} )
 	{
 	    foreach my $option (@{$opt{skip}})
 	    {
		# Make sure to make the compare case insensitive
 		if ( uc($option) eq uc($EVENTS[$i]) )
		{
		    $skip_event	= 1;
		    last;
		}
 	    }

	    # If a skip-hit occured, cycle out of the for loop
	    if ($skip_event)
	    {
		$skip_event	= 0;
		next;
	    }
 	}

	# Determine current time's placement within event times
	$before_last	= ( ($time_used{hour} <
			     $today_events{$EVENTS[$#EVENTS]}{hour}) ||
			    (($time_used{hour} ==
			      $today_events{$EVENTS[$#EVENTS]}{hour}) &&
			     ($time_used{min} <
			      $today_events{$EVENTS[$#EVENTS]}{min})) );
	$after_current	= ( ($time_used{hour} >
			     $today_events{$EVENTS[$i]}{hour}) ||
			    (($time_used{hour} ==
			      $today_events{$EVENTS[$i]}{hour}) &&
			     ($time_used{min} >=
			      $today_events{$EVENTS[$i]}{min})) );

	# If we are dealing with next day (upon start), cycle through
	if (!$before_last && !$doing_2moro) { last; }

	# Skip the past while making sure we didn't go over
	if ($before_last && $after_current)
	{
	    next;
	}
	else
	{
	    # If I'm here, then its immaterial which day I'm dealing with
	    $doing_2moro	= 0;

	    # See how many seconds we have left till next prayer
	    if ($before_last)
	    {
		# Get time difference between prayer and wallclock time
		$sec_diff	= ( $today_events{$EVENTS[$i]}{tot_sec} -
				    $time_used{tot_sec} );
	    }
	    else
	    {
		# Dealing with next day prayer
		# - Get portion of day left in today + tomorrow's
		$sec_diff	= ( ($DAY_TOT_SEC - $time_used{tot_sec}) +
				    $today_events{$EVENTS[$i]}{tot_sec} );
	    }		    

	    # Account for the reminder time and the time delta seconds
	    $sec_sleep	= ( $sec_diff - $time_used{sec} );

	    # Tell user what to expect next
	    $str	 = "$ID NOTE - Next event '$EVENTS[$i]' is at ";
	    $str	.= "$today_events{$EVENTS[$i]}";
	    &display(0, $EVENTS[$i], $str);

	    # Sort numerically descending
	    foreach $reminder (sort {$b <=> $a} @reminders)
	    {
		$sec_reminder	= ( $reminder * 60 );
		if ($sec_sleep < $sec_reminder)
		{
		    next;
		}

		$sec_sleep	-= $sec_reminder;

		# Take a nap and wait :-)
		&do_reminder($EVENTS[$i],
			     $sec_sleep,
			     \%today_events,
			     $reminder);

		$sec_sleep	= $sec_reminder;
	    }

	    # Take a nap and wait (without reminders :-)
	    &do_reminder($EVENTS[$i],
			 $sec_sleep,
			 0,
			 0);

	    &do_alarm($EVENTS[$i], \%today_events);

	    # Take a nap and wait before next event/reminder
	    sleep(300);

	    # Establish new time for next prayer
	    # - This is opted for instead of accounting for passed sec's
	    $ref_time_used	= &get_time(time());
	    %time_used		= %{$ref_time_used};
	}
    }

    # Cycle through & advance calc 1 day forward (ie. look at next day)
    $time_to_use	= ( time() + $DAY_TOT_SEC );
    $doing_2moro	= 1;
}

       ###        ###
######## Procedures ########
       ###        ###

##
# Print on screen passed-in string
sub display
{
    my ($salat_now,
	$salat_name,
	$string)	= @_;

    # See if user is opting to use his/her own external trigger
    if (defined $opt{execr} || ($salat_now && defined $opt{execs}))
    {
	# Escape various characters just in case external application cares
	$string		=~ s/\'/\\\'/g;
	$string		=~ s/\(/\\\(/g;
	$string		=~ s/\)/\\\)/g;

	# Kill a previous application in case its still running
	# - session/group PIDs (note '-')
	if ( $cpid ) { kill "KILL", -$cpid; }

	$ext_trigger	=  ( $salat_now ? $opt{execs} : $opt{execr} );

	# Do various user substitutions
	# - %m for normal printable message string
	# - %s for salat name string
	$ext_trigger	=~ s/\%m/$string/g;
	$ext_trigger	=~ s/\%s/$salat_name/g;
	$cpid		= &fork_exec("$ext_trigger");
    }
    else
    {
	# Do some clean-up and backtrack per use command-line directives
	if ($opt{inplace})
	{
	    print "\r";
	    print " " x 78;
	    print "\r";
	}
	print $string;

	if (!$opt{inplace})
	{
	    print "\n";
	}
    }
}

##
# Adjust user input to end-up with a proper array
sub listify_input
{
    my ($ref_in_opt,
	$in_str)	= @_;

    my (
	%in_opt,
	@in_arr,
	@mod_arr,
       );

    %in_opt		= %{$ref_in_opt};
    @in_arr		= @{$in_opt{$in_str}};

    # Make sure to accept for instance,
    # 1. -skip one -skip two
    # 2. -skip "one two"
    # 3. -skip "one, two"
    foreach my $string (@in_arr)
    {
	if ($string =~ /\s+/)
	{
	    my @sub_string	= split(/[,]*\s+/, $string);
	    push (@mod_arr, @sub_string);
	    next;
	}
	push (@mod_arr, $string);
    }

    $in_opt{$in_str}	= \@mod_arr;
    return(\%in_opt);
}

##
# Populate time hash/struct using the passed-in time value
sub get_time
{
    my ($in_time)	= @_;

    my (
	%tm,
       );

    # seconds, minutes, hours, day, month, year, day.week, day.year, DST
    ($tm{sec},
     $tm{min},
     $tm{hour},
     $tm{day},
     $tm{month},
     $tm{year},
     $tm{dayofweek},
     $tm{dayofyear},
     $tm{dst})		= localtime($in_time);

    # Some reability corrections
    $tm{month}		= ($tm{month} + 1);
    $tm{year}		= (1900 + $tm{year});

    # Set some needed values
    $tm{tot_min}	= ($tm{min} + (60 * $tm{hour}));
    $tm{tot_sec}	= (60 * $tm{tot_min});

    return(\%tm);
}

##
# Populate event hash/struct based on ipraytime's output using date passed-in
sub get_ipraytime
{
    my ($in_date)	= @_;

    my (
	$aux,
	@out_ipray,
	$ipray,
	@times,
       );

    if (defined $in_date)
    {
	$aux	= "--date $in_date ";
    }

    @out_ipray	= `$ipraytime $aux`;

    #     Date         Fajr    Shorooq   Zuhr     Asr    Maghrib   Isha 
    #--------------------------------------------------------------------
    # [22-07-2004]     5:22     6:50    13:27    16:43    20:04    21:34
    #
    #Today's Imsaak    :   5:17

    foreach my $line (@out_ipray)
    {
	# Remove the carriage returns
	chomp($line);

	# Find the proper line to process
	if ($line =~ /\[\d+-\d+-\d+\]/)
	{
	    @entries		= split (/\s\s+/, $line);
	    $ipray{fajr}	= $entries[1];
	    $ipray{shorooq}	= $entries[2];
	    $ipray{zuhr}	= $entries[3];
	    $ipray{asr}		= $entries[4];
	    $ipray{maghrib}	= $entries[5];
	    $ipray{isha}	= $entries[6];
	}

	# Grab today's imsaak time
	if ($line =~ /^Today.*Imsaak\s*:\s*(\d+:\d+)\s*/)
	{
	    $ipray{imsaak}	= $1;
	}
    }

    # Get the total number of seconds for each prayer and event
    for (my $i = 0; $i <= $#EVENTS; $i++)
    {
	@times				= split(/\:/, $ipray{$EVENTS[$i]});
	$ipray{$EVENTS[$i]}{hour}	= $times[0];
	$ipray{$EVENTS[$i]}{min}	= $times[1];
	$ipray{$EVENTS[$i]}{tot_min}	= ($ipray{$EVENTS[$i]}{min} +
					   (60 * $ipray{$EVENTS[$i]}{hour}));
	$ipray{$EVENTS[$i]}{tot_sec}	= (60 * $ipray{$EVENTS[$i]}{tot_min});

    }

    # Return the newly populated hash/structure
    return (\%ipray);
}

##
# Trigger a reminder
sub do_reminder
{
    my ($in_name,
	$in_time,
	$ref_event_struct,
	$in_rem)		= @_;

    my (
	$name,
	$str,
       );

    %event_struct	= %{$ref_event_struct};
    $name		= ucfirst($in_name);

    # Take a nap and wait
    sleep($in_time);
    
    # See if a reminder note is warranted
    if ($in_rem > 0)
    {

      NAME_CASE:
	{
	    if ($in_name eq "imsaak")
	    {
		$str = "Fasting is to commence ($event_struct{$in_name})";
		last NAME_CASE;
	    }
	    if ($in_name eq "shorooq")
	    {
		$str = "Salat Al-Fajr expires ($event_struct{$in_name})";
		last NAME_CASE;
	    }
	    #default
	    $str = "Salat Al-$name ($event_struct{$in_name})";
	}

	&play_sound();
	&display(0, $in_name, "$ID REMINDER - $str in $in_rem minutes !!");
    }
}

##
# Trigger the alarm
sub do_alarm
{
    my ($in_name,
	$ref_event_struct)	= @_;

    my (
	$name,
	$event_struct,
	$num_rings,
	$str,
       );

    $name		= ucfirst($in_name);
    %event_struct	= %{$ref_event_struct};
    $num_rings		= 4;

  NAME_CASE:
    {
	if ($in_name eq "imsaak")
	{
	    $str	= "to start your Fast";
	    last NAME_CASE;
	}
	if ($in_name eq "shorooq")
	{
	    $str	= "notes passing of Salat Al-Fajr";
	    last NAME_CASE;
	}
	#default
	$str		= "for Salat Al-$name";
    }

    &display(1, $in_name, "$ID $event_struct{$in_name} - Time NOW $str !!");

    for(my $i = 0; $i < $num_rings; $i++)
    {
	&play_sound();
	sleep(1);
    }
}

##
# Play a sound file (if possible) else gimme a beep :-)
sub play_sound
{
    print "\a";
}

##
# Use this function instead of "system" (to catch Interrupts/^C and/or kills)
sub fork_exec
{
    my ($command) = @_;

  FORK_IT:
    {
	if ( $child_pid = fork )          # parent here
	{
	    return($child_pid);
	}
	elsif ( defined $child_pid )      # child here (PID --> 0)
	{
	    &POSIX::setsid();             # set current PID (child) to be session/group leader
	    exec "$command" or &err(1, "Can't exec '$command': $!");
	}
	elsif ( $! =~ /no more/i )        # No more Processes (sleep & retry ?)
	{
#	    sleep 5; redo FORK_IT;
	    &err(1, "No more processes !!!");
	}
	else                              # weird fork error
	{
	    &err(1, "Can't fork: $!");
	}
    }
}

##
# Dump out an error message and possibly quit
sub err
{
    my ($die_after,
	$msg)		= @_;

    print "$ID ERROR: $msg\n";

    if ( $die_after ) { exit(1); }
}

##
# Print short usage info
sub usage
{
    my ($die_after,
	$err_msg)	= @_;

#    if (defined $err_msg) { print $err_msg; }
    if (defined $err_msg) { &err(0, $err_msg); }
    print qq
|Usage: $in_script [-ipraytime path]
       $in_spaces [-skip event_name]
       $in_spaces [-reminder minutes]
       $in_spaces [-inplace]
       $in_spaces [-execr cmd]
       $in_spaces [-execs cmd]
       $in_spaces [-help]
|;
    if ( $die_after ) { exit(5); }
}

##
# Print one-liner help
sub help
{
    &usage(0);

    print qq|
-> remind user visually and via audio of Islamic prayer times & misc events

  Options:

    [-ipraytime path]   : Specify the executable path to program
    [-skip event_name]  : Specify name of prayer/event to skip (list ok)
    [-reminder minutes] : Specify prior to how many minutes to remind (list ok)
    [-inplace]          : Specify to print output in-place (without scrolling)
    [-execr cmd]        : Specify an external application trigger for reminders
    [-execs cmd]        : Specify an external application trigger for salat NOW
    [-help]             : Produce this help screen

|;
    exit(10);
}

##
# Get the command line arguments
sub get_args
{
    &GetOptions(
		\%opt,		# Hash to store all input options in
		"ipraytime=s",	# specify the binary ipraytime executable
		"skip=s@",	# array'ed list of skip events
		"reminder=s@",	# array'ed list of reminder times (minutes)
		"inplace",	# specify to print in-place outputs (no scroll)
		"execr=s",	# specify an external trigger (for reminders)
		"execs=s",	# specify an external trigger (for salat_now)
		"help",		# print a brief help screen
		) || ( $? = 257, &usage(1, "Invalid argument !!") );
}