#!/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 ceph.conf] [--clobber_old_data] [--mkbtrfs]"
    exit
}

. $LIBDIR/ceph_common.sh


allhosts=0
clobber=""
mkbtrfs=0
numosd=
usecrushmapsrc=
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 | -c)
	    [ "$2" = "" ] && usage_exit
	    shift
	    conf=$1
	    ;;
    --numosd)
	    [ "$2" = "" ] && usage_exit
	    shift
	    numosd=$1
	    ;;
    --crushmapsrc)
	    [ "$2" = "" ] && usage_exit
	    shift
	    usecrushmapsrc=$1
	    ;;
    --crushmap)
	    [ "$2" = "" ] && usage_exit
	    shift
	    usecrushmap=$1
	    ;;
    *)
	    echo unrecognized option \'$1\'
	    usage_exit
	    ;;
esac
shift
done

verify_conf

get_name_list "$@"

# create the monmap if we're doing mon0
if echo $what | grep -q mon0 ; then
    # first, make a list of monitors
    mons=`$CCONF -c $conf -l mon | egrep -v '^mon$' | sort`
    args=""

    type="mon"
    for name in $mons; do
	id=`echo $name | cut -c 4- | sed 's/\\.//'`
	get_conf addr "" "mon addr"
	args=$args" --add $addr"
    done

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

    # build osdmap
    osdmap="/tmp/osdmap.$$"
    if [ -z "$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 "$usecrushmapsrc" "crush map src" mon0 mon global
    if [ -n "$crushmapsrc" ]; then
	echo Compiling crush map from $crushmapsrc to $crushmap
	crushmap="/tmp/crushmap.$$"
	$BINDIR/crushtool -c $crushmapsrc -o $crushmap
    fi
    get_conf crushmap "$usecrushmap" "crush map" mon0 mon global
    if [ -n "$crushmap" ]; then
	echo Importing crush map from $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'
    id=`echo $name | cut -c 4- | sed 's/\\.//'`
    num=$id

    check_host || continue

    if [ -n "$ssh" ] && ( echo $pushed_to | grep -v -q " $host " ); then
	scp -q $osdmap $host:$osdmap
	scp -q $monmap $host:$monmap
	pushed_to="$pushed_to $host "
    fi

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

    if [ "$type" = "osd" ]; then
	get_conf osd_data "" "osd data"
	get_conf btrfs_path "$osd_data" "btrfs path"  # mount point defaults so osd data
	get_conf btrfs_devs "" "btrfs devs"
	first_dev=`echo $btrfs_devs | cut '-d ' -f 1`
	get_conf btrfs_opt "notreelog,flushoncommit" "btrfs options"
	[ -n "$btrfs_opt" ] && btrfs_opt="-o $btrfs_opt"
	
	do_cmd "test -d $osd_data || mkdir -p $osd_data"

	if [ $mkbtrfs -eq 1 ]; then
	    do_cmd "umount $btrfs_path ; for f in $btrfs_devs ; do umount \$f ; done ; modprobe btrfs ; mkfs.btrfs $btrfs_devs ; modprobe btrfs ; btrfsctl -a ; mount -t btrfs $btrfs_opt $first_dev $btrfs_path"
	fi

	[ -n "$ssh" ] && scp $monmap $host:$monmap
	do_cmd "$BINDIR/cosd -c $conf --monmap $monmap -i $num --mkfs --osd-data $osd_data"
    fi

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

done
