#!/usr/bin/env texlua -- $Id: $ -- Copyright (C) 2021 Reinhard Kotucha -- -- You may freely use, modify, and/or distribute this file. local texmf_cnf_content = [[ TEXMFDIPLO = $TEXMFROOT/../texmf-diplo % You can define additional TEXMF trees as above and add them % to TEXMFEXTRA. TEXMFEXTRA is a comma-separated list. Example: % % TEXMFEXTRA = {$TEXMFDIPLO,$TEXMFWHATEVER} TEXMFEXTRA = {$TEXMFDIPLO} TEXMF = {$TEXMFCONFIG,$TEXMFVAR,$TEXMFHOME,!!$TEXMFSYSCONFIG,!!$TEXMFSYSVAR,!!$TEXMFMAIN,$TEXMFEXTRA,!!$TEXMFLOCAL,!!$TEXMFDIST} %EOF ]] kpse.set_program_name('texlua') local texmfcnf = false -- value changed at initialization local function die (str) -- Exit with error message io.stderr:write(str..'\n') os.exit(1) end local function cmdsubst (cmd, what) -- Unix command substitution local fh = io.popen(cmd, 'r') local ret = fh:read(what) fh:close() return ret end local function user_group () -- determine user and on Unix local user = cmdsubst('id -un 2>/dev/null', '*l') or '' local group = cmdsubst('id -gn 2>/dev/null', '*l') or '' return user..':'..group end local function errmsg (str) io.stderr:write(str) end local function initialize () -- determine correct config file print('Detected System Type: '..os.type) print('Detected System Name: '..os.name) local TEXMFROOT = kpse.var_value('TEXMFROOT') or false if TEXMFROOT then print('Detected TeX Live installation at '..TEXMFROOT) else die("Can't determine root of TeX Live.") end -- set global variable texmfcnf texmfcnf = TEXMFROOT..'/texmf.cnf' if lfs.isfile(texmfcnf) then print('Found config file '..texmfcnf..'\n') else errmsg('File '..texmfcnf..' not found,\n') os.exit(1) end end local function msg_file_not_writable () -- abort if file is not writable errmsg('ERROR: The file '..texmfcnf..' is not writable.\n\n') if os.type == 'unix' then errmsg('The most likely reason is that you installed TeX Live as root.\n') if texmfcnf:match('^/usr/local/texlive') then errmsg('You can either install this program with sudo\n\n' .. ' sudo texlua install-diplo-config\n\n' .. 'or steal the whole /usr/local/texlive tree.\n\n') errmsg(' sudo chown -R '..user_group()..' /usr/local/texlive\n') errmsg('\nThe latter solution is preferred for security reasons.\n') end else --windows errmsg('The most likely reason is that you installed TeX Live ' .. 'with admin privileges.\nIf this is true you have to ' .. 'invoke this program with admin privileges too.\n') end os.exit(1) end local function add_contents_to_texmfcnf () -- add support for texmf-diplo local conf = io.open(texmfcnf, 'ab') or msg_file_not_writable() conf:write(texmf_cnf_content) conf:close() end local function texmf_cnf_is_modified () -- is file already modified local conf = assert(io.open(texmfcnf, 'r')) for line in conf:lines() do if not (line:match('^%%') or line:match('^%s*$')) then return true end end conf:close() end local function content_installed () -- do not add content twice local fh=io.open(texmfcnf, 'rb') local content = fh:read('*a') fh:close() if content:match('TEXMFDIPLO') then return true end end initialize() if content_installed() then errmsg('Changes were already applied. Nothing to do. Good bye!\n') elseif texmf_cnf_is_modified () then errmsg("\nFile '".. texmfcnf .."' was modified. I don't touch it.\n") errmsg('If you modified the file manually then comment out ' .. 'your modifications, run this program again, and merge ' .. 'your modifications later.') os.exit(1) else add_contents_to_texmfcnf () io.write('Adding texmf-diplo to '..texmfcnf..' ... ') io.write('[done]\n') end -- Local Variables: -- mode: Lua -- lua-indent-level: 2 -- indent-tabs-mode: nil -- coding: utf-8-unix -- End: -- vim:set tabstop=2 expandtab: