From 089903493751e642472bd3827eabf12d464d4394 Mon Sep 17 00:00:00 2001
From: alopez <alopez@iri.upc.edu>
Date: Tue, 3 Nov 2020 12:01:29 +0100
Subject: [PATCH] Added add_lib_to_ld_config.sh and
 remove_lib_from_ld_config.sh user scripts. Added maintainer scripts to
 automatically call that scripts

---
 CMakeLists.txt                       |  2 +-
 scripts/add_lib_to_ld_config.sh      | 38 +++++++++++++++++++++++++++
 scripts/debian/postinst              | 32 +++++++++++++++++++++++
 scripts/debian/prerm                 | 22 ++++++++++++++++
 scripts/remove_lib_from_ld_config.sh | 39 ++++++++++++++++++++++++++++
 src/CMakeLists.txt                   |  6 +++++
 6 files changed, 138 insertions(+), 1 deletion(-)
 create mode 100755 scripts/add_lib_to_ld_config.sh
 create mode 100755 scripts/debian/postinst
 create mode 100644 scripts/debian/prerm
 create mode 100755 scripts/remove_lib_from_ld_config.sh

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 84baf9d..225761c 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -102,7 +102,7 @@ IF (UNIX)
   SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "labrobotica - labrobotica@iri.upc.edu")
 
 # Uncomment to add the necessary mantainer scripts
-#   SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_SOURCE_DIR}/package_scripts/preinst;${CMAKE_SOURCE_DIR}/package_scripts/postinst;${CMAKE_SOURCE_DIR}/package_scripts/prerm;${CMAKE_SOURCE_DIR}/package_scripts/postrm")
+  SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_SOURCE_DIR}/scripts/debian/postinst;${CMAKE_SOURCE_DIR}/scripts/debian/prerm")
 
 # Uncomment to add dependencies comma separated
 # SET(CPACK_DEBIAN_PACKAGE_DEPENDS "iri-<package_name>-dev (>= 1.0~${DISTRIB})")
diff --git a/scripts/add_lib_to_ld_config.sh b/scripts/add_lib_to_ld_config.sh
new file mode 100755
index 0000000..280b394
--- /dev/null
+++ b/scripts/add_lib_to_ld_config.sh
@@ -0,0 +1,38 @@
+#!/bin/bash
+
+set -e
+
+FILE="/etc/ld.so.conf.d/iri.conf"
+LIB=
+LOCAL="local/"
+
+usage() {
+  echo "Usage: $0 -l <library> [-p]"
+  echo " -l  specify library name"
+  echo " -p  specify if installed from package. Optional."
+  exit 1
+  }
+
+while getopts ":hl:p" arg; do
+  case $arg in
+    l) LIB=$OPTARG;;
+    p) LOCAL="";;
+    h) usage;;
+    *) usage;;
+  esac
+done
+
+if [ -z "$LIB" ]
+then
+  echo "Error: no library specified"
+  usage
+fi
+
+sudo touch $FILE
+LINE="/usr/${LOCAL}lib/iri/${LIB}"
+echo "Adding ${LINE} to ${FILE}..."
+grep -qF -- "$LINE" "$FILE" || echo "$LINE" | sudo tee -a "$FILE" > /dev/null
+
+sudo ldconfig
+
+echo "Done."
diff --git a/scripts/debian/postinst b/scripts/debian/postinst
new file mode 100755
index 0000000..bc45b96
--- /dev/null
+++ b/scripts/debian/postinst
@@ -0,0 +1,32 @@
+#!/bin/bash
+
+set -e
+
+case "$1" in
+  configure)
+
+    echo "  Adding iri-scripts-path.sh to /etc/profile.d..."
+    FILE="/etc/profile.d/iri-scripts-path.sh"
+    sudo echo "# Expand PATH to include the directory where IRI scripts go." > $FILE
+    sudo echo "iri_script_path=\"/usr/local/share/iri:/usr/share/iri\"" >> $FILE
+    sudo echo "if [ -n \"\${PATH##*\${iri_script_path}}\" -a -n \"\${PATH##*\${iri_script_path}:*}\" ]; then" >> $FILE
+    sudo echo "    export PATH=\$PATH:\${iri_script_path}" >> $FILE
+    sudo echo "fi" >> $FILE
+    echo "  Done. It's necessary a log out for changes to take effect."
+
+    echo "   Trying to add library to ldconfig..."
+    /usr/share/iri/add_lib_to_ld_config.sh -l iriutils -p && echo "   Done." || echo "   /usr/share/iri/add_lib_to_ld_config.sh doesn't exists. Download it from iriutils and execute it."
+    
+  ;;
+  abort-upgrade)
+
+  ;;
+  abort-remove)
+
+  ;;
+  abort-deconfigure)
+
+  ;;
+esac   
+
+exit 0
diff --git a/scripts/debian/prerm b/scripts/debian/prerm
new file mode 100644
index 0000000..e1d5ab7
--- /dev/null
+++ b/scripts/debian/prerm
@@ -0,0 +1,22 @@
+#!/bin/bash
+
+set -e
+
+case "$1" in
+  remove)
+    echo "   Trying to remove library from ldconfig..."
+    /usr/share/iri/remove_lib_from_ld_config.sh -l iriutils -p && echo "   Done." || echo "   /usr/share/iri/remove_lib_from_ld_config.sh doesn't exists. Download it from iriutils and execute it."
+
+  ;;
+  upgrade)
+
+  ;;
+  deconfigure)
+
+  ;;
+  failed-upgrade)
+
+  ;;
+esac    
+
+exit 0
diff --git a/scripts/remove_lib_from_ld_config.sh b/scripts/remove_lib_from_ld_config.sh
new file mode 100755
index 0000000..23a30cd
--- /dev/null
+++ b/scripts/remove_lib_from_ld_config.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+set -e
+
+FILE="/etc/ld.so.conf.d/iri.conf"
+LIB=
+LOCAL="local\/"
+
+usage() {
+  echo "Usage: $0 -l <library> [-p]"
+  echo " -l  specify library name"
+  echo " -p  specify if installed from package. Optional."
+  exit 1
+  }
+
+while getopts ":hl:p" arg; do
+  case $arg in
+    l) LIB=$OPTARG;;
+    p) LOCAL="";;
+    h) usage;;
+    *) usage;;
+  esac
+done
+
+if [ -z "$LIB" ]
+then
+  echo "Error: no library specified"
+  usage
+fi
+
+sudo touch $FILE
+echo "Removing /usr/${LOCAL}lib/iri/${LIB} from ${FILE}..."
+LINE="\/usr\/${LOCAL}lib\/iri\/${LIB}"
+sudo sed -i "/$LINE/d" $FILE
+
+
+sudo ldconfig
+
+echo "Done."
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 0143935..9dbcf76 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -28,4 +28,10 @@ INSTALL(FILES ${headers} DESTINATION include/iri/${PROJECT_NAME})
 
 INSTALL(FILES ../Findiriutils.cmake DESTINATION ${CMAKE_ROOT}/Modules/)
 
+INSTALL(FILES  ../scripts/add_lib_to_ld_config.sh 
+               ../scripts/remove_lib_from_ld_config.sh 
+               DESTINATION share/iri
+               PERMISSIONS OWNER_EXECUTE OWNER_READ GROUP_EXECUTE GROUP_READ WORLD_READ WORLD_EXECUTE)
+
+
 ADD_SUBDIRECTORY(examples)
-- 
GitLab