#!/bin/sh -e
# created 2010 by Frank Kster frank@debian.org
# The code may be freely copied, distributed and/or modified

# The plan: 
# 1. Analyze the current situation and compare with the libpaper setting:
#    a) Get the settings for dvips, xdvi, pdftex and dvipdfmx from 
#       their respective conffiles - use texconfig-sys for that
#    b) Get the libpaper setting
#    c) Check whether all 4 binaries have the same current setting.
# 2. Depending on the comparison, act differently
#    a) if Yes, check whether current setting and libpaper setting are the same
#        i. if Yes, we are done
#        ii. if no, call "texconfig-sys paper $libpaper-default" and check the return value
#            A. If the return value is 1, give a debconf warning that nothing has been changed
#            In any case we are done
#    b) if No, 
#       i.  print a debconf multiselect window that lists the current settings and 
#           lets the admin select which binaries should get the new, changed configuration
#       ii. next, call "texconfig-sys $binary paper $libpaper-default" for each selected binary,
#           and check the return value
#            A. If the return value is 1, give a debconf warning that nothing has been changed
# Done.

# If texlive-base is removed, just  do nothing. 
test -x /usr/share/texmf/texconfig/tcfmgr || exit 0

. /usr/share/debconf/confmodule
db_version 2.0

dvips=$(texconfig-sys dvips paperconf)
xdvi=$(texconfig-sys xdvi paperconf)
pdftex=$(texconfig-sys pdftex paperconf)
dvipdfmx=$(texconfig-sys dvipdfmx paperconf)

LibpaperPaper=$(paperconf)

FourPapersAllSame=false

if [ $dvips = $xdvi ] && \
   [ $dvips = $pdftex ] && \
   [ $dvips = $dvipdfmx ]; then
  FourPapersAllSame=true
fi

if [ $FourPapersAllSame = true ]; then
  if [ $dvips = $LibpaperPaper ]; then
    exit 0
  else
    if texconfig-sys paper $LibpaperPaper; then
      # all is well
      exit 0
    else
      # texconfig-sys didn't understand the paper name
      db_subst texlive-base/texconfig_ignorant libpaperPaper $LibpaperPaper
      db_subst texlive-base/texconfig_ignorant binary "all programs"
      db_subst texlive-base/texconfig_ignorant binary_commandline ""
      # the priority will be treated as critical anyway for all error templates.
      db_input critical texlive-base/texconfig_ignorant || true
      db_go || true
    fi
  fi
else
#    b) if No, 
#       i.  print a debconf multiselect window that lists the current settings and 
#           lets the admin select which binaries should get the new, changed configuration
#       ii. next, call "texconfig-sys $binary paper $libpaper-default" for each selected binary,
#           and check the return value
#            A. If the return value is 1, give a debconf warning that nothing has been changed
# Done.
  # the four Papers are not all the same. Ask the user.  
  db_subst texlive-base/binary_chooser libpaperPaper $LibpaperPaper
  db_fset texlive-base/binary_chooser seen false
  db_input high texlive-base/binary_chooser || true
  db_go || true

  db_get texlive-base/binary_chooser
  ListOfBinariesToUseLibpaper="$RET"

  # now get rid of the commas by assigning to the positional parameters
  OLD_IFS="$IFS"
  IFS=', '
  set $ListOfBinariesToUseLibpaper
  # IFS needs to be restored before talking to debconf
  IFS=$OLD_IFS

  for binary in "$@"; do
    if texconfig-sys $binary paper $LibpaperPaper; then
      # all is well
      :
    else
      db_subst texlive-base/texconfig_ignorant binary "$binary"
      db_subst texlive-base/texconfig_ignorant binary_commandline "$binary"
      db_subst texlive-base/texconfig_ignorant libpaperPaper "$LibpaperPaper"
      db_input high texlive-base/texconfig_ignorant || true

      db_go || true
    fi
  done

fi
