#!/bin/csh -f
#
# SUMMARY:      
# USAGE:        <script-name>
#
# AUTHOR:       Hugues DELAGRANGE
# ORG:          No Org
# E-MAIL:       Hugues.Delagrange@subatech.in2p3.fr
#
# ORIG-DATE:    15-Jan-02 at 14:09:10
# LAST-MOD:     17-Jan-02 at 14:52:49 by Hugues DELAGRANGE
#
# DESCRIPTION:
#    privateFD.csh DirPath FileName FDID mode
#    mode is either new or delete
# DESCRIP-END.

set num_args = ${#argv}

if ($num_args != 4) then
  echo 'Usage    :' './privateFD.csh DirPath FileName FDID mode(= new or del only)' !
  echo 'Example  :' './privateFD.csh /phenix/data06/delagran/TESTDB TEST 26013 new'
  exit
endif

set what = $argv[4]

if ( ${what} != 'new' && ${what} != 'del') then
  echo 'Usage    :' './privateFD.csh DirPath FileName FDID mode(= new or del only)' !
  echo 'Example  :' './privateFD.csh /phenix/data06/delagran/TESTDB TEST 26013 new'
  exit  
endif


echo Start private DB management for  $1 with mode ${what} !

set dir = $1

#check existence of dir
test -e $dir
set filestatus = $status

if( $filestatus != 0) then
  echo The file does not exist
  # if dir doesn't exist
  if ( ${what} == 'del' ) then
    echo You want to delete ${dir} but it does not exist !
    exit
  endif
  echo $1 does not exist !
  echo Create directory $1
  mkdir $1
  set mkdirstatus = $status
  if( $mkdirstatus != 0) then
    echo mkdir $1 failed !!!
    exit
  endif
#  change file rights
  chmod a+rwx $1
  ls -al $1
else if( $what == 'new' ) then
  echo $dir already exists and then can not activate new! 
    exit
endif


set TheFile = $argv[2]

setenv OO_FD_BOOT rmine00.rcf.bnl.gov::${dir}/${TheFile}

echo OO_FD_BOOT $OO_FD_BOOT

setenv OBJY_FDID $3
echo OBJY_FDID $OBJY_FDID

#set REF_BOOT = phenixls.phenix.bnl.gov::/export/database/objy/PHENIX_NEW
#set REF_BOOT = rmine201.rcf.bnl.gov::/phenix/data02/phnxreco/objy_try/PHENIX_TRY
#set REF_BOOT = phenixls.phenix.bnl.gov::/export/database/objy_try/PHENIX_TRY
set REF_BOOT = phnxobjy.phenix.bnl.gov::/database/main_federation/PHENIX_26000.boot

set LS_HOST = rmine00.rcf.bnl.gov
set FD_NUMBER = ${OBJY_FDID}
set BOOT = ${OO_FD_BOOT}

set PAGE_SIZE = 8192
set STANDALONE = ''

#
# We make a new Federation and create its schema from the one
# of the PHENIX_TRY_CAS Federation.
#

echo  what $what

if ( ${what} == 'new' ) then

  echo Creating new Federated Database...
          oocheckls -notitle ${LS_HOST} || oolockserver -notitle -noauto
          oonewfd -notitle \
               -fdfilepath ${dir}/TEST.FDDB \
               -lockserverhost ${LS_HOST} \
  		-jnldirpath ${dir} \
  		-fdfilehost rmine00.rcf.bnl.gov \
                -pagesize ${PAGE_SIZE} \
                -fdnumber ${FD_NUMBER} ${STANDALONE} ${BOOT}
        ooschemadump -outfile PHENIX_REF.dump ${REF_BOOT}
        ooschemaupgrade -infile PHENIX_REF.dump ${OO_FD_BOOT}
	rm -rf PHENIX_REF.dump

	#__________ makedbs ___________
	
        foreach database ( "Gains" "TofT0s" "TacPeds" "HLRatios" "Pedestals" "Pedestals5" "QAs" "LCTofs" "WalkTofs" )

            set name = calib.emc.${database}Tag0000
            oonewdb -db $name -filepath ${dir}/${name}
            set name = calib.emc.${database}Cal0000
            oonewdb -db $name -filepath ${dir}/${name}
    
        end

       oonewdb -db config.emc.femTag0000 -filepath ${dir}/config.emc.femTag0000
       oonewdb -db config.emc.femFile0000 -filepath ${dir}/config.emc.femFile0000

else if ( ${what} == 'del' ) then


    echo Deleting Federated Database ${BOOT} ..
    oocleanup -notitle ${STANDALONE} -local ${BOOT} 
    oodeletefd -notitle ${STANDALONE} ${BOOT}

    rm -fr $dir

else

  echo Unknown request type : ${what}
  exit
  

endif

setenv EMCAL_DATA_PBSC   /afs/rhic/phenix/phnxemc/PBSC/
setenv EMCAL_DATA_PBGL   /afs/rhic/phenix/phnxemc/PBGL/

