#!/bin/csh -f
#
# Copyright (c) 2005, Oracle. All rights reserved.  
#
#   NAME
#     osbcvt
#
#   DESCRIPTION
#     Utility that can be used to copy existing Reliaty Backup catalog to
#     the default location for new Oracle Secure Backup installation and  
#     perform the required format conversion
#
#   MODIFIED   (MM/DD/YY) 
#   rvullika    10/03/05 - Fix the web autostartup after RB to 
#                          OSB migration
#   rvullika    08/15/05 - rvullika_rbtoosb_cnv
#   rvullika    08/01/05 - Creation
#  
#   Usage
#   osbcvt [-srcdir <srcdir_name>] [-help]
#   srcdir_name : This refers to the location of the RB admin directory to be 
#                 moved. If not specified, the location is determined from
#                 /etc/rbconfig. The program exits with an error message if
#                 the srcdir is not specified and the machine is not an admin
#                 in a Reliaty Backup domain
#
#                
#  Common initialization
setenv OBTRACE 0
#  Process the arguments
@ i = 1
while ($i <= $#argv)
    if ("$argv[$i]" == "-srcdir") then
        if ($i < $#argv) then
            @ i = $i + 1
            set rbdatadir = $argv[$i]
        else
            goto usage
            endif
    else if ("$argv[$i]" == "-help") then
        goto usage
    else
usage:
        echo "Usage: osbcvt [-srcdir <srcdir_name>] [-help]"
        exit (1)
        endif
        @ i = $i + 1
    end
        

#  Common initialization and get the OSB admin directory path  
unset st
if (! $?installobinit) then
    if (-f initinstall.sh) then
        source initinstall.sh
        set st = $status
    else if (-f install/initinstall.sh) then
        source install/initinstall.sh
        set st = $status
    else
        echo "Error No initinstall.sh program exists in either '.' or './install'. "
        set st = 1
        endif
    if ($st) goto done
    endif
       
set obdir = `pwd`
set OB_TMP = "/tmp"
#  Get machine info
source $idir/mymachinfo.sh
set st = 1

done:
if (! $?obdir) then
    echo "The destination directory for the Reliaty Backup"
    echo "catalog could not be determined."
    exit (1)
    endif

#  See if Reliaty Backup is already installed on this host

if (-f /etc/rbconfig) then
    set rbdir_tmp = `grep -i < /etc/rbconfig "^[ 	]*rb[  ][ 	]*dir:[ 	]"`
    if (-d $rbdir_tmp[3]/bin && -d $rbdir_tmp[3]/etc) then
        set rb_installed 
        endif
    set rbadmindir = `grep -i < /etc/rbconfig "^[ 	]*admin[  ][ 	]*dir:[ 	]"`
    if ($#rbadmindir > 0)then
        if (-e $rbadmindir[3]/state/general/vol-sequence && $?rb_installed) then
            if (! $?rbdatadir) set rbdatadir = $rbadmindir[3]        
            set rb_admin_installed 
            endif
        endif
    endif

if (! $?rbdatadir) then
    echo "The source directory for the Reliaty Backup"
    echo "catalog could not be determined."
    echo "This host is not a Reliaty Backup administrative server."
    echo "Please specify an alternative location for the Reliaty"
    echo "Backup admin data."
    goto usage
    endif

set pscmd    = '/bin/ps'
set psopt    = '-e'

switch ($my_os_name)
    case ULTRIX:
    case SunOS:
         set psopt = '-ax'
         breaksw
    default:
         breaksw
    endsw

#  Start the migration
# Make sure that Reliaty Backup daemons are not running
if ($?rb_installed) then
    set daemons = (rbserviced rbhttpd rbtar rbrobotd rbndmpd rbixd rbscheduled rbnotify rbexec rbtool )
    set num_daemons = 0
    foreach daemon ($daemons)
        set pids = (`$pscmd $psopt | grep -v grep | grep "$daemon" | awk '{ print $1 }'` )
        foreach pid ($pids)
            echo "Reliaty Backup daemon $daemon (process id = $pid) is running."
            @ num_daemons += 1
            end
        end

    if ($num_daemons > 0) then
        echo "Please terminate all the Reliaty Backup daemons using install/stoprb"
        echo "and then run install/osbcvt."
        exit(1)
        endif
    endif

echo "Starting data migration from Reliaty Backup to Oracle Secure Backup."
# Move the admin data directory from RB tree to OB tree
echo "The Reliaty Backup admin data will be moved to $obdir" 
set ob_dbdir = "$obdir/admin/state/general"
if (-d $obdir/admin) then
# If the webserver ssl password was already set, then save it to tmp
    if (-f $obdir/admin/config/default/daemons) then
        if (-f $OB_TMP/obdaemons) rm -f $OB_TMP/obdaemons
        cp $obdir/admin/config/default/daemons $OB_TMP/obdaemons
        endif
    rm -rf $obdir/admin
    endif
if (-d $rbdatadir) then
    mv $rbdatadir/ $obdir/admin
# If the webserver ssl password was saved earlier, restore it here
    if (-f $OB_TMP/obdaemons) then
        cp $OB_TMP/obdaemons $obdir/admin/config/default/daemons
        rm -f $OB_TMP/obdaemons
# else if an RB ssl password file was copied over, delete it so that
# makelinks.sh can do the right thing later on
    else if (-f $obdir/admin/config/default/daemons) then
        rm -f $obdir/admin/config/default/daemons
        endif
    endif
# Perform any format conversion that maybe needed for ISAM database
if (-d $OB_TMP/ob) then
    rm -rf $OB_TMP/ob
    endif
mkdir $OB_TMP/ob
if (-d $ob_dbdir) then
    if (-f $ob_dbdir/archives.dat) then
        $obdir/tools.$my_mach_arch/db2asc -t archive -o $OB_TMP/ob/archives $ob_dbdir/archives
        $obdir/tools.$my_mach_arch/asc2db -t archive -r -o $ob_dbdir/archives $OB_TMP/ob/archives >&/dev/null
        endif
    if (-f $ob_dbdir/volumes.dat) then
        $obdir/tools.$my_mach_arch/db2asc -t volume -o $OB_TMP/ob/volumes $ob_dbdir/volumes
        $obdir/tools.$my_mach_arch/asc2db -t volume -r -o $ob_dbdir/volumes $OB_TMP/ob/volumes >&/dev/null
        endif
    endif
echo " "
echo "Data migration from Reliaty Backup is complete."
echo " "

