# This file, debianize-updmap, is meant to be sourced by updmap and
# enhance the --enable and --disable options with the functionality
# needed to deal with Debian's generated updmap.cfg.
# $Id$

# This is the planned scheme how it works
# 
# A for disableMap:
#   1. create a list of files to act on (either in sysconfdir only, or in user dirs too)
#   2. grep for not-commented lines in these files that contain $map
#   CHANGE 3. for each of these files, run configReplace appropriately
#   3. if in syswide-mode, for each of these files, run configReplace appropriately;
#      if not in syswide mode, for each of these files,
#             test whether it is in the syswide dir
#   3.a if not, run configReplace appropriately
#   3b. if yes, create copy in user dir and run configReplace appropriately
#
# B for enableMap:
#   1. create a list of files to act on (either in sysconfdir only, or in user dirs too)
#   2. grep for commented lines in these files that contain $map
#   2.1. if any are found, check whether it is only one
#   2.1.1 if there is only one, run configReplace on that file appropriately
#   2.1.2 if there is more than one, the precedence is 
#         20tetex-extra.cfg > XX*local*.cfg > 10tetex-base.cfg > any
#	  (extra has maximum priority because 
#   2.1.2.1 if it's clear on which file to act, run configReplace on that file
#           appropriately
#   2.1.2.2 if unclear (i.e. only "any"), exit with an error
#   2.2 if none are found, act on 10local.cfg (could be made configurable)
#       - if 10local.cfg does not exist, create it (with an explanatory comment)
#       - run configReplace on it appropriately
#
# C for syncwithtrees (and even more?)
# ?
#
# After that, update-updmap must be run (in the appropriate mode!), and then updmap
# must reexecute itself.  Since disableMap is called by enableMap, we need a wrapper
# for it.  An alternative would be to do this just before the creation of the output
# files, but I think it's easier this way, and currently updmap only enables one Map
# file per run.

###############################################################################
# DebianSyswideMode ()
#   determine whether we are running in syswide mode (or user-specific)
###############################################################################
DebianSyswideMode(){
  test "$(id -u)" -eq 0 && return 0 || return 1
}

###############################################################################
# FindDebianUserdir ()
#   find the directory containing user-specific updmap snippets
###############################################################################
FindDebianUserdir(){
  defaultuserdir=$HOME/.texmf-config/updmap.d

  # this should read a config item in updmap.cfg, but for now we just
  # check whether the default exists.
  if [ -d $defaultuserdir ]; then
    echo $defaultuserdir
  fi
}


###############################################################################
# createDebianConffilelist
#   create a list of configuration files containing updmap snippets;
#   we are going to act on these
###############################################################################
createDebianConffilelist(){
  local DebianConffilelist=""
  syswideFiles=`find /etc/texmf/updmap.d -maxdepth 1 -type f -name '*.cfg'`
  if ! DebianSyswideMode; then
    debianuserdir=`FindDebianUserdir`
    if [ -n "$debianuserdir" ]; then
      userFiles=`find "$debianuserdir" -maxdepth 1 -type f -name '*.cfg'`
    fi
  fi
  if [ -n "$userFiles" ]; then
    userBasenames=`for file in $userFiles; do basename $file; done`
    for file in $syswideFiles; do
      local copyInUserdir=false
      
    done
  else
  fi
}

###############################################################################
# runUpdate
#   run update-updmap
###############################################################################
runUpdate(){
  updOptions=""
  if [ -n "$cfgparam" ]; then
    # --conffile option was given on the command line
      updOptions="--output-file $orig"
  fi
  # here we could check for an alternative user dir, or output file
  # but as long as FindDebianUserdir only reports the default location, 
  # it doesn't make sense

  verboseMsg "Running update-updmap to merge the changed files"
  update-updmap --quiet $updOptions
}
###############################################################################
# cleanupNoExit()
#   clean up the temp area, but don't exit
###############################################################################
cleanupNoExit()
{
  rc=$?
  $needsCleanup && test -n "$tmpdir" && test -d "$tmpdir" \
    && { cd / && rm -rf "$tmpdir"; }
}

###############################################################################
# reExecSelf
#   execute ourselves again - the confile has changed after an enable or disable
#   command has been processed
###############################################################################
reExecSelf(){
  # remove the temporary directory
  cleanupNoExit
  
  # if we need not remake map files, we are done here
  if [ "$mkmapEnabled" = "false" ]; then exit 0; fi

  # pass ourselves the right options.  
  repeatOptions=""
  for option in dvipsoutputdir pdftexoutputdir dvipdfmoutputdir; do
    # strange that I can't do the same with eval test -n
    if eval [ ! -z  "\$$option" ]; then
      repeatOptions="$repeatOptions `eval echo --$option \\$$option`"
    fi
  done
  if [ -n "$cfgparam" ]; then
    # --conffile option was given on the command line
    repeatOptions="$repeatOptions --cnffile=$orig"
  fi
  if [ "texhashEnabled" = "false" ]; then
    repeatOptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to generate output files"
  echo $progname $repeatOptions
  exec##########################ptions="$repeatOptions --nohash"
  fi
  if [ "$verbose" = "false" ]; then
    repeatOptions="$repeatOptions --quiet"
  fi
  
  verboseMsg "Now running $progname again to gener