#!/bin/sh
# Copyright 2010-2016 Jetico Inc. Oy
# All rights reserved.

# chkconfig: 345 99 01
# description: BestCrypt for Linux
#
### BEGIN INIT INFO
# Provides: bestcrypt
# Required-Start: dkms
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: BestCrypt for Linux
# Description: BestCrypt for Linux
### END INIT INFO


KERNEL_VERSION=`uname -r|sed 's/\(.\..\).*/\1/'`

case "$1" in
  start)
	echo "Starting BestCrypt..."
	rm -rf /dev/bcrypt?* 2>/dev/null

	cd /usr/src/bestcrypt-3.3.0/build/ && for module in $(ls -lsa *.ko | awk '{print $10}' | cut -d "." -f1); do modprobe $module; done
	#depmod -a

#    modprobe bestcrypt
#    modprobe bc_blowfish
#	modprobe bc_des
#	modprobe bc_gost
#	modprobe bc_camellia
#	modprobe bc_twofish
#	modprobe bc_bf448
#	modprobe bc_bf128
#	modprobe bc_3des
#	modprobe bc_idea
#	modprobe bc_rijn
#	modprobe bc_cast
#	modprobe bc_serpent
#	modprobe bc_rc6

#modprobe bc_noop

    echo "Started."
	;;

  stop)
    echo "Stopping BestCrypt..."

    if bctool is_guard_on ; then
        echo "on" > "$HOME"/.config/Jetico/guard_status
    else
        echo "off" > "$HOME"/.config/Jetico/guard_status
    fi

	bctool umountall

	for i in `lsmod | egrep "^\"?bc_.*\"?" | awk '{print $1}' `; do
        rmmod $i;
    done

    rmmod bestcrypt

    echo "Stopped."
	;;

  status)
	if [ -f /sys/class/misc/bestcrypt ] ; then
        echo "BestCrypt driver is loaded. List of loaded algorithms:\n"
	    ls /sys/class/misc/bectcrypt/plugins
	else
		echo "SysFS entry unavailable, possibly driver is not running."
	fi

    if bctool is_guard_on ; then
      echo "BestCrypt container file guard is on"
    fi

	;;

  restart)
	$0 stop
	$0 start
	;;

  *)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
esac

exit 0

