File: AMC-latex-link.pl.in

package info (click to toggle)
auto-multiple-choice 1.4.0-2~exp1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 18,520 kB
  • sloc: perl: 23,839; xml: 20,920; cpp: 1,791; makefile: 472; ansic: 186; sh: 80
file content (160 lines) | stat: -rw-r--r-- 4,141 bytes parent folder | download | duplicates (2)
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
#! /usr/bin/perl
#
# Copyright (C) 2011-2017 Alexis Bienvenue <paamc@passoire.fr>
#
# This file is part of Auto-Multiple-Choice
#
# Auto-Multiple-Choice is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# Auto-Multiple-Choice is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty
# of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Auto-Multiple-Choice.  If not, see
# <http://www.gnu.org/licenses/>.

######################################################################
#
# auto-multiple-choice latex-link :
#
# Tests if the style file automultiplechoice.sty is accessible for
# LaTeX. If not, tries to make a link from a location inside texmf
# directories to the installed style file, and calls mktexlsr
#
# auto-multiple-choice latex-link remove :
#
# remove the link.
#
######################################################################

use AMC::Basic;

for my $c (qw/kpsewhich texconfig-sys/) {
    if(!commande_accessible($c)) {
	print STDERR "ERROR: I don't find the command $c. Perhaps LaTeX is not installed?\n";
	exit(1);
    }
}

if($> != 0) {
    print "WARNING: This command should be called by root!\n";
}

sub get_tex_var {
    my ($k)=@_;
    my $v='';
    open(SYS,"-|","texconfig-sys","conf")
	or die "Can't exec texconfig-sys: $!";
    for(<SYS>) {
	chomp;
	$v=$1 if(/^$k=(.*)/);
    }
    close(SYS);
    return($v);
}


my $conf_dir="/etc/AMC";
my $link_file=$conf_dir."/latex-link";
my $link='';

if(-f $link_file) {
    open(LK,$link_file);
    while(<LK>) {
	chomp;
	$link=$_;
    }
    close(LK);
}

my @styles=get_sty();

sub rehash {
    print "Calling mktexlsr to refresh LaTeX files list...\n";
    open(LSR,"-|","mktexlsr", "--quiet");
    while(<LSR>) {
	print $_;
    }
    close(LSR);
    @styles=get_sty();
}

if($ARGV[0] =~ /^(remove|rm)$/i) {
    if(-l $link) {
	print "Removing link $link...\n";
	unlink($link);
	if(-e $link) {
	    die "ERROR: could not remove link $link.\n";
	    exit(2);
	} else {
	    print("Done.\n");
	}
    } else {
	print "No link to remove.\n";
    }
    exit(0);
}

if(!@styles) {
    rehash();
}

if(@styles) {
    print "The style file is already accessible:\n";
    for(@styles) {
	print "$_\n";
    }
    if(-l $link) {
	print "Use 'auto-multiple-choice latex-link remove' to remove the link\n";
    }
} else {
    my $loc=get_tex_var('TEXMFLOCAL');
    if(! -d $loc) {
        print STDERR "WARNING: the directory TEXMFLOCAL does not exist ($loc).\n";
        print STDERR "WARNING: trying to create $loc...\n";
        mkdir("$loc");
    }
    my $installed='@/TEXDIR/@/automultiplechoice.sty';
    if(! -f $installed) {
	print STDERR "ERROR: I cannot find the installed style file $installed\n";
	exit(2);
    }
    for my $dir (qw:tex tex/latex:) {
	if(! -d "$loc/$dir") {
	    print "Creating directory $loc/$dir...\n";
	    mkdir("$loc/$dir");
	}
    }
    $link="$loc/tex/latex/automultiplechoice.sty";
    if(-l $link) {
        print "WARNING: removing a previously installed non-working symlink...\n";
        unlink($link)
            or die "ERROR: Failed to remove previous symlink $link\n";
    }
    print "Creating link to AMC style file...\n";
    symlink($installed,$link)
        or die "ERROR: Failed to create symlink $link\n";

    rehash();

    if(@styles) {
	if(! -d $conf_dir) {
	    mkdir($conf_dir);
	}
	print "Saving information about this 'latex-link' run in $link_file...\n";
	open(LF,">$link_file")
	    or die "Unable to write to $link_file: $!";
	print LF "$link\n";
	close(LF);

	print "Done, 'automultiplechoice.sty' is now reachable from latex.\n";
    } else {
	die "ERROR: even though the symlink has been properly created, 'automultiplechoice.sty'\n\
         cannot be found using 'kpsewhich -all automultiplechoice.sty'.\n";
    }
}