This file is indexed.

/usr/bin/gpsh is in hxtools 20170430-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
#!/usr/bin/perl
#
#	gpsh
#	A script to start playing files from your music archive
#	by grepping on their filename, or title (in case of mixes).
#	Written by Jan Engelhardt, 2010
#
#	This program is free software; you can redistribute it and/or
#	modify it under the terms of the WTF Public License version 2 or
#	(at your option) any later version.
#

use Data::Dumper;
use File::Find::Rule;
use Getopt::Long;
use strict;
my @opt_mplayer;
my $opt_nomixes;
my $opt_verbose;
my @meta_exts = qw(.txt .m3u .tls .lyr .lop);

&main();

sub main
{
	my $opt_idxfile = "index.m9u",
	my $opt_shuffle = 1;
	my $opt_listonly;
	my $rebuild_index;
	my $index;
	my $queue;

	$SIG{INT} = $SIG{QUIT} = sub { exit(0); };

	&Getopt::Long::Configure(qw(bundling pass_through));
	&GetOptions(
		"F" => \$opt_idxfile,
		"b" => \$rebuild_index,
		"l" => \$opt_listonly,
		"x" => \$opt_nomixes,
		"z" => sub { $opt_shuffle = 0 },
		"v" => \$opt_verbose,
		"af=s" => sub {
			push(@opt_mplayer, "-af", $_[1]);
		},
		"loop=s" => sub {
			push(@opt_mplayer, "-loop", $_[1]);
		},
		"O=s" => sub {
			if ($_[1] =~ s/^M,//) {
				push(@opt_mplayer, $_[1]);
			}
		},
	);
	if ($opt_listonly) {
		$opt_verbose = 1;
	}
	if (scalar(@ARGV) == 0) {
		@ARGV = ("");
	}
	foreach my $arg (@ARGV) {
		if (substr($arg, 0, 1) ne "-") {
			next;
		}
		print STDERR "\"$arg\" not recognized as option, ",
			"interpreting it as search keyword instead.\n";
	}

	if ($rebuild_index) {
		$index = &index_rebuild($opt_idxfile);
	} elsif (!-e $opt_idxfile) {
		print STDERR "[$$] No index file found at current level\n";
		$index = &index_rebuild($opt_idxfile);
	} elsif (-M $opt_idxfile > 1 && $opt_listonly) {
		print STDERR "[$$] Index file older than a day\n";
		$index = &index_rebuild($opt_idxfile);
	} elsif (-M $opt_idxfile > 1) {
		$index = &index_read($opt_idxfile);
		$queue = &queue_select($index, \@ARGV);
		# Avoid backgrounding if we have nothing to play
		if (scalar(@$queue) > 0) {
			print STDERR "[$$] Index file older than a day\n";
			&schedule(\&index_rebuild, $opt_idxfile);
		} else {
			$index = &index_rebuild($opt_idxfile);
			$queue = &queue_select($index, \@ARGV);
		}
	} else {
		$index = &index_read($opt_idxfile);
	}

	if (!defined($queue)) {
		$queue = &queue_select($index, \@ARGV);
	}
	if ($opt_listonly) {
		return;
	}
	if ($opt_shuffle) {
		@$queue = sort s_random sort { $a cmp $b } @$queue;
	}
	&queue_play($index, $queue);
}

sub basename
{
	my $s = shift @_;
	$s =~ s{.*/}{}s;
	return $s;
}

sub queue_select
{
	my($index, $argv) = @_;
	my $queue = [];

	print "[$$] Index has ", scalar(keys %$index), " entries\n";
	foreach my $arg (@$argv) {
		push(@$queue, sort grep {
			($_ =~ /$arg/i || $arg eq $index->{$_}->{file}) &&
			(!$opt_nomixes || !exists($index->{$_}->{"ofs"}))
		} keys %$index);
	}
	if ($opt_verbose) {
		foreach (@$queue) {
			print "[$$]   \\_ $_\n";
		}
	}
	print "[$$] Queue has ", scalar(keys @$queue), " entries\n";
	return $queue;
}

sub filename_nonext
{
	my $ext = ($_[0] =~ /^(.*)\.[^\.]+$/)[0];
	return defined($ext) ? $ext : $_[0];
}

sub filename_ext
{
	my($ext) = (shift(@_) =~ /(\.[^\.]+)$/);
	return $ext;
}

sub filename_meta
{
	my $ext = shift @_;

	foreach my $t (@meta_exts) {
		if ($ext eq $t) {
			return 1;
		}
	}
	return 0;
}

sub filename_timidity
{
	my $ext = lc shift @_;

	foreach my $t (qw(.mid .mus .rmi)) {
		if ($ext eq $t) {
			return 1;
		}
	}
	return 0;
}

sub filename_xmp
{
	my $ext = lc shift @_;

	foreach my $t (qw(.it .s3m .xm .mod .xm2 .j2b .psm)) {
		if ($ext eq $t) {
			return 1;
		}
	}
	return 0;
}

sub queue_play
{
	my $index = shift @_;
	my $queue = shift @_;

	foreach my $title (@$queue) {
		my $entry = $index->{$title};
		my $ofs = 0;

		if (exists $entry->{ofs}) {
			$ofs = $entry->{ofs};
		}
		if (exists $entry->{parent}) {
			$entry = $entry->{parent};
		}

		my $file = $entry->{file};
		my $ext = &filename_ext($file);
		$ext = lc $ext;

		&print_lyr($title);
		print STDERR "\e[1;31m$title\e[0m\n"; # ]]
		if (&filename_timidity($ext)) {
			# my $t = $opt_verbose ? "t" : "";
			system "timidity", "-Os", "-idt", $file;
			next;
		} elsif (&filename_xmp($ext)) {
			system "xmp", $file;
			next;
		}
		if ($ext eq ".mp3" && exists($entry->{vbr})) {
			if ($opt_nomixes) {
				print STDERR "\e[31m(VBR file and reindexing disabled)\e[0m\n";
			} else {
				$file = &workaround_mp3vbr($file);
			}
		}
		if ($ofs > 0) {
			--$ofs;
		}
		system "mplayer", "-vo", "null", $file, "-ss", $ofs, @opt_mplayer;
	}
}

sub print_lyr
{
	# A lyr file for a version may be available in a basename.
	# If so, print a line notifying about its existence.
	my $title = shift @_;
	my $title2 = $title;
	do {
		if (-e "$title2.lyr") {
			print STDERR "\e[1;32m[lyr]\e[0m "; # ]]
			return;
		}
	} while ($title2 =~ s{\s*(?:\([^\)]+\)|\[[^\]]+\])$}{});
	if (-e "$title.lyr") {
		print STDERR "\e[1;32m[lyr]\e[0m "; # ]]
	}
}

sub workaround_mp3vbr
{
	# MP3 ABR/VBR files have no seek index. Players fall back to computing
	# the position using CBR semantics. (That is faster than reindexing it
	# *everytime*, and thus a reasonable default.) However, accessing mixes
	# requires a functional seek index, so reindex the audio file *and
	# keep it* (which players doing on-the-fly-reindex don't - which would
	# make it even more costly).
	my $ifile = shift @_;
	my $ofile = $ifile;
	my $tmpdir = "/tmp/psh.$>";
	my @stat = stat($ifile);

	if (!defined($stat[0])) {
		return $ifile;
	}
	#
	# Use a path-independent name, so that gpsh can be run from
	# subdirectories and not cause remuxing a file.
	#
	$ofile = "$tmpdir/$stat[0].$stat[1]";
	if (-e $ofile) {
		return $ofile;
	}
	if (!-e $tmpdir) {
		if (!mkdir($tmpdir)) {
			print "[$$] Could not create $tmpdir: $!\n";
			return $ifile;
		}
	}
	system "mkvmerge", "-o", $ofile, $ifile;
	return $ofile;
}

sub play_umx
{
	my $name = shift @_;
	my $buffer;

	if (!open(IN, "< $name")) {
		warn "[$$] Could not open $name: $!\n";
		return;
	}

	my $of = "/tmp/playmuch-$$.it";
	open(OUT, "> $of");
	read(IN, $buffer, 256);
	$buffer =~ s{^.*?(?=IMP|SCRM|Extended Module)}{}ogs;
	print OUT $buffer;
	while (read(IN, $buffer, 65536)) {
		print OUT $buffer;
	}
	close IN;
	close OUT;

	system "timidity", "-Os", "-id".($opt_verbose ? "t" : ""), $of;
	unlink $of;
}

#
# Run a sub in the background.
#
sub schedule
{
	my $sub = shift @_;
	my $pid = fork();

	$SIG{CHLD} = "IGNORE";
	if (!defined($pid)) {
		die "[$$] Could not schedule subprocess: $!";
		return 0;
	} elsif ($pid == 0) {
		&$sub(@_);
		exit(0);
	}
}

sub audio_file_for
{
	my($file) = (shift(@_) =~ m{([^/]*)\.tls$});
	my $dir = $`;
	if ($dir eq "") {
		$dir = ".";
	}
	my $obj = File::Find::Rule->file();
	foreach my $t (@meta_exts) {
		$obj->not_name("*$t");
	}
	my @a = $obj->name("$file.*")->in($dir);
	return shift @a;
}

sub index_rebuild
{
	my $idxfile = shift @_;
	local(*DB, *FH);
	my @audio;
	my $track_list = {};

	print "[$$] Rebuilding index\n";
	@audio = File::Find::Rule->not_symlink()->file()->in(".");

	foreach my $file (@audio) {
		my $ext = &filename_ext($file);
		if ($file =~ m{_noindex/}) {
			next;
		}
		if (&filename_meta($ext)) {
			next;
		}
		my $title = &filename_nonext($file);
		# Collect with extension to enable tabbing on the shell
		$track_list->{$title} = {"file" => $file};
	}

	foreach my $file (grep(/\.tls$/, @audio)) {
		my $af = &audio_file_for($file);
		if (!defined($af)) {
			print "[$$]  \\__ No audio for $file\n";
			next;
		}

		# Mark this title as being a mix and/or part of a mix.
		# In this case, we do not want the extension,
		# to facilitate grepping for e.g. "mix 01,02".
		my $parent_title = &filename_nonext($af);
		$track_list->{$parent_title}->{"ofs"} = 0;

		if (!open(FH, "< $file")) {
			next;
		}
		while (defined(my $line = <FH>)) {
			chomp $line;
			if ($line =~ m{^<<VBR>>}) {
				# vivify hash entry, but don't waste
				# memory for a value like 1
				$track_list->{$parent_title}->{"vbr"} = undef;
				next;
			}
			my($h, $m, $s, $title) =
			    ($line =~ m{^\[\s*(?:(\d+):)?(\d+):(\d+)\]\s+(.+)});
			if (!defined($title)) {
				next;
			}
			$s = $h * 3600 + $m * 60 + $s;
			$track_list->{"$parent_title,$title"} = {
				"ofs" => $s,
				"parent" => $track_list->{$parent_title},
			};
		}
		close FH;
	}

	if (!open(DB, "> $idxfile")) {
		die "[$$] Could not write to $idxfile: $!\n";
	}
	$Data::Dumper::Indent = 0;
	$Data::Dumper::Purity = 1;
	print DB Dumper($track_list);
	close DB;
	return $track_list;
}

sub index_read
{
	my $idxfile = shift @_;
	do $idxfile || die "[$$] Could not read from $idxfile: $!\n";
	return $::VAR1;
}

sub s_random
{
	return int(rand 2) ? 1 - (int(rand(256 ** 4)) % 3) :
	       ((time() ^ $$ ^ int(rand(256 ** 4))) % 3) - 1;
}