summaryrefslogtreecommitdiff
path: root/Master/tlpkg/dev/mktexupd.texlua
blob: 13bb58771ad74cece5c37f0b7df1f7c63973b208 (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
#!/usr/bin/env texlua
-- Written in lua by Norbert Preining (2008) based on the shell script by
-- Thomas Esser.
-- Copyright 2008 Norbert Preining
-- License: GPL


-- Changelog
-- 0.1
--  - first initial version

progname = 'mktexupd (texlua version)';
version = '0.1';
usage = 'Usage: ' .. progname .. ' [-h|--help] DIR FILE\
\
  Update the ls-R file with an entry for FILE in DIR.\
\
	 -h|--help\t\t Show this help\
         -V|--version\t\t Print the version of the program\
';


function die (str)
  io.stderr:write(str)
  os.exit(1)
end
  
while table.maxn(arg) > 0 and string.match(arg[1],'^%-') do
   curr_arg = table.remove(arg,1)
   if string.match (curr_arg,'-h') or string.match (curr_arg,'--help') then
      print (usage);
      os.exit(0);
   elseif string.match (curr_arg,'-V') or string.match (curr_arg,'--version') then
      print (progname .. ' version: ' .. version );
      os.exit(0);
   end
end

if not arg[2] then
   print (usage);
   return
end

-- initialize kpathsea
kpse.set_program_name("mktexupd")

dir = arg[1]
file = arg[2]
fullfile = dir .. '/' .. file

if (not lfs.isdir(dir)) then
  die (progname .. ': ' .. dir .. ' not a directory.\n')
end
if (not lfs.isfile(fullfile)) then
  die (progname .. ': ' .. fullfile .. ' not a file.\n')
end

-- we have to get the list of all directories where ls-R files are present
-- using TEXMFDBS. In the shell code `kpsewhich  -show-path=ls-R was used
-- we do it with kpse.expand_braces("$TEXMFDBS") and then split at : and
-- remove the !!

texmfdbs = kpse.expand_braces("$TEXMFDBS")
for d in string.gmatch(texmfdbs,"[^:]+") do 
  d = d:gsub("^!!","")
  pel =  string.match(dir,d.."/(.*)$") 
  if pel then
    target = d
    pathcomponent = pel
  end
end
if (not target) then
  --[[ no path found, just give up ]]
  os.exit(0)
end

db_file = target..'/ls-R'
db_file_lc = target..'/ls-r'

if (not(lfs.isfile(db_file))) then
  if lfs.isfile(db_file_lc) then
    db_file = db_file_lc
  end
end
    
if (not(lfs.isfile(db_file))) then
  print ('Calling mktexlsr for '..target)
  os.execute("mktexlsr "..target)
  os.exit()
end

-- Old ls-R files should continue to work.
ls_R_magic = '% ls-R -- filename database for kpathsea; do not change this line.'
old_ls_R_magic='% ls-R -- maintained by MakeTeXls-R; do not change this line.'


-- read first line of ls-R file
lsrfh = io.open(db_file)
if (not(lsrfh)) then
  die('that should not happen ...\n')
end
firstline = lsrfh:read()
io.close(lsrfh)

if (firstline ~= ls_R_magic) then
  if (firstline ~= old_ls_R_magic) then
    die(progname..': '..db_file..' lacks magic string '..ls_R_magic..'\n')
  end
end

-- ok we are that far, try to open the file for writing
lsrfh,erro = io.open(db_file,"a+")
if (not(lsrfh)) then
  die(progname..': cannot open '..db_file..' for writing: '..erro..'\n')
end

-- May as well always put in a new directory entry; presumably cron will
-- come along soon enough and clean things up.
lsrfh:write('./'..pathcomponent..':\n')
lsrfh:write(file..'\n')
io.close(lsrfh)

os.exit(0)