1#!perl -w
2#
3# $Id: exe_wrap.pl,v 1.101 2017/11/28 05:28:04 rmeden Exp $
4# This is a quick XMLTV shell routing to use with the windows exe
5#
6# A single EXE is needed to allow sharing of modules and dlls of all the
7# programs.  If PerlAPP was run on each one, the total size would be more than
8# 12MB, even leaving out PERL56.DLL!
9#
10# Perlapp allows you to attach pathed files, but you need the same path
11# to access them.  The Makefile creates a text file of these files which is
12# used to build a translation table, allowing users to just type the app name
13# and not the development path.
14#
15# Robert Eden rmeden@yahoo.com
16#
17
18use File::Basename;
19use Carp;
20
21$Carp::MaxEvalLen=40; # limit confess output
22
23#
24# this check should not be done, at least not this way. it prevents some regular expressions!
25#
26## Check for error of running from 'Run' dialogue box with redirection,
27## which Run doesn't understand,
28##
29#if (grep /[<>|]/, @ARGV) {
30#    warn <<END
31#The command line:
32#
33#$0 @ARGV
34#
35#contains redirections, so should be run from a command prompt window.
36#In general, it's a good idea to always run xmltv from a command prompt
37#so that you can see any errors and warnings produced.
38#END
39#      ;
40#    sleep 10;
41#    exit 1;
42#}
43
44#
45# check for --quiet
46#
47my $opt_quiet=0;
48foreach (@ARGV) {$opt_quiet = 1 if /--quiet/i };
49
50#
51# get/check time zone
52#
53unless (exists $ENV{TZ})
54{
55    my $now    =  time();
56    my $lhour  = (localtime($now))[2];
57    my $ghour  = (   gmtime($now))[2];
58    my $tz     = ($lhour - $ghour);
59       $tz    -= 24 if $tz >  12;
60       $tz    += 24 if $tz < -12;
61       $tz= sprintf("%+03d00",$tz);
62
63       $ENV{TZ}= $tz;
64
65} #timezone
66print STDERR "Timezone is $ENV{TZ}\n" unless $opt_quiet;
67
68
69$cmd = shift || "";
70
71# --version (and abbreviations thereof)
72my $VERSION = '0.5.70';
73if (index('--version', $cmd) == 0 and length $cmd >= 3) {
74    print "xmltv $VERSION\n";
75    exit;
76}
77
78#
79# some programs use a "share" directory
80#
81if ($cmd eq 'tv_grab_na_dd',
82 or $cmd eq 'tv_grab_na_icons',
83 )
84{
85    unless (grep(/^--share/i,@ARGV))  # don't add our --share if one supplied
86    {
87        my $dir = dirname(PerlApp::exe()); # get full program path
88        $dir =~ s!\\!/!g;      # use / not \
89#       die "EXE path contains spaces.  This is known to cause problems.\nPlease move xmltv.exe to a different directory\n" if $dir =~ / /;
90        $dir .= "/share/xmltv";
91    	unless (-d $dir )
92    	    {
93	        die "directory $dir not found\n If not kept with the executable, specify with --share\n"
94	        }
95        print STDERR "adding '--share=$dir'\n" unless $opt_quiet;
96        push @ARGV,"--share",$dir;
97    }
98}
99
100#
101# special hack, allow "exec" to execute an arbitrary script
102# This will be used to allow XMLTV.EXE modules to be used on beta code w/o an alpha exe
103#
104# Note, no extra modules are included in the EXE.  There is no guarantee this will work
105# it is an unsupported hack.
106#
107# syntax XMLTV.EXE exec filename --options
108#
109if ($cmd eq 'exec')
110{
111   my $exe=shift;
112   $0=$exe;
113   do $exe;
114   print STDERR $@ if length($@);
115   exit 1 if length($@);
116   exit 0;
117}
118
119#
120# scan through attached files and execute program if found
121#
122$files=PerlApp::get_bound_file("exe_files.txt");
123foreach my $exe (split(/ /,$files))
124{
125    next unless length($exe)>3; #ignore trash
126    $_=$exe;
127    s!^.+/!!g;
128    $cmds{$_}=1;  # build command list (just in case)
129
130    next unless $cmd eq $_;
131
132#
133# execute our command
134#
135    $0 = $_;        # set $0 to our script
136    do $exe;
137    print STDERR $@ if length($@);
138    exit 1 if length($@);
139    exit 0;
140}
141
142#
143# command not found, print error
144#
145if ($cmd eq "" )
146   {
147	print STDERR "you must specify the program to run\n    for example: $0 tv_grab_fi --configure\n";
148    }
149else
150   {
151    print STDERR "$cmd is not a valid command.\n";
152   }
153
154print STDERR "Valid commands are:\n";
155@cmds=sort keys %cmds;
156$rows = int($#cmds / 3)+1;
157
158map {$_='' unless defined $_} @cmds[0..($rows*3+2)];
159unshift @cmds,undef;
160
161foreach (1..$rows)
162{
163   printf STDERR "    %-20s %-20s %-20s\n",@cmds[$_,$rows+$_,2*$rows+$_];
164}
165exit 1;
166
167