#!/usr/bin/env bash

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

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

start_samba=false
start_kvm=false
start_mysql=false
start_apache=false
start_vservers=true
vservers="<vserver1, vserver2,..>"


echo -e "\n\033[1;34m"
/sbin/cryptsetup luksOpen $raw_partition $part_name > /dev/null 2>&1
echo -e "\033[0m"
if ! df | grep "$crypt_partition" > /dev/null 2>&1 ;then
   declare -i mount_count=`tune2fs -l $crypt_partition | grep "Mount count" | cut -d ":" -f 2`
   declare -i max_mount_count=`tune2fs -l $crypt_partition | grep "Maximum mount count" | cut -d ":" -f 2`
   if [ $max_mount_count -gt 0 ];then

      if [ $mount_count -ge  $max_mount_count ]; then
         echo ""
         echo -e "\tMaximal mount count of $max_mount_count was reached."
         echo -e "\tSo a filecheck on \"$crypt_partition\" will be initiated.."
         echo -e "\t\033[1;33m\E[5mPlease wait. This may take some time..\033[00m"
         echo ""
         e2fsck -p $crypt_partition  > /dev/null
         if [ $? -gt -0 ]; then
            echo -e "\n\t[\033[1;31mERROR\033[0m] filecheck on $crypt_partition failed.\n"
         fi
         tune2fs -C 0 $crypt_partition  > /dev/null
      fi

   fi
fi


if ! df | grep "$crypt_partition" > /dev/null 2>&1 ;then
   
   echo -n -e  "\tMounting crypto-partition \"$part_name\"..\t"

   /bin/mount $crypt_partition $mount_point > /dev/null 2>&1

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

   echo -e "\t\033[1;33mPartition \"$part_name\" already mounted..\033[0m"

fi


if $start_vservers ; then
   if [ -n "$vservers" ]; then
      echo ""
      for _vserver in $vservers ; do
         echo -n -e  "\tStarting VServer \"$_vserver\".."
         vserver $_vserver start > /dev/null 2>&1
         if [ $? -ne 0  ] ; then
            echo -e "\t [\033[40m\033[1;31m failed \033[0m]"
         else
            echo -e "\t [\033[1;32m done \033[0m]"
         fi
      done
      echo ""
   fi
fi


if $start_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

   ## - Starting Mysql..
   ## -
   echo -n -e  "\n\tStarting MySQL Server..\t\t"
   /etc/init.d/mysql start > /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 $start_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

   ## - Starting Apache..
   ## -
   echo -n -e  "\n\tStarting Apache Webserver..\t"
   /etc/init.d/apache2 start > /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  $start_samba ; then
   ## - Starting Samba..
   ## -
   if ps ax | grep nmbd | grep -v grep > /dev/null 2>&1 ||  ps ax | grep smbd | grep -v grep > /dev/null 2>&1 ; then 
      /etc/init.d/samba stop > /dev/null
      killall smbd > /dev/null 2>&1 
      killall nmbd > /dev/null 2>&1
   fi
   echo -n -e  "\n\tStarting Samba Fileserver..\t\t"
   /etc/init.d/samba start > /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 $start_kvm ; then
   ## - Starting libvirt-bin (also starts virtual boxes)
   ## -
   echo -n -e  "\n\tStarting libvirt-bin..\t\t\t"
   /etc/init.d/libvirt-bin start > /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
