#!/usr/bin/env bash
### BEGIN INIT INFO
# Provides:          umount_crypt
# Required-Start:    $remote_fs $syslog $network postfix
# Required-Stop:     $remote_fs $syslog $network postfix
# Default-Start:     
# Default-Stop:      0 1 6
# Short-Description: Unmounting crypto device(s)
### END INIT INFO

## ---
## - add with:
## -    update-rc.d umount_crypt stop 01 0 1 6 .
## ---

PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

## - Partition: data
## -
part_name=<choose a name i.e data>
crypt_partition="/dev/mapper/$part_name"
raw_partition=< i.e. /dev/vda3 or /dev/sda3 ..>
mount_point=<your mount point i.e /data>

stop_samba=false
stop_kvm=false
stop_mysql=false
stop_apache=false
stop_vservers=true

if $stop_vservers ; then
   if [ -n $vservers ];then
      echo ""
      vservers=`vserver-stat | grep -E "^[0-9]{2,}" | awk '{print$8}'`
      for _vserver in $vservers ; do
         echo -n -e  "\tStopping VServer \"$_vserver\".."
         vserver $_vserver stop > /dev/null 2>&1
         if [ $? -ne 0  ] ; then
            echo -e "\t[ \033[1;31mfailed\033[0m ]"
         else
            echo -e "\t[ \033[1;32mdone\033[0m ]"
         fi
      done
      echo ""
   fi
fi


if $stop_apache ; then
   if ps ax | grep /usr/sbin/apache2 | grep -v grep > /dev/null 2>&1 ||  ps ax | grep smbd | grep -v grep > /dev/null 2>&1 ; then 
      ## - Stopping Apache Webserver..
      ## -
      echo -n -e  "\n\tStopping Apache Weberver..\t"
      /etc/init.d/apache2 stop > /dev/null

      if [ $? -ne 0  ] ; then
         echo -e " [ \033[1;31mfailed\033[0m ]\n"
      else
         echo -e " [ \033[1;32mdone\033[0m ]\n"
      fi
   fi
fi


if $stop_mysql ; then
   if ps ax | grep /usr/sbin/mysqld | grep -v grep > /dev/null 2>&1 ||  ps ax | grep smbd | grep -v grep > /dev/null 2>&1 ; then 
      ## - Stopping Mysql..
      ## -
      echo -n -e  "\n\tStopping MySQL Server..\t\t"
      /etc/init.d/mysql stop > /dev/null

      if [ $? -ne 0  ] ; then
         echo -e " [ \033[1;31mfailed\033[0m ]\n"
      else
         echo -e " [ \033[1;32mdone\033[0m ]\n"
      fi
   fi
fi


if $stop_kvm ; then
   ## - Stopping KVM's
   ## -
   echo -n -e  "\n\tStopping Virtual Boxes..\t\t"
   /etc/init.d/kvm_shutdown_guests stop > /dev/null 2>&1
   if [ $? -ne 0  ] ; then
      echo -e " [ \033[1;31mfailed\033[0m ]\n"
   else
      echo -e " [ \033[1;32mdone\033[0m ]\n"
   fi

   echo -n -e  "\n\tStopping libvirt-bin..\t\t\t"
   /etc/init.d/libvirt-bin stop > /dev/null 2>&1
   if [ $? -ne 0  ] ; then
      echo -e " [ \033[1;31mfailed\033[0m ]\n"
   else
      echo -e " [ \033[1;32mdone\033[0m ]\n"
   fi
fi


if $stop_samba ; then
   ## - Stopping Samba-Server
   ## -
   echo -n -e  "\n\tStopping Samba Fileserver..\t\t"
   /etc/init.d/samba stop > /dev/null 2>&1

   if [ $? -ne 0  ] ; then
      echo -e " [ \033[1;31mfailed\033[0m ]\n"
   else
      echo -e " [ \033[1;32mdone\033[0m ]\n"
   fi
fi


if ! df | grep "$crypt_partition" > /dev/null 2>&1 ;then
   echo -e "\n\t\033[1;33mPartition \"$part_name\" is NOT mounted..\033[0m"
else
   echo -n -e  "\n\tUnmounting Partition $mount_point..\t\t"
   /bin/umount $crypt_partition > /dev/null 2>&1

   if [ $? -ne 0  ] ; then
      echo -e " [ \033[1;31mfailed\033[0m ]"
   else
      echo -e " [ \033[1;32mdone\033[0m ]"
   fi

   echo -n -e  "\tDecrypting $part_name..\t\t\t"
   cryptsetup luksClose $part_name > /dev/null 2>&1

   if [ $? -ne 0  ] ; then
      echo -e " [ \033[1;31mfailed\033[0m ]\n"
   else
      echo -e " [ \033[1;32mdone\033[0m ]\n"
   fi
fi

echo ""
exit 0
