if [ -z "$1" ]
then
    echo ""
    echo "no server [dev/tst/beta/prd] speficified!"
    echo ""
    exit
fi

if [ -z "$2" ]
then
   echo ""
   echo "no data version specified!"
   echo ""
   exit
fi

server=$1
ver=$2

#coll_list="c_protein c_glycan c_motif c_site c_list c_network"
coll_list="c_motif"


wrk_dir="/data/shared/glygen/deployment"

db_name="glydb"
username="glydbadmin"
password="glydbpass"
api_cgi_dir="/var/www/cgi-bin/api/"
if [ $server == "beta" ]
then
    db_name="glydb_beta"
    username="glydb_betaadmin"
    password="glydb_betapass"
    api_cgi_dir="/var/www/cgi-bin/beta/api/"
fi


#########################################################
# PART-I: Update collections in tmpdb, and then 
# dump each tmpdb collection to disk. This step takes time
# but does not involve the targetdb
#########################################################
echo `date`" ... PART-I: started time stamp"

cd $wrk_dir

# Update collections in tmpdb 
dump_dir="/data/shared/glygen/mongodump/$server/"
archive_file="/data/shared/glygen/mongodump/$server/tmpdb_archive"

for collname in $coll_list
do
    python3 delete-tmpdb-objects.py -c $collname -v $ver
    python3 load-tmpdb-objects.py -c $collname -v $ver 
done

rm -f $archive_file

mongodump --username $username --password $password --db=tmpdb --archive=$archive_file

echo `date`" ... PART-I: end time stamp"

exit


######################################################
# PART-II: restore dump of tmpdb to final targetdb, 
# this is the only time the targetdb will be busy
#######################################################
echo `date`" ... PART-II: started time stamp"

for collname in $coll_list
do
    mongorestore --username $username --password $password --authenticationDatabase=$db_name --archive=$archive_file --drop --nsFrom="tmpdb.*" --nsTo="$db_name.*" --nsInclude="tmpdb.$collname"
done



echo `date`" ... PART-II: end time stamp"





