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
|
#!/usr/bin/env perl
#
# KETCindy starter script
#
# (C) 2017-2018 Norbert Preining
# Licensed under the same license terms as ketpic itself, that is GPLv3+
#
use strict;
$^W = 1;
use Digest::MD5;
use File::Copy;
my $BinaryName = "Cinderella2";
my $TemplateFile = "template1basic.cdy";
my $devnull = "/dev/null";
my $systype;
if (win32()) {
$systype = "Windows";
$devnull = "nul";
} else {
$systype = `uname`;
chomp($systype);
}
my $HOME = ($systype eq "Windows") ? $ENV{'USERPROFILE'} : $ENV{'HOME'};
my $workdir ="$HOME/ketcindy";
my $cinderella;
if ($#ARGV >= 0) {
if ($ARGV[0] eq '-c') {
$cinderella = ($ARGV[1] ? $ARGV[1] : "");
}
} else {
$cinderella = which($BinaryName);
}
if (! "$cinderella") {
if ($systype eq 'Darwin') {
if (-r '/Applications/Cinderella2.app/Contents/MacOS/Cinderella2') {
$cinderella = '/Applications/Cinderella2.app/Contents/MacOS/Cinderella2';
}
} elsif ($systype eq 'Windows') {
if (-f 'c:/Program Files (x86)/Cinderella/Cinderella2.exe') {
$cinderella = 'c:/Program Files (x86)/Cinderella/Cinderella2.exe';
}
}
}
if (! "$cinderella") {
die "Cannot find $BinaryName!";
}
if ( ! -x "$cinderella" ) {
die "Program $cinderella is not executable!";
}
# find real path
my $realcind = win32() ? $cinderella : `realpath "$cinderella"`;
chomp($realcind);
my ($cinddir, $bn) = dirname_and_basename($realcind);
my $plugindir = ($systype eq 'Darwin') ? "$cinddir/../PlugIns" : "$cinddir/Plugins";
my $plugin = "$plugindir/KetCindyPlugin.jar";
my $dirheadplugin = "$plugindir/ketcindy.ini";
# find Jar
chomp(my $KetCdyJar = `kpsewhich -format=texmfscripts KetCindyPlugin.jar`);
# search for template.cdy
chomp(my $TempCdy = `kpsewhich -format=texmfscripts $TemplateFile`);
chomp(my $DirHead=`kpsewhich -format=texmfscripts ketcindy.ini`);
if (-z "$TempCdy" || -z "$KetCdyJar") {
die "Cannot find $TemplateFile via kpsewhich, is ketpic installed?";
}
if ( ! -r "$plugin" || ! -r "$dirheadplugin" ) {
print "Cinderella is *NOT* set up for KETCindy!\n";
print "You need to copy\n";
print " $KetCdyJar\n";
print " $DirHead\n";
print "into\n";
print " $plugindir\n";
print "\n";
exit(1);
}
my $myjarmd = md5digest($KetCdyJar);
my $sysjarmd = md5digest($plugin);
if ( $myjarmd ne $sysjarmd ) {
print "The installed version of the plugin in\n";
print " $plugin\n";
print "differs from the version shipped in\n";
print " $KetCdyJar\n";
print "You might need to update the former one with the later one!\n";
}
# print "DEBUG workdir =$workdir=\n";
# print "DEBUG TemplateFile =$TemplateFile=\n";
mkdir($workdir);
copy($TempCdy, $workdir) or die "Copy failed: $!";
# print "Exec $cinderella $workdir/$TemplateFile\n";
if (win32()) {
# no idea why a normal call with exec did not find the template file
my $out = `"$cinderella" "$workdir/$TemplateFile"`;
} else {
exec($cinderella, "$workdir/$TemplateFile");
}
sub md5digest {
my $file = shift;
open(FILE, $file) || die "open($file) failed: $!";
binmode(FILE);
my $out = Digest::MD5->new->addfile(*FILE)->hexdigest;
close(FILE);
return $out;
}
# taken from TeXLive::TLUtils.pm
sub win32 {
if ($^O =~ /^MSWin/i) {
return 1;
} else {
return 0;
}
}
sub which {
my ($prog) = @_;
my @PATH;
my $PATH = getenv('PATH');
if (&win32) {
my @PATHEXT = split (';', getenv('PATHEXT'));
push (@PATHEXT, ''); # in case argument contains an extension
@PATH = split (';', $PATH);
for my $dir (@PATH) {
for my $ext (@PATHEXT) {
if (-f "$dir/$prog$ext") {
return "$dir/$prog$ext";
}
}
}
} else { # not windows
@PATH = split (':', $PATH);
for my $dir (@PATH) {
if (-x "$dir/$prog") {
return "$dir/$prog";
}
}
}
return "";
}
sub dirname_and_basename {
my $path=shift;
my ($share, $base) = ("", "");
if (win32()) {
$path=~s!\\!/!g;
}
# do not try to make sense of paths ending with /..
return (undef, undef) if $path =~ m!/\.\.$!;
if ($path=~m!/!) { # dirname("foo/bar/baz") -> "foo/bar"
# eliminate `/.' path components
while ($path =~ s!/\./!/!) {};
# UNC path? => first split in $share = //xxx/yy and $path = /zzzz
if (win32() and $path =~ m!^(//[^/]+/[^/]+)(.*)$!) {
($share, $path) = ($1, $2);
if ($path =~ m!^/?$!) {
$path = $share;
$base = "";
} elsif ($path =~ m!(/.*)/(.*)!) {
$path = $share.$1;
$base = $2;
} else {
$base = $path;
$path = $share;
}
return ($path, $base);
}
# not a UNC path
$path=~m!(.*)/(.*)!; # works because of greedy matching
return ((($1 eq '') ? '/' : $1), $2);
} else { # dirname("ignore") -> "."
return (".", $path);
}
}
sub getenv {
my $envvar=shift;
my $var=$ENV{"$envvar"};
return 0 unless (defined $var);
if (&win32) {
$var=~s!\\!/!g; # change \ -> / (required by Perl)
}
return "$var";
}
|