#!/bin/sh

updateVPNManifest()
{
  echo "${BINDIR}/manifesttool_nvm -x ${INSTPREFIX} ${INSTPREFIX}/${NVMMANIFEST}" >> ${UNINSTALLLOG}
  ${BINDIR}/manifesttool_nvm  -x ${INSTPREFIX} ${INSTPREFIX}/${NVMMANIFEST} >> ${UNINSTALLLOG}
}

removeManifestTool()
{
  if [ ! -f ${ACMANIFESTDAT} ] && [ -f ${BINDIR}/manifesttool ]; then
    echo "Removing ${BINDIR}/manifesttool" >> ${UNINSTALLLOG}
    rm -f ${BINDIR}/manifesttool
  fi
}

killNVMAgent()
{
  if [ ! "x${NVMPROC}" = "x" ] ; then
      echo Killing `ps -A -o pid,command -p ${NVMPROC} | grep ${NVMPROC} | egrep -v 'ps|grep'` >> ${UNINSTALLLOG}
      kill -KILL ${NVMPROC} >> ${UNINSTALLLOG} 2>&1
  fi
}

removeKDF()
{
# Unload the kernel driver
  if /sbin/lsmod | grep anyconnect_kdf > /dev/null; then
    echo "/sbin/modprobe -r anyconnect_kdf" >> ${UNINSTALLLOG}
    /sbin/modprobe -r anyconnect_kdf >> ${UNINSTALLLOG} 2>&1
  fi

# Remove the kernel driver
  if [ -f "${KERNELDRIVERDIR}/anyconnect_kdf.ko" ]; then
      echo "Removing ${KERNELDRIVERDIR}/anyconnect_kdf.ko" >> ${UNINSTALLLOG}
      rm -f ${KERNELDRIVERDIR}/anyconnect_kdf.ko
      echo "Updating kernel module depenedencies"
      /sbin/depmod -a
  fi
# Remove the kernel driver folder if it is empty
  rmdir --ignore-fail-on-non-empty -p ${KERNELDRIVERDIR}
}

removeInstalledFiles()
{
  echo "rm -f $1" >> /tmp/nvm-uninstall.log
  rm -f $1 >> /tmp/nvm-uninstall.log 2>&1
}

removeNVMDir()
{
  if [ -d ${NVMDIR} ]; then
    echo "rm -rf "${NVMDIR}"" >> ${UNINSTALLLOG}
    rm -rf "${NVMDIR}" >> ${UNINSTALLLOG} 2>&1
  fi
}

INSTPREFIX="/opt/cisco/anyconnect"
NVMDIR="${INSTPREFIX}/NVM"
BINDIR="${NVMDIR}/bin"
LIBDIR="${NVMDIR}/lib"
NVMAGENT="acnvmagent"
KERNELDRIVERDIR=/lib/modules/$(uname -r)/extra/cisco
ACMANIFESTDAT="${INSTPREFIX}/VPNManifest.dat"
NVMMANIFEST="ACManifestNVM.xml"
UNINSTALLLOG="/tmp/nvm-uninstall.log"

# Copy the config files under NVM dir to a NVM.temp dir if saveconfig flag is set
if [ "$1" = "-saveconfig" ]; then
    TMPNVMDIR="${INSTPREFIX}/NVM.tmp"
    mkdir -p ${TMPNVMDIR}
    # Except folders all files within the NVM directory will be cached
    find ${NVMDIR} -maxdepth 1 -type f ! -name lib,bin -exec cp -t ${TMPNVMDIR} {} +
fi

# Array of commom files to remove
COMMONFILELIST="${INSTPREFIX}/${NVMMANIFEST} \
          ${BINDIR}/${NVMAGENT} \
          ${LIBDIR}/libsock_fltr_api.so \
          ${LIBDIR}/libacciscossl.so \
          ${LIBDIR}/libacciscocrypto.so \
          ${LIBDIR}/libacruntime.so \
          ${LIBDIR}/libboost_date_time.so \
          ${LIBDIR}/libboost_filesystem.so \
          ${LIBDIR}/libboost_system.so \
          ${LIBDIR}/libboost_thread.so \
          ${LIBDIR}/libboost_signals.so \
          ${LIBDIR}/libboost_chrono.so \
          ${BINDIR}/nvm_uninstall.sh"

# Make sure we are root
if [ `id | sed -e 's/(.*//'` != "uid=0" ]; then
  echo "Sorry, you need super user privileges to run this script."
  exit 1
fi

#!/bin/sh

PLUGINDIR=${INSTPREFIX}/bin/plugins
# update the VPNManifest.dat; if no entries remain in the .dat file then
# this tool will delete the file - DO NOT blindly delete VPNManifest.dat by
# adding it to the FILELIST above - allow this tool to delete the file if needed
updateVPNManifest

# check the existence of the manifest file - if it does not exist, remove the manifesttool
removeManifestTool

# move the plugins to a different folder to stop the NVM agent and then remove
# these plugins once NVM agent is stopped.
if [ -f "${PLUGINDIR}/libacnvmctrl.so" ]; then
  mv -f ${PLUGINDIR}/libacnvmctrl.so ${INSTPREFIX} 2>&1 >/dev/null
  echo "mv -f ${PLUGINDIR}/libacnvmctrl.so ${INSTPREFIX}" >> ${UNINSTALLLOG}
fi

# wait for 2 seconds for the NVM agent to exit
sleep 2

# ensure that the NVM agent is not running
killNVMAgent

# Remove the KDF
removeKDF

# Remove service plugin
echo "rm -f libacnvmctrl.so" >> /tmp/nvm-uninstall.log
rm -f ${PLUGINDIR}/libacnvmctrl.so >> /tmp/nvm-uninstall.log 2>&1

# Remove only those files that we know we installed
for FILE in ${COMMONFILELIST}; do
  removeInstalledFiles ${FILE}
done

# Remove the NVM directory
# During an upgrade, this dir(profile,cache,kconfig files) will be moved and restored by
# installer scripts
removeNVMDir

echo "Successfully removed Cisco AnyConnect Network Visibility Module from the system." >> ${UNINSTALLLOG}
echo "Successfully removed Cisco AnyConnect Network Visibility Module from the system."

exit 0
