#!/bin/sh -e

create_etc_modules() {
    if [ ! -e /etc/modules ]; then
	cat <<EOT > /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.
# Parameters can be specified after the module name.

EOT
	chmod 644 /etc/modules
    fi
}

archmodel() { 
  local arch=$(uname -m)
  case $arch in
  i[0-9]86)	arch=i386 ;;
  x86_64|amd64)	arch=x86_64 ;;
  lpia)		arch=i386 ;;
  arm*)		arch=arm ;;
  mips*)	arch=mips ;;
  # 64 bit variants of some architectures are treated like the 32 bit
  s390x)	arch=s390 ;;
  parisc64)	arch=parisc ;;
  sparc64)	arch=sparc ;;
  # these architectures have variants with wildly different hardware
  ppc64)	arch=powerpc.generic ;;
  ppc|powerpc)
    if [ -f /proc/cpuinfo ]; then
      model=$(sed -ne 's/^machine[[:space:]]*:[[:space:]]*//p' /proc/cpuinfo)
    else
      echo "/proc/cpuinfo does not exist, assuming generic powerpc system"
    fi
    case "$model" in
      Amiga*) arch="powerpc.apus" ;;
      Power*) arch="powerpc.pmac" ;; 
      *)      arch="powerpc.generic" ;;
    esac
    ;;
  m68k)
    if [ -f /proc/hardware ]; then
      model=$(sed -ne 's/^Model:[[:space:]]*//p' /proc/hardware)
    else
      echo "/proc/hardware does not exist, assuming generic m68k system"
    fi
    case "$model" in
      Atari*)		arch="m68k.atari" ;;
      Amiga*)		arch="m68k.amiga" ;;
      *)		arch="m68k.generic" ;;
    esac
    ;;
  esac

  echo $arch
}

create_arch_symlink() {
  cd /etc/modprobe.d/

  model=$(archmodel)
  oldmodel=$model

  while [ ! -f arch/$model ]; do
    oldmodel=$model
    model=${oldmodel%.*}.generic
    [ "$model" = "$oldmodel" ] && break
    echo "Configuration for $oldmodel not found, trying $model"
  done

  ARCHCONFFILE=arch/$model
  if [ -f $ARCHCONFFILE ]; then
    ln -sf $ARCHCONFFILE arch-aliases
  else
    echo "Architecture-specific config file not found"
  fi
}

remove_compat_symlinks() {
  for file in /bin/lsmod.modutils /sbin/ksyms /sbin/kallsyms; do
    [ -L $file ] && rm $file
  done
  return 0
}

undivert_gen() {
  DEXT=${3:-modutils}
  dpkg-divert --remove --rename --package module-init-tools \
    --divert $2/$1.$DEXT $2/$1 > /dev/null
}

undivert_man() {
  DSECTION=${2:-8}
  for locale in '' fr/; do
    # When a diverted file is removed from a package, old version of dpkg
    # forget to delete it. See #428650 for the gory details.
    rm -f /usr/share/man/${locale}man$DSECTION/$1.modutils.$DSECTION.gz
    dpkg-divert --remove --rename --package module-init-tools --divert \
      /usr/share/man/${locale}man$DSECTION/$1.modutils.$DSECTION.gz \
      /usr/share/man/${locale}man$DSECTION/$1.$DSECTION.gz > /dev/null
  done
}

big_modutils_cleanup() {
  remove_compat_symlinks

  undivert_man modules 5

  for cmd in depmod insmod update-modules modinfo; do
    undivert_gen $cmd /sbin
    undivert_man $cmd
  done
  for cmd in kallsyms ksyms; do
    undivert_gen $cmd /sbin
  done
  for cmd in lsmod modprobe rmmod; do
    rm -f /sbin/$cmd.modutils
    undivert_gen $cmd /sbin Lmodutils
    undivert_man $cmd
  done

  # modutils forgets to delete this file on purge
  rm -f /etc/rcS.d/S20modutils
}

upgrade_quirks() {
  [ "$2" ] || return 0

  dpkg --compare-versions $2 lt 3.3-pre11-4 || return 0
  # finally remove the diversions of modutils
  big_modutils_cleanup

  return 0
}

case "$1" in
    configure)
    create_etc_modules
    create_arch_symlink

    upgrade_quirks "$@"
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
    echo "$0 called with unknown argument '$1'" >&2
    exit 1
    ;;
esac

# Automatically added by dh_installinit
if [ -x "/etc/init.d/module-init-tools" ]; then
	update-rc.d module-init-tools start 20 S . >/dev/null || exit $?
fi
# End automatically added section


exit 0
