#!/bin/sh

#Read in the boot cmdline params
for i in `/bin/cat /proc/cmdline`; do
    export `echo $i`
    echo "export $i" >> /etc/profile
done

# Timeout to wait and number of retries to check for storageAPI to stop
sleep=3
check_count=30

/etc/init.d/isd stop
/etc/init.d/storageAPI stop
killall crond
# Check $check_count times and wait $sleep seconds each for storageAPI to stop
echo -n "Wait for storageAPI to stop ..."
count=0
while true; do
    pidof storageAPI.py >/dev/null 2>&1
    [ $? -ne 0 ] && break
    sleep ${sleep}s
    echo -n "."
    count=$(($count+1))
    [ $count -gt ${check_count} ] && break
done
[ $count -le ${check_count} ] && echo -n " storageAPI is not running."
echo ""

rm -f /DB/NODE-*/*.txn
if [ ! -e /onappstore/DB ]; then workdir=`ls -d /DB/NODE-* | awk 'NR==1'`; mkdir -p $workdir/onappstore/DB; mkdir -p /onappstore; ln -s $workdir/onappstore/DB /onappstore/DB; fi
/usr/pythoncontroller/initialise -B

/etc/init.d/storageAPI start
/usr/pythoncontroller/localupdate
/etc/init.d/isd guestconf
/etc/init.d/isd start 
/usr/sbin/crond 
