#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/install/scripts/saveodms.sh 1.4 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1997,2006 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)56    1.4  src/bos/usr/sbin/install/scripts/saveodms.sh, cmdinstl, bos720 11/10/06 17:34:58
# COMPONENT_NAME: (CMDINSTL) command install
#
# FUNCTIONS: Save ODM data (default scripts for installp)
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 1997
# All Rights Reserved
# Licensed Material - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#-----------------------------------------------------------------------------
#
#     Program:  saveodms
#     Purpose:  save the history, product, and lpp databases for future use.
#               The saved vpd can be restored by restodms
#
#     Usage:    saveodms [-s] [-Z] {[savedir] | -f [savefile]}
#                  where:   savedir is the directory to save the vpd tree in.
#			        default value: orig
#				e.g. saveodms myvpd    causes the vpd to get
#				     save under /u/myvpd.odm
#				     use: restore myvpd      to restore.
#                           -Z  indicates just to return the size needed for this.
#                           -s  indicates save the short version of the odms.
#				Doesn't save inventory part.
#                           -f  indicates to save odm to a file instead of
#				a directory.
#				savefile is the file name to save odms into.
#				default value: orig
#				e.g. saveodms -f myvpd   causes the vpd to
#				     get save as file /u/myvpd.odm.bff.Z
#				     use: restore -f myvpd    to restore.
#
#
#  Notes for directory saves:
#               1. It makes sure that we have enough space in /u to succeed.
#               2. Time of save operation is left in the save directory in the 
#                  file timestamp. 
#               3. If the savedir exists, it is moved to savedir.old
#  Notes for directory saves:
#		1. It makes sure that we have enough space in /u to succeed.
#               2. If the savefile exists, it is moved to savefile.old
#               
#
#-----------------------------------------------------------------------------
#
export PATH="/usr/bin:/usr/sbin:/dev:/etc:$PATH"

DEFAULT_DIR=/u/orig.odm
DEFAULT_FILE=/u/orig.odm
BASEDIR=$DEFAULT_DIR
BASEFILE=$DEFAULT_FILE.bff
FILE_LIST="history history.vc inventory inventory.vc lpp lpp.vc product product.vc"
ODMDIR_LIST="/etc/objrepos  /usr/lib/objrepos /usr/share/lib/objrepos"
ODMFILE_LIST=""
SAVEFS=/home            # File system where VPD will be saved
cd / >/dev/null 2>&1

while [[ $1 != "" ]] ; do
  case $1 in 
      "-Z" )
         SIZE_NEEDED=1;;                   # they want to know just the size.
      "-s" )
         SMALL_VER=1                       # they want the short from <no inventory stuff>
         FILE_LIST="history history.vc lpp lpp.vc product product.vc" ;;
      "-f" ) 
         FILE_IT=1                         # they want it backed up to a file 
         if [[ $2 != "" && $2 != "-s" && $2 != "-Z" ]] ; then
           BASEFILE=/u/$2.odm.bff          # here is the file name
           shift
         fi ;;
      * )
         BASEDIR=/u/$1.odm ;;
  esac
  shift
done

# Save the old directory and/or file
if [[ -a $BASEFILE.Z && $FILE_IT -eq 1 && $SIZE_NEEDED -ne 1 ]]; then                    # save old file
  mv $BASEFILE.Z $BASEFILE.Z.old >/dev/null 2>&1
  echo "$BASEFILE.Z is moved to $BASEFILE.Z.old"
fi
if [[ -d $BASEDIR && $FILE_IT -ne 1 && $SIZE_NEEDED -ne 1 ]]; then                    # save old directory
  mv $BASEDIR $BASEDIR.old  >/dev/null 2>&1
  echo "$BASEDIR is moved to $BASEDIR.old"
fi

#
#  Expand /home if necessary and -f not specified, otherwise it is in tmp.
#

integer tot_space=0     # Total space in 512 blocks used by VPD
for i in $ODMDIR_LIST
do
  for j in $FILE_LIST
  do
     let tot_space=`du -s $i/$j | awk '{print $1}'`+$tot_space
  done
done
let tot_space=`du -s /usr/lib/objrepos/vendor | awk '{print $1}'`+$tot_space
let tot_space=`du -s /usr/lib/objrepos/vendor.vc | awk '{print $1}'`+$tot_space
 
if [[ $SIZE_NEEDED -eq 1 && $FILE_IT -ne 1 ]]; then
  let tot_space=$tot_space*512
  echo $tot_space
  cd - >/dev/null 2>&1
  exit 0
fi
if [[ $SIZE_NEEDED -ne 1 ]]; then
  let free=`df "$SAVEFS" | awk 'NR==2 {print $3}'`
  if [[ $tot_space -gt $free && $FILE_IT -eq 1 ]] ; then
     echo "** Not enough space for saveodms to succeed, quitting.. $SAVEFS too small **"
     cd - >/dev/null 2>&1
     exit 1
  elif [[ $tot_space -gt $free ]]; then
     let need=$tot_space-$free
     let need=$need*2
     chfs -a size=+$need $SAVEFS
     if [[ $? -ne 0 ]]; then    # destroy the .old directory 
       if [[ ! -d $BASEDIR.old ]];then
         echo "** Not enough space for saveodms to succeed, quitting.. $SAVEFS too small **"
         cd - >/dev/null 2>&1
         exit 1
       fi
       mv $BASEDIR.old /tmp
       echo "$BASEDIR.old is saved in /tmp"
     fi
  fi

# Create the tree under savedir if -f not specified.
#
  if [[ ! $FILE_IT -eq 1 ]] ; then
    mkdir -p $BASEDIR $BASEDIR/etc $BASEDIR/etc/objrepos >/dev/null 2>&1
    mkdir -p $BASEDIR $BASEDIR/usr $BASEDIR/usr/lib $BASEDIR/usr/lib/objrepos >/dev/null 2>&1
    mkdir -p $BASEDIR/usr/share $BASEDIR/usr/share/lib $BASEDIR/usr/share/lib/objrepos >/dev/null 2>&1
  fi
fi

# Copy the vpd   
#
for i in $ODMDIR_LIST 
do
  for j in $FILE_LIST 
  do
     if [[ $FILE_IT -ne 1 ]] ; then
       cp $i/$j  $BASEDIR/$i/$j
     else
       ODMFILE_LIST="$ODMFILE_LIST.$i/$j\n"
     fi
  done
done
if [[ $FILE_IT -ne 1 ]] ; then
  cp /usr/lib/objrepos/vendor $BASEDIR/usr/lib/objrepos/vendor
  cp /usr/lib/objrepos/vendor.vc $BASEDIR/usr/lib/objrepos/vendor.vc
else
  ODMFILE_LIST="$ODMFILE_LIST./usr/lib/objrepos/vendor\n"
  ODMFILE_LIST="$ODMFILE_LIST./usr/lib/objrepos/vendor.vc\n"
fi

if [[ $FILE_IT -ne 1 ]] ; then
  date > $BASEDIR/timestamp
  echo "VPD is saved in $BASEDIR"
else
  if [[ $SIZE_NEEDED -ne 1 ]]; then
    echo $ODMFILE_LIST | backup -iqf - | compress > $BASEFILE.Z
    if [[ ! -a $BASEFILE.Z ]] ; then
       echo "\nVPD failed to be saved."
    else
       echo "VPD is saved as $BASEFILE.Z"
    fi
  else
    echo `echo $ODMFILE_LIST | backup -iqf - | compress | wc -c`
  fi
fi
cd - >/dev/null 2>&1
exit 0