#!/bin/sh
#********************************************************************#
#  Easy Bootstrap Installer - remove bootstrap                       #
#  Description: automatic bootstrap installer for synology nas       #
#  Author:      QTip from the german Synology support forum          #
#  Copyright:   2015-2016 by QTip                                    #
#  License:     GNU GPLv3 (see LICENSE)                              #
#  ----------------------------------------------------------------  #
#  Version:     0.96 - 25/01/2016                                    #
#  for more information check the changelog                          #
#********************************************************************#

if [ -f `/usr/bin/dirname $0`/lang/${SYNOPKG_DSM_LANGUAGE} ] ; then
     . `/usr/bin/dirname $0`/lang/${SYNOPKG_DSM_LANGUAGE}
else
     . `/usr/bin/dirname $0`/lang/enu
fi
. `/usr/bin/dirname $0`/lsof_lite

PKG_VOL_TMP=/volume1/@tmp
FOLDER=""
FOLDNAME=""
FOLDER=`/usr/bin/find /volume?/@optware/bin /volume?/@qnapware/opt/bin /volume?/@entware/bin /volumeUSB?/usbshare*/@???ware/bin -type f -iname ?pkg 2> /dev/null`
TIMESTAMP="`/bin/date "+%s"`"
USBSTATION=`/bin/get_key_value /etc.defaults/synoinfo.conf usbstation`
USBVOLUME=`/bin/get_key_value /etc.defaults/synoinfo.conf support_usb_volume`
PARTNUM=3
# detect device with usb enabled volumes (synology router rt1900ac)... /volumeUSB?/usbshare*/@???ware
[ "${USBSTATION}" == "yes" -a "${USBVOLUME}" == "yes" ] && PARTNUM=4

move_opt_back() {
     if [ -d ${PKG_VOL_TMP}/ebi/VirtualBox ] ; then
          [ -e /opt ] || /bin/mkdir -p /opt
          /bin/mv -f ${PKG_VOL_TMP}/ebi/* /opt
     fi
}

if [ -n "${select_remove_bssource}" -o "${pkgwizard_remove_bootstrap}" == "true" ] ; then
     # stop all running processes which started in /volume(x)/@optware/etc/init.d, /volume(x)/@qnapware/opt/etc/init.d, /volume(x)/@entware/etc/init.d or /volumeUSB?/usbshare*/@???ware/etc/init.d
     for FOLD in ${FOLDER} ; do
          BSTYPE=`echo ${FOLD} | /usr/bin/cut -d '/' -f${PARTNUM}`
          BSSOURCE=`echo ${FOLD} | /usr/bin/cut -d '/' -f1-${PARTNUM}`
          if [ "${BSTYPE}" == "@optware" -a -f ${FOLD}/opkg ] ; then
               [ -x /opt/etc/init.d/rc.unslung ] && /opt/etc/init.d/rc.unslung stop 2> /tmp/bootstrap_procstop_errors
          elif [ "${BSTYPE}" == "@entware" ] ; then
               [ -x /opt/etc/init.d/rc.unslung ] && /opt/etc/init.d/rc.unslung stop 2> /tmp/bootstrap_procstop_errors
          elif [ "${BSTYPE}" == "@qnapware" ] ; then
               [ -x /Apps/opt/etc/init.d/rc.unslung ] && /Apps/opt/etc/init.d/rc.unslung stop 2> /tmp/bootstrap_procstop_errors
          elif [ "${BSTYPE}" == "@optware" ] ; then
               if [ -f /usr/local/etc/rc.d/optware-ng.sh ] ; then
                    PROCSCRIPT=/usr/local/etc/rc.d/optware-ng.sh
               elif [ -f /usr/local/etc/rc.d/optware.sh ] ; then
                    PROCSCRIPT=/usr/local/etc/rc.d/optware.sh
               else
                    # for old optware installations
                    PROCSCRIPT="`/usr/bin/dirname $0`/optware_stop"
               fi
               [ -x ${PROCSCRIPT} ] && ${PROCSCRIPT} stop 2> /tmp/bootstrap_procstop_errors
          fi
     done

     if [ -f /tmp/bootstrap_procstop_errors ] ; then
          /bin/grep -ivE "(Invalid argument|In some|use the device)" /tmp/bootstrap_procstop_errors > /tmp/bootstrap_procstop_error
          while read LINE ; do
               if [ -n "${LINE}" ] ; then
                    ERR_="${ERR_}${LINE}<br>"
               fi
          done </tmp/bootstrap_procstop_error

          if [ -n "${ERR_}" ] ; then
               move_opt_back
               if [ -z "${pkgwizard_remove_bootstrap}" ] ; then
                    /bin/rm -rf /var/packages/${SYNOPKG_PKGNAME}
                    /bin/rm -rf ${SYNOPKG_PKGDEST}
               fi
               if [ "${BSTYPE}" == "@optware" ] ; then
                    echo "${PKG_PROCSTOP_ERROR1} <b>${BSSOURCE}/etc/init.d</b>${PKG_ERROR_PART2}${PKG_ERROR_PART3}<br>${PKG_PROCSTOP_ERROR2}<br>&nbsp;&nbsp;${ERR_}" > ${SYNOPKG_TEMP_LOGFILE}
               elif [ "${BSTYPE}" == "@entware" ] ; then
                    echo "${PKG_PROCSTOP_ERROR1} <b>${BSSOURCE}/etc/init.d</b>${PKG_ERROR_PART2}${PKG_ERROR_PART3}<br>${PKG_PROCSTOP_ERROR2}<br>&nbsp;&nbsp;${ERR_}" > ${SYNOPKG_TEMP_LOGFILE}
               elif [ "${BSTYPE}" == "@qnapware" ] ; then
                    echo "${PKG_PROCSTOP_ERROR1} <b>${BSSOURCE}/opt/etc/init.d</b>${PKG_ERROR_PART2}${PKG_ERROR_PART3}<br>${PKG_PROCSTOP_ERROR2}<br>&nbsp;&nbsp;${ERR_}" > ${SYNOPKG_TEMP_LOGFILE}
               fi
               exit 1
          fi
     fi
     /bin/rm -f /tmp/bootstrap_procstop_error* 2> /dev/null

     # the following part is identical for renaming and removing
     # collect all open files of folders in ${FOLDER}
     OPENFILES=""
     OPENFILES_=""
     FOLDER_="/opt ${FOLDER}"
     for FOLD in ${FOLDER_} ; do
          FOLD=`echo ${FOLD} | /usr/bin/cut -d '/' -f1-${PARTNUM}`
          [ "${FOLD}" != "/opt" ] && FOLDERSOURCES="${FOLDERSOURCES}`echo ${FOLD} | /usr/bin/cut -d '/' -f${PARTNUM}` "
          OPENFILES_=$(lsof_lite "${FOLD}")
          if [ -n "${OPENFILES_}" ] ; then
               OPENFILES="${OPENFILES}${OPENFILES_}<br>&nbsp;&nbsp;"
               OPENFILES_=""
          fi
     done

     if [ -n "${OPENFILES}" ] ; then
          move_opt_back
          if [ -z "${pkgwizard_remove_bootstrap}" ] ; then
               /bin/rm -rf /var/packages/${SYNOPKG_PKGNAME}
               /bin/rm -rf ${SYNOPKG_PKGDEST}
          fi
          if [ "${select_remove_bssource}" == "true" -o "${pkgwizard_remove_bootstrap}" == "true" ] ; then
               echo "${PKG_OPENFILES_ERROR1} <b>${FOLDERSOURCES}</b> ${PKG_ERROR_PART3}<br>${PKG_OPENFILES_ERROR2}<br>&nbsp;&nbsp;${OPENFILES}" > ${SYNOPKG_TEMP_LOGFILE}
          else
               echo "${PKG_OPENFILES_ERROR3} <b>${FOLDERSOURCES}</b> ${PKG_ERROR_PART3}<br>${PKG_OPENFILES_ERROR2}<br>&nbsp;&nbsp;${OPENFILES}" > ${SYNOPKG_TEMP_LOGFILE}
          fi
          exit 1
     fi

     # remove start scripts in /usr/local/etc/rc.d
     /bin/rm -f /usr/local/etc/rc.d/optware.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/entware.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/qnapware.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/optware-ng.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/entware-ng.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/S98rc.optware-ng.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/S98rc.entware-ng.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/S99optware-ng.sh 2> /dev/null
     /bin/rm -f /usr/local/etc/rc.d/S99entware-ng.sh 2> /dev/null

     # remove /etc/rc.optware
     /bin/rm -f /etc/rc.optware 2> /dev/null

     # remove /etc/rc.local
     /bin/rm -f /etc/rc.local 2> /dev/null

     # try to unmount /opt
     if [ `/bin/mount | /bin/grep -ic "opt"` -ne 0 ] ; then
          /bin/umount /opt 2>/dev/null
          if [ $? != 0 ] ; then
               move_opt_back
               if [ -z "${pkgwizard_remove_bootstrap}" ] ; then
                    /bin/rm -rf /var/packages/${SYNOPKG_PKGNAME}
                    /bin/rm -rf ${SYNOPKG_PKGDEST}
               fi
               echo ${PKG_BS_UNMOUNT_ERROR} > ${SYNOPKG_TEMP_LOGFILE}
               exit 1
          fi
     fi

     # remove PATH from /root/.profile
     /bin/sed -i '/^PATH=\/opt\/bin:\/opt\/sbin:\$PATH$/,/^export PATH$/d' /root/.profile
     /bin/sed -i '/^PATH=\$PATH:\/opt\/bin:\/opt\/sbin$/,/^export PATH$/d' /root/.profile
     /bin/sed -i '/^PATH=\/opt\/bin:\/opt\/sbin:\$PATH$/,/^export PATH$/d' /etc/profile
     /bin/sed -i '/#setup entware environment/,/\. \/opt\/etc\/profile/d' /root/.profile
     /bin/sed -i '/#setup entware-ng environment/,/\. \/opt\/etc\/profile/d' /root/.profile
     /bin/sed -i '/#setup qnapware environment/,/\. \/Apps\/opt\/etc\/profile/d' /root/.profile
fi

if [ "${select_remove_bssource}" == "true" -o "${pkgwizard_remove_bootstrap}" == "true" ] ; then
     # the following part is only for removing
     # check for existing folder /volume(x)/@optware, /volume(x)/@qnapware, /volume(x)/@entware or /volumeUSB?/usbshare*/@???ware and remove it
     for FOLD in ${FOLDER} ; do
          FOLD=`echo ${FOLD} | /usr/bin/cut -d '/' -f1-${PARTNUM}`
          /bin/rm -r ${FOLD} 2> /dev/null
     done
     [ -h /opt ] && /bin/rm -f /opt 2> /dev/null
     [ -d /opt ] && /bin/rm -rf /opt 2> /dev/null
     [ -d /Apps ] && /bin/rm -rf /Apps/ 2> /dev/null
     exit 0

elif [ "${select_remove_bssource}" == "false" ] ; then
     # the following part is only for renaming
     # remove /opt or /Apps/opt of a previously installed bootstrap
     if [ -h /opt ] ; then
          REALOPTOLD=`/usr/bin/readlink /opt`
          for FOLD in ${FOLDER} ; do
               FOLD=`echo ${FOLD} | /usr/bin/cut -d '/' -f1-${PARTNUM}`
               if [ "${REALOPTOLD}" == "${FOLD}" ] ; then
                    /bin/rm /opt 2> /dev/null
               fi
          done
     else
          [ -d /opt ] && /bin/mv -f /opt /opt_${TIMESTAMP} 2> /dev/null
          [ -d /Apps ] && /bin/rm -rf /Apps/ 2> /dev/null
     fi

     # check for existing folder /volume(x)/@optware, /volume(x)/@qnapware, /volume(x)/@entware or /volumeUSB?/usbshare*/@???ware and rename it to _${FOLD:1}_${TIMESTAMP}
     for FOLD in ${FOLDER} ; do
          FOLD=`echo ${FOLD} | /usr/bin/cut -d '/' -f1-${PARTNUM}`
          FOLDNEW=`echo ${FOLD} | /bin/sed 's/\/@/\/_/'`
          /bin/mv ${FOLD} ${FOLDNEW}_${TIMESTAMP} 2> /dev/null
     done
     exit 0
fi
