#!/bin/sh
# postinst script for ros librealsense package
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

# source debconf library
. /usr/share/debconf/confmodule

OLDLIB="/usr/local/lib/librealsense.so"
OLDHEADER="/usr/local/include/librealsense"
OLDUDEV="/etc/udev/rules.d/99-realsense-libusb.rules"

# Are we running in a Docker Environment?
docker_cnt=$(awk -F/ '$2 == "docker"' /proc/1/cgroup  | wc -l)
# docker_cnt == 0 we are in a docker environment

case "$1" in

  configure)
    # Set FORCE_UDEV_IN_DOCKER to non-empty to override
    # fix: if docker_cnt > 0 was -eq
    if [ ${docker_cnt} -gt 0 -a -z "${FORCE_UDEV_IN_DOCKER}" ]
    then
      echo "INFO: Docker environment not supported for udevadm; skipping."
    else
      # Enforce the newly installed  udev rules
      (udevadm control --reload-rules && udevadm trigger) || true
    fi

    # Warn user about /usr/local installation
    if [ -f ${OLDLIB} -o -d ${OLDHEADER} -o -f ${OLDUDEV} ]
    then
      echo "WARNING: Locally installed librealsense components should removed:"
      if [ -f ${OLDLIB} ]
      then
        echo "         sudo rm -f ${OLDLIB}"
      fi
      if [ -d ${OLDHEADER} ]
      then
        echo "         sudo rm -rf ${OLDHEADER}"
      fi
      if [ -f ${OLDUDEV} ]
      then
        echo "         sudo rm -f ${OLDUDEV}"
      fi
    fi

    # Set FORCE_DKMS_IN_DOCKER to non-empty to override
    # fix: if docker_cnt > 0 was -eq
    if [ ${docker_cnt} -gt 0 -a -z "${FORCE_DKMS_IN_DOCKER}" ]
    then
      echo "INFO: Docker environment not supported for DKMS; skipping."
    else
      # Check for uvcvideo loadable kernel module patches in the running kernel
      UVC_MODULE=$(modinfo -F filename uvcvideo)
      PATCHED_FORMATS=$(/usr/bin/strings ${UVC_MODULE} | \
        /bin/egrep '\((Y8I|Y12I|Z16|SRGGB10P|RAW8|RW16|INVZ|INZI|INVR|INRI|INVI|RELI|L8|L16|D16)\)' | \
        /usr/bin/wc -l)

      if [ ${PATCHED_FORMATS} -eq 15 ]
      then
        echo "INFO: Intel RealSense(TM) F200, SR300, R200, LR200, and ZR300 cameras are already supported."
        echo "       DKMS will not be enabled for this system."
      else
        IS_DKMS_INSTALLED=$(dkms status uvcvideo/1.1.1-3-realsense)
        case "${IS_DKMS_INSTALLED}" in
          *Installed*|*installed*)
            echo "INFO: DKMS module already installed"
            ;;
          *Added*|*added*)
            dkms build -m uvcvideo -v "1.1.1-3-realsense" && dkms install -m uvcvideo -v "1.1.1-3-realsense" || true
            ;;
          *)
            dkms add -m uvcvideo -v "1.1.1-3-realsense" && \
            dkms build -m uvcvideo -v "1.1.1-3-realsense" && dkms install -m uvcvideo -v "1.1.1-3-realsense" || true
            ;;
        esac

        IS_DKMS_INSTALLED=$(dkms status uvcvideo/1.1.1-3-realsense)
        case "${IS_DKMS_INSTALLED}" in
          *Installed*|*installed*)
            echo "INFO: DKMS module installed successfully."
            ;;
          *Added*|*added*)
            echo "WARNING: DKMS module failed to installed; removing..."
            dkms remove -m uvcvideo -v "1.1.1-3-realsense" --all || true
            ;;
        esac

        # Check for uvcvideo loadable kernel module patches in the running kernel
        UVC_MODULE=$(modinfo -F filename uvcvideo)
        PATCHED_FORMATS=$(/usr/bin/strings ${UVC_MODULE} | \
          /bin/egrep '\((Y8I|Y12I|Z16|SRGGB10P|RAW8|RW16|INVZ|INZI|INVR|INRI|INVI|RELI|L8|L16|D16)\)' | \
          /usr/bin/wc -l)
        case "${PATCHED_FORMATS}" in
          15)
            echo "INFO: Intel RealSense(TM) F200, SR300, R200, LR200, and ZR300 cameras are supported."
            ;;
          4)
            echo "WARNING: Only Intel RealSense(TM) R200 camera is supported!"
            echo "       To resolve, please follow the installation directions at:"
            echo "           http://wiki.ros.org/librealsense#Installation"
            ;;
          0)
            echo "WARNING: No Intel RealSense(TM) cameras are supported!"
            echo "       To resolve, please follow the installation directions at:"
            echo "           http://wiki.ros.org/librealsense#Installation"
            ;;
          *)
            echo "WARNING: Unknown configuration for Intel RealSense(TM) cameras!"
            echo "       To resolve, please follow the installation directions at:"
            echo "           http://wiki.ros.org/librealsense#Installation"
            ;;
        esac
      fi
    fi
    ;;

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

  *)
    echo "postinst called with unknown argument \`$1'" >&2
    exit 1
    ;;

esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

# Automatically added by dh_installudev
if [ "$1" = configure ]; then
	if [ -e "/etc/udev/rules.d/z60_ros-kinetic-librealsense.rules" ]; then
		echo "Preserving user changes to /etc/udev/rules.d/60-ros-kinetic-librealsense.rules ..."
		if [ -e "/etc/udev/rules.d/60-ros-kinetic-librealsense.rules" ]; then
			mv -f "/etc/udev/rules.d/60-ros-kinetic-librealsense.rules" "/etc/udev/rules.d/60-ros-kinetic-librealsense.rules.dpkg-new"
		fi
		mv -f "/etc/udev/rules.d/z60_ros-kinetic-librealsense.rules" "/etc/udev/rules.d/60-ros-kinetic-librealsense.rules"
	fi
fi
# End automatically added section


db_stop

exit 0