Debian Codegraph @

texk/texlive/linked_scripts/ketcindy/ketcindy.pl

raw
1 #!/usr/bin/env perl
2 #
3 # KETCindy starter script
4 #
5 # (C) 2017-2018 Norbert Preining
6 # Licensed under the same license terms as ketpic itself, that is GPLv3+
7 #
8
9 use strict;
10 $^W = 1;
11
12 use Digest::MD5;
13 use File::Copy;
14
15 my $BinaryName = "Cinderella2";
16 my $TemplateFile = "template1basic.cdy";
17 my $devnull = "/dev/null";
18 my $prog = "ketcindy";
19 my $systype;
20 if (win32()) {
21 $systype = "Windows";
22 $devnull = "nul";
23 } else {
24 $systype = `uname`;
25 chomp($systype);
26 }
27 my $HOME = ($systype eq "Windows") ? $ENV{'USERPROFILE'} : $ENV{'HOME'};
28 my $workdir ="$HOME/ketcindy";
29
30 my $cinderella;
31 if ($#ARGV >= 0) {
32 if ($ARGV[0] eq '-c') {
33 $cinderella = ($ARGV[1] ? $ARGV[1] : "");
34 }
35 } else {
36 $cinderella = which($BinaryName);
37 }
38
39 if (! "$cinderella") {
40 if ($systype eq 'Darwin') {
41 if (-r '/Applications/Cinderella2.app/Contents/MacOS/Cinderella2') {
42 $cinderella = '/Applications/Cinderella2.app/Contents/MacOS/Cinderella2';
43 }
44 } elsif ($systype eq 'Windows') {
45 if (-f 'c:/Program Files (x86)/Cinderella/Cinderella2.exe') {
46 $cinderella = 'c:/Program Files (x86)/Cinderella/Cinderella2.exe';
47 }
48 }
49 }
50
51 if (! "$cinderella") {
52 die "$prog: Cannot find $BinaryName!";
53 }
54
55 if ( ! -x "$cinderella" ) {
56 die "$prog: Program $cinderella is not executable!";
57 }
58
59 # find real path
60 my $realcind = win32() ? $cinderella : `realpath "$cinderella"`;
61 chomp($realcind);
62 my ($cinddir, $bn) = dirname_and_basename($realcind);
63
64 my $plugindir = ($systype eq 'Darwin') ? "$cinddir/../PlugIns" : "$cinddir/Plugins";
65
66 my $plugin = "$plugindir/KetCindyPlugin.jar";
67 my $dirheadplugin = "$plugindir/ketcindy.ini";
68
69 # find Jar
70 chomp(my $KetCdyJar = `kpsewhich -format=texmfscripts KetCindyPlugin.jar`);
71 # search for template.cdy
72 chomp(my $TempCdy = `kpsewhich -format=texmfscripts $TemplateFile`);
73 chomp(my $DirHead=`kpsewhich -format=texmfscripts ketcindy.ini`);
74
75 if (-z "$TempCdy" || -z "$KetCdyJar") {
76 die "$prog: Cannot find $TemplateFile via kpsewhich, is ketpic installed?";
77 }
78
79
80 if ( ! -r "$plugin" || ! -r "$dirheadplugin" ) {
81 print "$prog: Cinderella is *NOT* set up for KETCindy!\n";
82 print "$prog: You need to copy\n";
83 print "$prog: $KetCdyJar\n";
84 print "$prog: $DirHead\n";
85 print "$prog: into\n";
86 print "$prog: $plugindir\n";
87 print "\n";
88 exit(1);
89 }
90
91 my $myjarmd = md5digest($KetCdyJar);
92 my $sysjarmd = md5digest($plugin);
93
94 if ( $myjarmd ne $sysjarmd ) {
95 print "$prog: The installed version of the plugin in\n";
96 print "$prog: $plugin\n";
97 print "$prog: differs from the version shipped in\n";
98 print "$prog: $KetCdyJar\n";
99 print "$prog: You might need to update the former one with the later one!\n";
100 }
101
102 # print "DEBUG workdir =$workdir=\n";
103 # print "DEBUG TemplateFile =$TemplateFile=\n";
104 mkdir($workdir);
105 copy($TempCdy, $workdir) or die "$prog: Copy failed: $!";
106
107 # print "Exec $cinderella $workdir/$TemplateFile\n";
108 if (win32()) {
109 # no idea why a normal call with exec did not find the template file
110 my $out = `"$cinderella" "$workdir/$TemplateFile"`;
111 } else {
112 exec($cinderella, "$workdir/$TemplateFile");
113 }
114
115
116 sub md5digest {
117 my $file = shift;
118 open(FILE, $file) || die "$prog: open($file) failed: $!";
119 binmode(FILE);
120 my $out = Digest::MD5->new->addfile(*FILE)->hexdigest;
121 close(FILE);
122 return $out;
123 }
124
125 # taken from TeXLive::TLUtils.pm
126 sub win32 {
127 if ($^O =~ /^MSWin/i) {
128 return 1;
129 } else {
130 return 0;
131 }
132 }
133 sub which {
134 my ($prog) = @_;
135 my @PATH;
136 my $PATH = getenv('PATH');
137
138 if (&win32) {
139 my @PATHEXT = split (';', getenv('PATHEXT'));
140 push (@PATHEXT, ''); # in case argument contains an extension
141 @PATH = split (';', $PATH);
142 for my $dir (@PATH) {
143 for my $ext (@PATHEXT) {
144 if (-f "$dir/$prog$ext") {
145 return "$dir/$prog$ext";
146 }
147 }
148 }
149
150 } else { # not windows
151 @PATH = split (':', $PATH);
152 for my $dir (@PATH) {
153 if (-x "$dir/$prog") {
154 return "$dir/$prog";
155 }
156 }
157 }
158 return "";
159 }
160 sub dirname_and_basename {
161 my $path=shift;
162 my ($share, $base) = ("", "");
163 if (win32()) {
164 $path=~s!\\!/!g;
165 }
166 # do not try to make sense of paths ending with /..
167 return (undef, undef) if $path =~ m!/\.\.$!;
168 if ($path=~m!/!) { # dirname("foo/bar/baz") -> "foo/bar"
169 # eliminate `/.' path components
170 while ($path =~ s!/\./!/!) {};
171 # UNC path? => first split in $share = //xxx/yy and $path = /zzzz
172 if (win32() and $path =~ m!^(//[^/]+/[^/]+)(.*)$!) {
173 ($share, $path) = ($1, $2);
174 if ($path =~ m!^/?$!) {
175 $path = $share;
176 $base = "";
177 } elsif ($path =~ m!(/.*)/(.*)!) {
178 $path = $share.$1;
179 $base = $2;
180 } else {
181 $base = $path;
182 $path = $share;
183 }
184 return ($path, $base);
185 }
186 # not a UNC path
187 $path=~m!(.*)/(.*)!; # works because of greedy matching
188 return ((($1 eq '') ? '/' : $1), $2);
189 } else { # dirname("ignore") -> "."
190 return (".", $path);
191 }
192 }
193 sub getenv {
194 my $envvar=shift;
195 my $var=$ENV{"$envvar"};
196 return 0 unless (defined $var);
197 if (&win32) {
198 $var=~s!\\!/!g; # change \ -> / (required by Perl)
199 }
200 return "$var";
201 }
202
203