#!/bin/sh
#
#  The OpenDiamond Platform for Interactive Search
#  Version 2
#
#  Copyright (c) 2002-2005 Intel Corporation
#  All rights reserved.
#
#  This software is distributed under the terms of the Eclipse Public
#  License, Version 1.0 which can be found in the file named LICENSE.
#  ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS SOFTWARE CONSTITUTES
#  RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT
#

# read the configuration information
. ./remote.config

BINFILES="adiskd"

copybin() {
	for disk in $DISKLIST; do
		for bin in $BINFILES; do
			scp $bin ${REMOTEUSER}@${disk}:${BINPATH}/
		done
		scp diamond_config.sample ${REMOTEUSER}@${disk}~${REMOTEUSER}/.diamond/diamond_config
	done
}

remotesetup()
{
	for disk in $DISKLIST; do
                ssh -x ${REMOTEUSER}@${disk} "mkdir -p ~${REMOTEUSER}/.diamond"
      		ssh -x ${REMOTEUSER}@${disk} "mkdir -p ${BINPATH}"
                ssh -x ${REMOTEUSER}@${disk} "mkdir -p ${IDX_PATH}"
                ssh -x ${REMOTEUSER}@${disk} "mkdir -p ${CACHE_DIR}"
	done
}

install()
{
	remotesetup
	copybin
	#remote_configure
}


shutdown()
{
	for disk in $DISKLIST; do
      		ssh -x ${REMOTEUSER}@${disk} "killall -q -s 9 adiskd"
	done
}

start()
{
	shutdown
	for disk in $DISKLIST; do
		ssh -x ${REMOTEUSER}@${disk} "${BINPATH}/adiskd"
	done
}


gen_index()
{

	for disk in $DISKLIST; do
	ssh -x ${REMOTEUSER}@${disk} "find ${DATA_ROOT} -name \*.ppm > ${IDX_PATH}/GIDIDX0000000000000001"
	done


}

#
# Parse the commands to see what we should do.
#
case "$1" in
  start)
    echo -n "Starting Daemons: "
    start
    echo "done."
    ;;
                                                                                
  stop)
    echo -n "Stopping Daemons: "
    shutdown
    echo "done."
    ;;
  
  install)
    echo -n "Installing ... "
    shutdown
    install
    echo "done."
    ;;
  
  genindex)
    echo -n "Index ... "
    gen_index
    echo "done."
    ;;
  
                                                                                
  *)
    echo "Usage: dsetup {start|stop|install}"
    exit 1
    ;;
esac

exit 0
