#!/bin/sh
set -e
# register openni modules
#
# This script supports two actions, POST-INSTALL and DEINSTALL, that will
# register or unregister openni modules.
#
# Lines starting with "# NIREG: " are data read by this script that
# name the openni module and directory to be registered by niReg
#
#	# NIREG: lib/bar.so etc/foo
#	# NIREG: lib/baz.so etc/foo
#
# For each NIREG entry, if the path is relative, that it is taken to be
# relative to ${PKG_PREFIX}.
#

ECHO="echo"
SED="/bin/sed"
SORT="/usr/bin/sort"
TEST="test"
NIREG="/opt/openrobots/bin/niReg"
NILICENSE="/opt/openrobots/bin/niLicense"

echo=${PKG_VERBOSE:+${ECHO}}
: ${echo:=:}

case $2 in
  POST-INSTALL)
    ${SED} -n "/^\# NIREG: /{s/^\# NIREG: //;p;}" $0 | ${SORT} -u |
    while read file dir; do
      case $file in
        "") continue ;;
        [!/]*) file="${PKG_PREFIX}/$file" ;;
      esac
      ${TEST} -f "$file" || continue

      case $dir in
        [!/]*) dir="${PKG_PREFIX}/$dir" ;;
      esac

      ${echo} "$1: registering $file in $dir"
      $NIREG -r $file $dir
    done

    ${SED} -n "/^\# NILIC: /{s/^\# NILIC: //;p;}" $0 | ${SORT} -u |
    while read vendor key; do
      ${echo} "$1: registering license for $vendor"
      $NILICENSE -r $vendor $key
    done
    ;;

  DEINSTALL)
    ${SED} -n "/^\# NIREG: /{s/^\# NIREG: //;p;}" $0 | ${SORT} -u |
    while read file dir; do
      case $file in
        "") continue ;;
        [!/]*) file="${PKG_PREFIX}/$file" ;;
      esac
      ${TEST} -f "$file" || continue

      case $dir in
        [!/]*) dir="${PKG_PREFIX}/$dir" ;;
      esac

      ${echo} "$1: unregistering $file $dir"
      $NIREG -u $file $dir
    done

    ${SED} -n "/^\# NILIC: /{s/^\# NILIC: //;p;}" $0 | ${SORT} -u |
    while read vendor key; do
      ${echo} "$1: unregistering license for $vendor"
      $NILICENSE -u $vendor $key
    done
  ;;
esac
# NIREG: lib/libnimMockNodes.so /opt/openrobots/share/openni
# NIREG: lib/libnimCodecs.so /opt/openrobots/share/openni
# NIREG: lib/libnimRecorder.so /opt/openrobots/share/openni
exit 0
