#!/bin/sh

if [ $# -lt 2 ]
then
    echo "Usage <command> <path> [options]"
    exit 1
fi

class_for_command() # in $1 command name, out $2 class
{
    case $1 in
	Chgrp|chgrp)
	    class=Chgrp
	    ;;

	Chmod|chmod)
	    class=Chmod
	    ;;

	Chown|chown)
	    class=Chown
	    ;;

	Ls|ls)
	    class=Ls
	    ;;

	Lstag|lstag)
	    class=Lstag
	    ;;

	Mkdir|mkdir)
	    class=Mkdir
	    ;;

	Readtag|readtag)
	    class=Readtag
	    ;;

	Writetag|writetag)
	    class=Writetag
	    ;;

        Setfacl|setfacl)
            class=Setfacl
            ;;

        Getfacl|getfacl)
            class=Getfacl
            ;;

	Writedata|writedata)
	    class=Writedata
	    ;;

        Checksum|checksum)
            class=Checksum
            ;;

	Rmtag|rmtag)
	    class=Rmtag
	    ;;

	*)
	    echo "Unknown command $1.  Available commands are:"
            echo "    chgrp chmod chown ls lstag mkdir readtag rmtag writetag"
            echo "    writedata setfacl getfacl checksum"
            exit 1
	    ;;
    esac

    cmd=$2=org.dcache.chimera.cli.$class
    eval $cmd
}

class_for_command "$1" class
shift


getCanonicalPath() # $1 = path                              
{                                                           
local link                                              
link="$1"                                               
if readlink -f . > /dev/null 2>&1; then                 
RET="$(readlink -f $link)"                          
else                                                    
RET="$(cd $(dirname $link); pwd)/$(basename $link)" 
while [ -h "$RET" ]; do                             
link="$(ls -ld $RET | sed 's/.*-> //')"         
if [ -z "${link##/*}" ]; then                   
RET="${link}"                               
else                                            
link="$(dirname $RET)/${link}"              
RET="$(cd $(dirname $link); pwd)/$(basename $link)" 
fi                                              
done                                                
fi                                                      
}                                                           

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

if [ -z "$DCACHE_HOME" ]; then                              
getCanonicalPath "$0"                                     
DCACHE_HOME="${RET%/*/*}"                                 
fi                                                          
if [ ! -d "$DCACHE_HOME" ]; then                            
echo "$DCACHE_HOME is not a directory"                    
exit 2                                                    
fi                                                          

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

lib="$(getProperty dcache.paths.share.lib)"
. ${lib}/utils.sh
. ${lib}/services.sh

dbpass=$(getProperty chimera.db.password)

classpath=$(printLimitedClassPath chimera bonecp \
    guava slf4j-api dcache-common logback-classic \
    logback-core logback-console-config \
    $(getProperty chimera.db.jar))

quickJava -Xbootclasspath/a:$classpath \
    -Dlog=${DCACHE_LOG:-warn} \
    ${class}  $(getProperty chimera.db.driver) \
    $(getProperty chimera.db.url) \
    $(getProperty chimera.db.dialect) \
    $(getProperty chimera.db.user) \
    ${dbpass:-""} \
    "$@"

