#!/bin/sh

# Prepares pools using BerkeleyDB for meta data for upgrade from dCache 2.5 to 2.6. Run this script
# if pools fail to start with a com.sleepycat.je.EnvironmentFailureException error after upgrade.


[ -f /etc/default/dcache ] && . /etc/default/dcache       
[ -f /etc/dcache.env ] && . /etc/dcache.env               

if [ -z "$DCACHE_HOME" ]; then                            
DCACHE_HOME="/usr/share/dcache"                         
fi                                                        
if [ ! -d "$DCACHE_HOME" ]; then                          
echo "$DCACHE_HOME is not a directory"                  
exit 2                                                  
fi                                                        

DCACHE_CLASSPATH=${DCACHE_HOME}/classes/*                 
DCACHE_DEFAULTS=${DCACHE_HOME}/defaults                   
DCACHE_CACHED_CONFIG=/var/lib/dcache/config/cache         
. ${DCACHE_HOME}/lib/loadConfig.sh

for domain in $(getProperty dcache.domains); do
    for cell in $(getProperty dcache.domain.cells "$domain"); do
        service=$(getProperty dcache.domain.service "$domain" "$cell")
        if [ "$service" = "pool" -a $(getProperty metaDataRepository "$domain" "$cell") = "org.dcache.pool.repository.meta.db.BerkeleyDBMetaDataRepository" ]; then
            path=$(getProperty path "$domain" "$cell")
            echo "Preparing $path for upgrade"
            quickJava -jar $(getProperty dcache.paths.share)/misc/je-4.1.21.jar DbPreUpgrade_4_1 -h "$path/meta"
        fi
    done
done
