#!/bin/sh -e

# Hack to get the dpkg process' PID despite using debconf
if [ -z "$PARENT_PID" ]; then
  export PARENT_PID=$PPID
fi

. /usr/share/debconf/confmodule

supported_kernel() {
  local version
  if [ "$1" ]; then
    version="$1"
  else
    version="$(uname -r)"
  fi

  case "$version" in
    2.[012345].*|2.6.[0-9]|2.6.[0-9][!0-9]*) return 1 ;;
    2.6.1[0-9]|2.6.1[0-9][!0-9]*) return 1 ;;
    2.6.2[0-5]|2.6.2[0-5][!0-9]*) return 1 ;;
  esac
  return 0
}

check_installed_kernel() {
  for ver in /lib/modules/*; do
    ver=${ver##*/}
    [ "$ver" = '*' ] && return 1
    supported_kernel "$ver" && return 0
  done
  return 1
}

check_installing_kernel() {
  for pkg in $(ps hp $PARENT_PID -o args); do
    ver=$(echo $pkg | sed -nr "s/^.*linux-image-(2\.6\.[0-9]+)-([0-9]+|trunk)-.*_.*_.*\.deb$/\1/p")
    [ "$ver" ] && supported_kernel "$ver" && return 0
  done
  return 1
}

check_kernel_version() {
  # skip the check if udev is not already active
  [ -d /dev/.udev/ ] || return 0

  supported_kernel && return

  if [ -e /etc/udev/kernel-upgrade ]; then
    echo "This version of udev requires a kernel >= 2.6.26, but the upgrade was forced."
    # restart udevd which was killed by the old prerm
    udevd -d || true
    return 0
  fi
  
  db_settitle udev/title/upgrade

  if check_installed_kernel || check_installing_kernel; then
    db_fset udev/reboot_needed seen false
    db_input high udev/reboot_needed || true
    db_go
    db_stop
    echo "A reboot is needed, but proceeding with the upgrade."
    touch /etc/udev/kernel-upgrade
    # restart udevd which was killed by the old prerm
    udevd -d || true
    return 0
  fi

  db_fset udev/new_kernel_needed seen false
  db_reset udev/new_kernel_needed
  db_input critical udev/new_kernel_needed || true
  db_go
  db_get udev/new_kernel_needed
  if [ "$RET" = true ]; then
    db_stop
    echo "This version of udev requires a kernel >= 2.6.26, but the upgrade was forced."
    touch /etc/udev/kernel-upgrade
    # restart udevd which was killed by the old prerm
    udevd -d || true
    return 0
  fi
  db_stop
  exit 1
}

check_kernel_features() {
  # skip the check if udev is not already active
  [ -d /dev/.udev/ ] || return 0

  # skip the check if check_kernel_version() decided that a new kernel is
  # being installed
  [ -e /etc/udev/kernel-upgrade ] && return 0

  local abort_install=0

  if [ -e /proc/kallsyms ]; then

  local needed_symbols='sys_inotify_init sys_signalfd'
  local missing_symbol=0
  for symbol in $needed_symbols; do
    if ! egrep -q "^[a-fA-F0-9]+ T \.?${symbol}$" /proc/kallsyms; then
      missing_symbol=1
      abort_install=1
      break
    fi
  done

  fi

  local sysfs_deprecated=0
  if [ -d /sys/class/mem/null -a ! -L /sys/class/mem/null ]; then
    sysfs_deprecated=1
  fi

  if [ "$missing_symbol" -eq 1 ]; then
    cat <<END
Since release 146, udev requires support for the following features in
the running kernel:

- inotify(2)       (CONFIG_INOTIFY_USER)
- signalfd(2)      (CONFIG_SIGNALFD)
END
  fi

  if [ "$sysfs_deprecated" -eq 1 ]; then
    db_input critical udev/sysfs_deprecated_incompatibility || true
  fi

  if [ "$abort_install" -eq 0 ]; then
    return 0
  fi

  cat <<END

Please upgrade your kernel before or while upgrading udev.

AT YOUR OWN RISK, you can force the installation of this version of udev
WHICH DOES NOT WORK WITH YOUR RUNNING KERNEL AND WILL BREAK YOUR SYSTEM
AT THE NEXT REBOOT by creating the /etc/udev/kernel-upgrade file.
There is always a safer way to upgrade, do not try this unless you
understand what you are doing!


END

  db_stop
  exit 1
}

check_devfs_names() {
  [ -e /etc/udev/rules.d/devfs.rules ] || return 0

  cat <<END
Since release 0.124-1 udev does not support anymore devfs-like names.
Please convert to standard names before upgrading:

rm -f /etc/udev/rules.d/devfs.rules /etc/udev/rules.d/compat-full.rules /etc/udev/rules.d/compat.rules
ln -s ../udev.rules /etc/udev/rules.d/

END
  exit 1
}

rm_conffile() {
  mv_conffile "$1" "$1.dpkg-bak"
}

mv_conffile() {
  local package='udev'
  local name="$1"
  local newname="$2"

  [ -e "$name" ] || return 0

  local md5="$(md5sum $name | sed -e 's/ .*//')"
  oldmd5="$(dpkg-query -W -f='${Conffiles}' $package | \
    sed -n -e "\' $name ' { s/ obsolete$//; s/.* //; p }")"

  if [ "$md5" = "$oldmd5" ]; then
    rm -f "$name"
  else
    mv "$name" "$newname"
  fi
}

replace_sysfs_attr() {
  local file=$1
  [ -f $file ] || return 0
  egrep '^[^#].+\<SYSFS\{' $file || return 0

  sed -r -e '/^#/!s/\<SYSFS(\{[^}]+}==)/ATTR\1/g' < $file > $file.tmp
  mv $file.tmp $file
}

check_version() {
  # $2 is non-empty when installing from the "config-files" state
  [ "$2" ] || return 0

  if dpkg --compare-versions $2 lt 157-1; then

  if dpkg --compare-versions $2 lt 150-1; then

  # these must be checked first to allow aborting before changing anything
  check_kernel_version
  check_kernel_features

  if dpkg --compare-versions $2 lt 0.140-1; then
  if dpkg --compare-versions $2 lt 0.124-1; then

  # this must be checked first to allow aborting before changing anything
  check_devfs_names

  if dpkg --compare-versions $2 lt 0.098-1; then
    echo 'Upgrading udev from packages older than 0.098-1 is not supported.'
    echo 'Please purge the package and then reinstall it.'
    exit 1
  fi

  # lt 0.124-1
  rm_conffile /etc/udev/cd-aliases-generator.rules
  rm_conffile /etc/udev/compat-full.rules
  rm_conffile /etc/udev/compat.rules
  rm_conffile /etc/udev/devfs.rules
  rm_conffile /etc/udev/hotplug.rules
  rm_conffile /etc/udev/permissions.rules
  rm_conffile /etc/udev/persistent-input.rules
  rm_conffile /etc/udev/persistent-net-generator.rules
  rm_conffile /etc/udev/persistent.rules
  rm_conffile /etc/udev/run.rules
  rm_conffile /etc/udev/udev.rules
  # removed in 0.113-1
  rm_conffile /etc/udev/hotplugd.rules

  (
  cd /etc/udev/rules.d/

  # delete dangling symlinks
  find -L . -type l -print0 | xargs --no-run-if-empty --null rm

  # rename the generated files
  if [ -e z25_persistent-cd.rules  -a ! -e 70-persistent-cd.rules  ]; then
    mv z25_persistent-cd.rules  70-persistent-cd.rules
  fi
  if [ -e z25_persistent-net.rules -a ! -e 70-persistent-net.rules ]; then
    mv z25_persistent-net.rules 70-persistent-net.rules
  fi
  )

  fi # 0.124-1

  # lt 0.140-1
  rm_conffile /etc/modprobe.d/display_class
  rm_conffile /etc/modprobe.d/pnp-hotplug
  mv_conffile /etc/modprobe.d/blacklist /etc/modprobe.d/blacklist.conf

  for file in 50-udev 60-persistent-input 60-persistent-storage \
      60-persistent-storage-tape 60-persistent-v4l 91-permissions 80-drivers \
      75-cd-aliases-generator 75-persistent-net-generator 95-late; do
    rm_conffile /etc/udev/rules.d/$file.rules
  done

  # SYSFS is equivalent to ATTRS, and multiple ATTRS attributes do not match
  # when the keys are from different kobjects like in this case.
  replace_sysfs_attr /etc/udev/rules.d/70-persistent-net.rules

  fi # 0.140-1

  # lt 146-1
  rm_conffile /etc/scsi_id.config

  fi # 146-1

  # suppress errors when the new rules files contain options not supported by
  # the old daemon
  udevadm control --log-priority=0 || true

  fi # 157-1
}

case "$1" in
    install)
    check_version "$@"
    ;;

    upgrade|abort-upgrade)
    check_version "$@"
    ;;

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



