#!/bin/sh

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

usage_exit() {
    echo "usage: $0 [--allhosts] [-c conffile.conf] [--clobber_old_data] [--mkbtrfs]"
    exit
}

. $LIBDIR/ceph_common.sh


allhosts=0
clobber=""
mkbtrfs=0
numosd=
usecrushmap=
verbose=0

while [ $# -ge 1 ]; do
case $1 in
    -v )
	    verbose=1;
	    ;;
    --allhosts | -a)
	    allhosts=1
	    ;;
    --clobber_old_data)
	    clobber="--clobber"
	    ;;
    --mkbtrfs)
	    mkbtrfs=1
	    ;;
    --conf_file | -c)
	    [ "$2" == "" ] && usage_exit
	    shift
	    conf=$1
	    ;;
    --numosd)
	    [ "$2" == "" ] && usage_exit
	    shift
	    numosd=$1
	    ;;
    --crushmap)
	    [ "$2" == "" ] && usage_exit
	    shift
	    usecrushmap=$1
	    ;;
    *)
	    echo unrecognized option \'$1\'
	    usage_exit
	    ;;
esac
shift
done

get_name_list "$@"

# create the monmap if we're doing mon0
if [[ $what =~ "mon0" ]]; then
    # first, make a list of monitors
    mons=`$CCONF -c $conf -l mon | egrep -v '^mon$' | sort`
    args=""
    for mon in $mons; do
	get_conf addr "" "mon addr" mon0 mon global
	args=$args" --add $addr"
    done

    # build monmap
    monmap="/tmp/monmap.$$"
    $BINDIR/monmaptool --create --clobber $args --print $monmap || exit 1

    # build osdmap
    osdmap="/tmp/osdmap.$$"
    if [[ $numosd = "" ]]; then
	maxosd=`$CCONF -c $conf -l osd | egrep -v '^osd$' | tail -1 | cut -c 4-`
	numosd=$(($maxosd + 1))
	echo max osd in $conf is $maxosd, num osd is $numosd
    fi
    $BINDIR/osdmaptool --clobber --createsimple $numosd $osdmap || exit 1

    # import crush map?
    get_conf crushmapsrc "$usecrushmap" "crush map" mon0 mon global
    if [[ $crushmapsrc != "" ]]; then
	echo Building crush map from $crushmapsrc
	crushmap="/tmp/crushmap.$$"
	$BINDIR/crushtool -c $crushmapsrc -o $crushmap
	$BINDIR/osdmaptool --clobber --import-crush $crushmap $osdmap
    fi
fi

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

    check_host || continue

    get_conf conf_file "$runtime_conf" "conf file" $sections

    if [[ $ssh = 1 ]] && [[ ! $pushed_to =~ " $host " ]]; then
	scp $osdmap $host:$osdmap
	scp $monmap $host:$monmap
	pushed_to="$pushed_to $host "
    fi

    if [[ $type = "mon" ]]; then
	get_conf mon_data "" "mon data" $sections
	do_cmd "$BINDIR/mkmonfs $clobber $mon_data --mon $num --monmap $monmap --osdmap $osdmap"
    fi

    if [[ $type = "osd" ]]; then
	get_conf osd_path "" "osd data" $sections
	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`
	
	if [ $mkbtrfs -eq 1 ]; then
	    do_cmd "modprobe btrfs ; umount $btrfs_path ; mkfs.btrfs $btrfs_devs ; mount -t btrfs $first_dev $btrfs_path"
	fi

	[[ $ssh != "" ]] && scp $monmap $host:$monmap
	do_cmd "$BINDIR/cosd -c $conf_file --monmap_file $monmap --mkfs_for_osd $num $osd_path"
    fi

    if [[ $type = "mds" ]]; then
	# do nothing
	echo
    fi

done
