#!/bin/sh
# Start/stop ceph daemons

# if we start up as ./ceph-daemons, assume everything else is in the
# current directory too.
if [ `dirname $0` = "." ] && [ $PWD != "/etc/init.d" ]; then
    BINDIR=.
    LIBDIR=.
    ETCDIR=.
else
    BINDIR=/usr/bin
    LIBDIR=/usr/lib/ceph
    ETCDIR=/etc/ceph
fi

usage_exit() {
    echo "usage: $0 [options] {start|stop|restart} [mon|osd|mds]..."
    printf "\t-c conffile.conf\n"
    printf "\t--valgrind\trun via valgrind\n"
    exit
}

. $LIBDIR/ceph_common.sh


stop_daemon() {
    name=$1
    daemon=$2
    pidfile=$3
    signal=$4
    echo -n "Stopping ceph $name on $host..."
    do_cmd "while [ 1 ]; do 
	[ -e $pidfile ] || break
	pid=\`cat $pidfile\`
	while [ -e /proc/\$pid ] && grep -q $daemon /proc/\$pid/cmdline ; do
	    cmd=\"kill $signal \$pid\"
	    echo -n \$cmd...
	    \$cmd
	    sleep 1
	    continue
	done
	break
    done"
    echo done
}

## command line options
options=

version=0
dovalgrind=0
docrun=1
allhosts=0
debug=0
monaddr=
dobtrfs=1

while [[ $1 =~ '-' ]]; do     # FIXME: why not '^-'?
case $1 in
    -v | --verbose)
	    verbose=1
	    ;;
    --valgrind)
	    dovalgrind=1
	    ;;
    --novalgrind)
	    dovalgrind=0
	    ;;
    --allhosts | -a)
	    allhosts=1;
	    ;;
    --restart)
	    docrun=1
	    ;;
    --norestart)
	    docrun=0
	    ;;
    -m )
	    [ "$2" == "" ] && usage_exit
	    options="$options $1"
	    shift
	    MON_ADDR=$1
	    ;;
    --btrfs)
	    dobtrfs=1
	    ;;
    --nobtrfs)
	    dobtrfs=1
	    ;;
    --conf_file | -c)
	    [ "$2" == "" ] && usage_exit
	    options="$options $1"
	    shift
	    conf=$1
	    ;;
    *)
	    echo unrecognized option \'$1\'
	    usage_exit
	    ;;
esac
options="$options $1"
shift
done

# build mon_addr_arg with all mon addrs
n=0
mon_addr_arg=""
while [ 1 ]; do
    name="mon$n"
    get_conf mon_addr "" "mon addr" $name "mon" "global"
    [ "$mon_addr" == "" ] && break
    mon_addr_arg="$mon_addr_arg -m $mon_addr"
    n=$(($n + 1))
done


command=$1
shift

get_name_list "$@"

for name in $what; do
    type=`echo $name | cut -c 1-3`   # e.g. 'mon', if $item is 'mon1'
    num=`echo $name | cut -c 4-`
    sections="$name $type global"

    check_host || continue

    get_conf pid_file "/var/run/ceph/$name.pid" "pid file" $sections
    get_conf conf_file "$runtime_conf" "conf file" $sections

    # extract name-specific options from $conf
    if [[ $name =~ "mon" ]]; then
	get_conf mon_data "" "mon data" $sections
	module_opt="$mon_data"
	module_bin="$BINDIR/cmon"
    fi

    if [[ $name =~ "mds" ]]; then
	module_opt="$mon_addr_arg"
	module_bin="$BINDIR/cmds"
    fi

    if [[ $name =~ "osd" ]]; then
	get_conf osd_data "" "osd data" $sections
	get_conf osd_journal "" "osd journal" $sections
	[ "$osd_journal" != "" ] && osd_journal_cmd="-j $osd_journal" || osd_journal_cmd=""
	module_opt="$mon_addr_arg $osd_data $osd_journal_cmd"
	module_bin="$BINDIR/cosd"

	get_conf btrfs_path "$osd_path" "btrfs path" $sections  # mount point defaults so osd path
	get_conf btrfs_devs "" "btrfs devs" $sections
	first_dev=`echo $btrfs_devs | cut '-d ' -f 1`
    fi

    module_opt="-p $pid_file -c $conf_file $module_opt"

    case "$command" in
	start)
            # build final command
	    wrap=""
	    runflags="-d"
	    runmode=""
	    
	    get_conf_bool crun "$docrun" "restart on core dump" $sections
	    [[ $crun -eq 1 ]] && wrap="$BINDIR/crun"
	    
	    get_conf_bool valgrind "$dovalgrind" "valgrind" $sections
	    [[ $valgrind -eq 1 ]] && wrap="$wrap valgrind"
	    
	    [[ $wrap != "" ]] && runflags="-f" && runmode="&"
	    
	    cmd="$wrap $module_bin $runflags $module_opt $runmode"
	    
	    echo Starting ceph $name on $host...
	    [ $dobtrfs -eq 1 ] && do_cmd "mount -t btrfs $first_dev $btrfs_path"
            do_cmd "$cmd"
	    ;;
	
	stop)
	    stop_daemon $name c$type $pid_file
	    ;;

	forcestop)
	    stop_daemon $name c$type $pid_file -9
	    ;;
	    
	killall)
	    echo "killall c$type on $host"
	    do_cmd "killall -9 c$type"
	    ;;
	
	restart)
	    $0 $options stop $name
	    $0 $options start $name
	    ;;

	cleanlogs)
	    get_conf log_dir "/var/log/ceph" "log dir" $sections
	    get_conf log_sym_dir "/var/log/ceph" "log sym dir" $sections
	    do_cmd "for f in $log_sym_dir/$name*; do rm -f \`readlink \$f\` ; rm -f \$f ; done"
	    ;;

	cleanalllogs)
	    get_conf log_dir "/var/log/ceph" "log dir" $sections
	    get_conf log_sym_dir "/var/log/ceph" "log sym dir" $sections
	    do_cmd "rm -f $log_dir/* $log_sym_dir/*"
	    ;;

	*)
	    usage_exit
	    ;;
    esac
done

exit 0
