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

# This is the planned scheme how it works
# 
# A for disablefmt:
#   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 $fmt
#   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-fmtutil must be run (in the appropriate mode!), and then fmtutil
# must reexecute itself.  Since disableMap is called by enableMap, we need a wrapper
# for it.  

###############################################################################
# verboseMsg(msg)
#   print `msg' to stderr is $verbose is true
###############################################################################
verboseMsg() {
  $verboseFlag && verbose echo ${1+"$@"} >&2
}

###############################################################################
#  enablefmt(format)
#    enables format in configuration file
###############################################################################
enablefmt()
{
  enableFmtInner $1
  runUpdate
}

###############################################################################
# disablefmt(format)
#   disables format in configuration file
###############################################################################
disablefmt()
{
  disableFmtInner $1
  runUpdate
}

###############################################################################
# debianEditWarnAndShow()
#   warn that this won't work
###############################################################################
debianEditWarnAndShow(){
  echo "Warning!"
  echo ""
  echo "In Debian, fmtutil.cnf, the format configuration file, is a generated file."
  echo "The actual configuration files are in /etc/texmf/fmt.d."
  echo "Please read the update-fmtutil(1) manpage and edit those files."
  echo ""
  echo "Instead of editing, you will now be shown fmtutil.cnf in a pager."
  echo ""
  echo "Press enter to continue."
  read dummy
  
  sensible-pager $cnf_file

}

###############################################################################
# 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 (intil
# must reexecute itself.  Since disableMap is called by enableMap, we need a wrapper
# for it.  

###############################################################################
# verboseMsg(msg)
#   print `msg' to stderr is $verbose is true
###############################################################################
verboseMsg() {
  $verboseFlag && verbose echo ${1+"$@"} >&2
}

###############################################################################
#  enablefmt(format)
#    enables format in configuration file
###############################################################################
enablefmt()
{
  enableFmtInner $1
  runUpdate
}

###############################################################################
# disablefmt(format)
#   disables format in configuration file
###############################################################################
disablefmt()
{
  disableFmtInner $1
  runUpdate
}

###############################################################################
# debianEditWarnAndShow()
#   warn that this won't work
###############################################################################
debianEditWarnAndShow(){
  echo "Warning!"
  echo ""
  echo "In Debian, fmtutil.cnf, the format configuration file, is a generated file."
  echo "The actual configuration files are in /etc/texmf/fmt.d."
  echo "Please read the update-fmtutil(1) manpage and edit those files."
  echo ""
  echo "Instead of editing, you will now be shown fmtutil.cnf in a pager."
  echo ""
  echo "Press enter to continue."
  read dummy
  
  sensible-pager $cnf_file

}

###############################################################################
# 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 (intil
# must reexecute itself.  Since disableMap is called by enableMap, we need a wrapper
# for it.  

###############################################################################
# verboseMsg(msg)
#   print `msg' to stderr is $verbose is true
###############################################################################
verboseMsg() {
  $verboseFlag && verbose echo ${1+"$@"} >&2
}

###############################################################################
#  enablefmt(format)
#    enables format in configuration file
###############################################################################
enablefmt()
{
  enableFmtInner $1
  runUpdate
}

###############################################################################
# disablefmt(format)
#   disables format in configuration file
###############################################################################
disablefmt()
{
  disableFmtInner $1
  runUpdate
}

###############################################################################
# debianEditWarnAndShow()
#   warn that this won't work
###############################################################################
debianEditWarnAndShow(){
  echo "Warning!"
  echo ""
  echo "In Debian, fmtutil.cnf, the format configuration file, is a generated file."
  echo "The actual configuration files are in /etc/texmf/fmt.d."
  echo "Please read the update-fmtutil(1) manpage and edit those files."
  echo ""
  echo "Instead of editing, you will now be shown fmtutil.cnf in a pager."
  echo ""
  echo "Press enter to continue."
  read dummy
  
  sensible-pager $cnf_file

}

###############################################################################
# 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 (intil
# must reexecute itself.  Since disableMap is called by enableMap, we need a wrapper
# for it.  

###############################################################################
# verboseMsg(msg)
#   print `msg' to stderr is $verbose is true
###############################################################################
verboseMsg() {
  $verboseFlag && verbose echo ${1+"$@"} >&2
}

###############################################################################
#  enablefmt(format)
#    enables format in configuration file
###############################################################################
enablefmt()
{
  enableFmtInner $1
  runUpdate
}

###############################################################################
# disablefmt(format)
#   disables format in configuration file
###############################################################################
disablefmt()
{
  disableFmtInner $1
  runUpdate
}

###############################################################################
# debianEditWarnAndShow()
#   warn that this won't work
###############################################################################
debianEditWarnAndShow(){
  echo "Warning!"
  echo ""
  echo "In Debian, fmtutil.cnf, the format configuration file, is a generated file."
  echo "The actual configuration files are in /etc/texmf/fmt.d."
  echo "Please read the update-fmtutil(1) manpage and edit those files."
  echo ""
  echo "Instead of editing, you will now be shown fmtutil.cnf in a pager."
  echo ""
  echo "Press enter to continue."
  read dummy
  
  sensible-pager $cnf_file

}

###############################################################################
# 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 (intil
# must reexecute itself.  Since disableMap is called by enableMap, we need a wrapper
# for it.  

###############################################################################
# verboseMsg(msg)
#   print `msg' to stderr is $verbose is true
###############################################################################
verboseMsg() {
  $verboseFlag && verbose echo ${1+"$@"} >&2
}

###############################################################################
#  enablefmt(format)
#    enables format in configuration file
###############################################################################
enablefmt()
{
  enableFmtInner $1
  runUpdate
}

###############################################################################
# disablefmt(format)
#   disables format in configuration file
###############################################################################
disablefmt()
{
  disableFmtInner $1
  runUpdate
}

###############################################################################
# debianEditWarnAndShow()
#   warn that this won't work
###############################################################################
debianEditWarnAndShow(){
  echo "Warning!"
  echo ""
  echo "In Debian, fmtutil.cnf, the format configuration file, is a generated file."
  echo "The actual configuration files are in /etc/texmf/fmt.d."
  echo "Please read the update-fmtutil(1) manpage and edit those files."
  echo ""
  echo "Instead of editing, you will now be shown fmtutil.cnf in a pager."
  echo ""
  echo "Press enter to continue."
  read dummy
  
  sensible-pager $cnf_file

}

###############################################################################
# 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 (intil
# must reexecute itself.  Since disableMap is called by enableMap, we need a wrapper
# for it.  

###############################################################################
# verboseMsg(msg)
#   print `msg' to stderr is $verbose is true
###############################################################################
verboseMsg() {
  $verboseFlag && verbose echo ${1+"$@"} >&2
}

###############################################################################
#  enablefmt(format)
#    enables format in configuration file
###############################################################################
enablefmt()
{
  enableFmtInner $1
  runUpdate
}

###############################################################################
# disablefmt(format)
#   disables format in configuration file
###############################################################################
disablefmt()
{
  disableFmtInner $1
  runUpdate
}

###############################################################################
# debianEditWarnAndShow()
#   warn that this won't work
###############################################################################
debianEditWarnAndShow(){
  echo "Warning!"
  echo ""
  echo "In Debian, fmtutil.cnf, the format configuration file, is a generated file."
  echo "The actual configuration files are in /etc/texmf/fmt.d."
  echo "Please read the update-fmtutil(1) manpage and edit those files."
  echo ""
  echo "Instead of editing, you will now be shown fmtutil.cnf in a pager."
  echo ""
  echo "Press enter to continue."
  read dummy
  
  sensible-pager $cnf_file

}

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

##########################################################