#!/bin/bash
# ----------------------------------------------------------------------
# File: eosd
# Author: Andreas-Joachim Peters - CERN
# ----------------------------------------------------------------------

# ************************************************************************
# * EOS - the CERN Disk Storage System                                   *
# * Copyright (C) 2011 CERN/Switzerland                                  *
# *                                                                      *
# * This program is free software: you can redistribute it and/or modify *
# * it under the terms of the GNU General Public License as published by *
# * the Free Software Foundation, either version 3 of the License, or    *
# * (at your option) any later version.                                  *
# *                                                                      *
# * This program is distributed in the hope that it will be useful,      *
# * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
# * GNU General Public License for more details.                         *
# *                                                                      *
# * You should have received a copy of the GNU General Public License    *
# * along with this program.  If not, see <http://www.gnu.org/licenses/>.*
# ************************************************************************
#
#       /etc/rc.d/init.d/eosd
#
# Starts eosd 
#
# chkconfig: 345 96 6
# description: Starts eosd 
# processname: eosd

# Source function library.

export EOS_ROOT=/usr

. /etc/init.d/functions



if test ! -x ${EOS_ROOT}/sbin/eosd ; then
  echo Error: ${EOS_ROOT}/sbin/eosd does not exist!
  exit 0
fi

[ -f /etc/sysconfig/eos ] && . /etc/sysconfig/eos

RETVAL=0
prog="eosd"

[ -z "${EOS_FUSE_MOUNTS}" ] && EOS_FUSE_MOUNTS="main";

start() {
    for i in ${EOS_FUSE_MOUNTS}; do
        if [ "$i" = "main" ]; then
            [ -f /etc/sysconfig/eos ] && . /etc/sysconfig/eos
        else 
            if [ -f /etc/sysconfig/eos.$i ]; then
                . /etc/sysconfig/eos.$i
            else
                echo -n "Skipping fuse mount for instance $i - no /etc/sysconfig/eos.$i configuration file"
                echo 
                echo_warning
                echo 
                continue;
            fi
        fi

        # Check if eosd is already running
	echo
        eosdpid=`pgrep -f "${EOS_ROOT}/sbin/eosd ${EOS_FUSE_MOUNTDIR-/eos/}"`
	export EOS_FUSE_DEBUG=${EOS_FUSE_DEBUG-0}
	export EOS_FUSE_NOACCESS=${EOS_FUSE_NOACCESS-1}
	export EOS_FUSE_KERNELCACHE=${EOS_FUSE_KERNELCACHE-1}
	export EOS_FUSE_DIRECTIO=${EOS_FUSE_DIRECTIO-0}
	export EOS_FUSE_CACHE=${EOS_FUSE_CACHE-1}
	export EOS_FUSE_CACHE_SIZE=${EOS_FUSE_CACHE_SIZE-300000000}
	export EOS_FUSE_CACHE_READ=${EOS_FUSE_CACHE_READ-0}
	export EOS_FUSE_CACHE_WRITE=${EOS_FUSE_CACHE_WRITE-1}
	export EOS_FUSE_BIGWRITES=${EOS_FUSE_BIGWRITES-0}	
        export EOS_FUSE_EXEC=${EOS_FUSE_EXEC-0}
	export FUSE_OPT=""
	if [ "x$EOS_FUSE_BIGWRITES" = "x1" ]; then
	  FUSE_OPT="big_writes,"
        fi	
        if [ "x$eosdpid" = "x" ]; then
            echo -n "Starting $prog: "
	    mkdir -p /var/run/eosd/
	    cd /var/run/eosd/
	    mkdir -p ${EOS_FUSE_MOUNTDIR-/eos/}
	    chmod -R 755 ${EOS_FUSE_MOUNTDIR-/eos/}
	    unset KRB5CCNAME
	    unset X509_USER_CERT
	    unset X509_USER_KEY
	    ulimit -S -c ${DAEMON_COREFILE_LIMIT:-0}
	    ulimit -n 65000
	    test -c /dev/fuse || modprobe fuse
            ${EOS_ROOT}/sbin/eosd ${EOS_FUSE_MOUNTDIR-/eos/} -o${FUSE_OPT-""}max_readahead=131072,max_write=4194304,fsname=eos$i,allow_other,url=root://${EOS_FUSE_MGM_ALIAS-eosdev.cern.ch}/${EOS_FUSE_MOUNTDIR-/eos/}//  >& /dev/null 
            
            
            SRETVAL=$?
	    [ $SRETVAL -eq 0 ] && touch /var/lock/subsys/eosd
            [ $SRETVAL -eq 0 ] && echo_success;
            if [ $SRETVAL -gt 0 ]; then
                RETVAL=$SRETVAL; 
            fi
	    echo
	    echo "EOS_FUSE_DEBUG       : ${EOS_FUSE_DEBUG}"
	    echo "EOS_FUSE_NOACCESS    : ${EOS_FUSE_NOACCESS}"
	    echo "EOS_FUSE_KERNELCACHE : ${EOS_FUSE_KERNELCACHE}"
	    echo "EOS_FUSE_DIRECTIO    : ${EOS_FUSE_DIRECTIO}"
	    echo "EOS_FUSE_CACHE       : ${EOS_FUSE_CACHE}"
	    echo "EOS_FUSE_CACHE_SIZE  : ${EOS_FUSE_CACHE_SIZE}"
	    echo "EOS_FUSE_CACHE_READ  : ${EOS_FUSE_CACHE_READ}"
	    echo "EOS_FUSE_CACHE_WRITE : ${EOS_FUSE_CACHE_WRITE}"
	    echo "EOS_FUSE_BIGWRITES   : ${EOS_FUSE_BIGWRITES}"
	    echo "EOS_FUSE_EXEC        : ${EOS_FUSE_EXEC}"
	else 
            echo eosd already running. Pid is $eosdpid 
            echo Stop it first with 'service eosd stop'
            echo and try again or use directly 'service eosd restart'.
            RETVAL=1
	    echo_warning
        fi
    done
}

stop() {
    for i in ${EOS_FUSE_MOUNTS}; do
        if [ "$i" = "main" ]; then
            [ -f /etc/sysconfig/eos ] && . /etc/sysconfig/eos
        else 
            if [ -f /etc/sysconfig/eos.$i ]; then
                . /etc/sysconfig/eos.$i
            else
                echo -n "Skipping fuse mount for instance $i - no /etc/sysconfig/eos.$i configuration file"
                echo 
                echo_warning
                echo 
                continue;
            fi
        fi
        echo -n "Stopping $prog: "
        umdir="${EOS_FUSE_MOUNTDIR-/eos/}";
        pgrep -f "${EOS_ROOT}/sbin/eosd $umdir"	>& /dev/null
	existed=$?
        umount -f -l $umdir >& /dev/null
	pkill -f "${EOS_ROOT}/sbin/eosd $umdir"	
        pgrep -f "${EOS_ROOT}/sbin/eosd $umdir"	>& /dev/null
	killed=$?
	while [ $killed -eq 0 ]; 
	do 	
	        pkill -9 -f "${EOS_ROOT}/sbin/eosd $umdir"
	        usleep 100000
                pgrep -f "${EOS_ROOT}/sbin/eosd $umdir" >& /dev/null
		killed=$?
   	done
        rm -f /var/lock/subsys/eosd
        echo
	if [ $existed -eq 0 ]; then
	        echo_success	
	else
		echo_failure
        fi	
        echo
        RETVAL=0;
    done
}


restart() {
    stop
    start
}       

reload() {
    restart
}

case "$1" in
    start)
    start
    ;;
    stop)
    stop
    ;;
    reload|restart)
    restart
    ;;
    condrestart)
    
    if [ -f /var/lock/subsys/eosd ]; then
        restart
    fi
    ;;
    status)
    
    status $prog
    RETVAL=$?
    ;;
    *)
    
    echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
    RETVAL=1
esac

exit $RETVAL
