#!/bin/sh
#
# @(#)restore.sh	3.5 99/12/06 Sun Microsystems, Inc.
#
# Accept an archive of all the SKIP files and SunScreen configuration data
# and install it all back into its proper places in the system.
#

PATH="/opt/SUNWicg/SunScreen/ssadm:/usr/bin:/usr/sbin/"
export PATH

CONFIGS=/etc/opt/SUNWicg/SunScreen/configs
TMPDIR=/etc/opt/SUNWicg/SunScreen/.backup_temp
SS_LIBDIR=/opt/SUNWicg/SunScreen/lib

set_num=64	# SI18N_CONFIG_BIN_BACKUP_RESTORE
LS()		# localize a string
{
	msg_num=$1; shift
	if [ -x ${SS_LIBDIR}/catgets ]
	then ${SS_LIBDIR}/catgets $set_num $msg_num "$*"
	else echo "$*\c"
	fi
}

usage()
{
    echo>&2 "`LS 1 Usage`: ssadm restore"
    exit 1;
}

set -e

domain=default
while getopts vD: c
do case $c in
   D) domain=$OPTARG;;
   *) usage;;
   esac
done

# Make temp dir.
rm -rf $TMPDIR >/dev/null 2>&1
mkdir -m 0700 $TMPDIR
cd $TMPDIR

# This following line is to allow some rudimentary analysis of any
# failure of the restore operation by examining the error output.
#exec 3>&2 2>&1; set -x

# Extract the files from stdin.
tar -xf - SunScreenSKIPBackup.3.0.tar SunScreenConfigurationBackup.3.0.tar

if [ ! -r SunScreenSKIPBackup.3.0.tar ] ||
   [ ! -r SunScreenConfigurationBackup.3.0.tar ]
then msg=`LS 10 "Input is not a SunScreen 3.0 configuration backup file."`
     echo>&2 "ssadm restore: $msg"
     exit 1
fi

# Generate log record.
$SS_LIBDIR/logmsg -A restore -F NOTICE -P restore "reason:utf8=\"restored all configuration data from backup\"; user:utf8=\"$SSADM_USER\""

# Untar localid(s) & CA(s).
msg=`LS 11 "Restoring SKIP configuration..."`
echo "$msg"
(cd /etc/skip; exec tar -xf -) <SunScreenSKIPBackup.3.0.tar
/usr/sbin/skipd_restart

# Restore configurations.
msg=`LS 12 "Restoring SunScreen configurations..."`
echo "$msg"
(cd $CONFIGS
 if [ -d "$domain" ]
 then msg=`LS 13 "Existing data will be moved to the %s directory."`
      printf "$msg\n" "OLD$domain"
      rm -rf "OLD$domain"
      mv "$domain" "OLD$domain"
 fi
 domain -a "$domain"
 cd "$domain"
 exec tar -xf -
) <SunScreenConfigurationBackup.3.0.tar

# Clean up.
cd /
rm -rf $TMPDIR
