summaryrefslogtreecommitdiff
path: root/Master/texmf/scripts/texlive/uninstall-tl.pl
blob: 9eab203e38ca05a11d2e08eef0ef54e2b9d0bb54 (plain)
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
#!/usr/bin/env perl

# $Id: uninstall-tl.pl 6381 2008-01-23 17:50:54Z preining $
# uninstall-tl.pl
#
# Copyright 2008 Norbert Preining, Siep Kroonenberg
# This file is licensed under the GNU General Public License version 2
# or any later version.
#


my $Master;

BEGIN {
  $^W = 1;
  $Master = `kpsewhich -var-value=SELFAUTOPARENT`;
  chomp($Master);
  unshift (@INC, "$Master/tlpkg");
}


use TeXLive::TLWinGoo;
use TeXLive::TLPDB;
use Cwd qw/abs_path/;
use strict;

&main ();

sub win32
{
  return ($^O=~/^MSWin(32|64)$/i ? 1 : 0);
}

sub main
{
  # get the db.
  my $tlpdb = TeXLive::TLPDB->new ("root" => "$Master");
  my $opt_symlinks = 0;
  my ($sys_bin,$sys_info,$sys_man);
  my ($texdir,$texmflocal,$texmfhome,$texmfsysvar);
  if ($tlpdb) {
    $sys_bin = $tlpdb->option_sys_bin;
    $sys_man = $tlpdb->option_sys_man;
    $sys_info= $tlpdb->option_sys_info;
    $opt_symlinks = $tlpdb->option_symlinks;
    $texdir = $Master;
    $texmfhome = `kpsewhich -var-value=TEXMFHOME`; chomp($texmfhome);
    $texmfsysvar = `kpsewhich -var-value=TEXMFSYSVAR`; chomp($texmfsysvar);
    $texmflocal = `kpsewhich -var-value=TEXMFLOCAL`; chomp($texmflocal);
  } else {
    warn("Cannot find tlpdb in $Master!\n");
  }
  # we have to
  # - (win32) remove the entry of bin/arch from the PATH environment
  # - (unix) remove the links if opt_symlinks
  # - (win32) remove the .texlua association
  # - (win32) remove the entry of PATHEXT
  # - (unix) ... the links
  if (win32()) {
    remove_texbindirs_from_path();
    unregister_script_type(".texlua");
    broadcast_env();
    update_assocs();
  } else {
    # remove the links missing
    if ($opt_symlinks) {
      my @files;
      if ((-d "$sys_bin") && (-w "$sys_bin")) {
        my $plat_bindir;
        if (-l "$sys_bin/pdftex") {
          my $fullpath = readlink("$sys_bin/pdftex");
          if ($fullpath =~ m;^$Master/bin/(.*)/[^/]*$;) {
            $plat_bindir = $1;
          }
        } else {
          warn "$sys_bin/pdftex not present or not a link, not removing any link of binaries!\n";
        }
        if ($plat_bindir) {
          @files = `ls "$Master/bin/$plat_bindir"`;
          chomp(@files);
          foreach my $f (@files) {
            next if (! -r "$sys_bin/$f");
            if ((-l "$sys_bin/$f") &&
                (readlink("$sys_bin/$f") =~ m;^$Master/bin/$plat_bindir/;)) {
              unlink("$sys_bin/$f");
            } else {
              warn ("not removing $sys_bin/$f, not a link or wrong destination!\n");
            }
          }
        }
        `rmdir "$sys_bin" 2>/dev/null`;
      } else {
        warn "destination of bin symlink $sys_bin not writable, no removal of links of bin files done!\n";
      }
      # info files
      if (-w  $sys_info) {
        @files = `ls "$Master/texmf/doc/info"`;
        chomp(@files);
        foreach my $f (@files) {
          next if (! -r "$sys_info/$f");
          if ((-l "$sys_info/$f") &&
              (readlink("$sys_info/$f") =~ m;^$Master/texmf/doc/info/;)) {
            unlink("$sys_info/$f");
          } else {
            warn ("not removing $sys_info/$f, not a link or wrong destination!\n");
          }
        }
        `rmdir "$sys_info" 2>/dev/null`;
      } else {
        warn "destination of info symlink $sys_info not writable, no removal of links of info files done!\n";
      }
      # man files
      if (-w  $sys_man) {
        my $foo = `(cd "$Master/texmf/doc/man" && echo *)`;
        my @mans = split ' ', $foo;
        chomp(@mans);
        foreach my $m (@mans) {
          my $mandir = "$Master/texmf/doc/man/$m";
          next unless -d $mandir;
          next unless -d "$sys_man/$m";
          @files = `ls "$mandir"`;
          chomp(@files);
          foreach my $f (@files) {
            next if (! -r "$sys_man/$m/$f");
            if ((-l "$sys_man/$m/$f") &&
                (readlink("$sys_man/$m/$f") =~ m;^$Master/texmf/doc/man/$m/;)) {
              unlink("$sys_man/$m/$f");
            } else {
              warn ("not removing $sys_man/$m/$f, not a link or wrong destination!\n");
            }
          }
          # ignore errors, it might be not empty
          `rmdir "$sys_man/$m" 2>/dev/null`;
        }
        `rmdir "$sys_man" 2>/dev/null`;
      } else {
        warn "destination of man symlink $sys_man not writable, no removal of links of man files done!\n";
      }
    }
  }
}


__END__


### Local Variables:
### perl-indent-level: 2
### tab-width: 2
### indent-tabs-mode: nil
### End:
# vim:set tabstop=2 expandtab: #