#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/eznim.sh 1.3 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2005 
# 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 

# @(#)30 1.3 src/bos/usr/lib/nim/methods/eznim.sh, cmdnim, bos720 9/26/05 15:38:53
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/nim_master_setup.sh
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#


NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
export NIMPATH NIM_METHODS
. ${NIM_METHODS}/c_sh_lib


#EZ NIM helper functions
#-----------------------------------------
# Function name: remove_eznim_env
#
#-----------------------------------------

remove_eznim_env()
{
  rc=0
  rm_fs=${1}

  # determine the eznim resource location
  locale=${EZNIM_FS}

  # reset the clients/clean bootp & nfs
  mac_list=`/usr/sbin/lsnim -a spot |${GREP} -v spot |${CUT} -f1 -d":"`
  /usr/sbin/nim -Fo reset ${mac_list} 2>/dev/null
  /usr/sbin/nim -Fo deallocate -a subclass=all ${mac_list} 2>/dev/null

  # unconfigure the NIM Master
  /usr/sbin/nim -o unconfig master
  rc=$?

  # deinstall the NIM master fileset
  ${INSTALLP} -u bos.sysmgt.nim.master
  let rc=$rc+$?

  # just in case -
  # remove locale & /export/nim/scripts from /etc/exports
  if [[ -n ${locale} ]]
  then
    /usr/sbin/exportfs -ua
    ${GREP} -v "^${locale}" /etc/exports >/tmp/exports.new
    ${GREP} -v "^/export/nim/scripts" /tmp/exports.new > /etc/exports
    /usr/sbin/exportfs -a

    if [[ -n ${rm_fs} ]]
    then
	  # remove the locale fs
	  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_REMOVING_FS} '\n>>>> Removing the %s filesystem.\n' ${locale}
	  ${UNMOUNT} $locale
      /usr/sbin/rmfs $locale
    fi
  fi

  # clean up /tftpboot
  /usr/bin/rm -e /tftpboot/*.[um]p.*
    
  exit $rc
}

#-----------------------------------------
# Function name: show_eznim_env
#
#-----------------------------------------

show_eznim_env()
{

  # First, set snapshot vars for the environment
  # -  standalones/lpp_sources/spots/mksysbs
  mac_states="/usr/sbin/lsnim -a Cstate -a Mstate"
  lpp_states="/usr/sbin/lsnim -a Rstate -a location -t lpp_source"
  spot_states="/usr/sbin/lsnim -a Rstate -a location -t spot"
  sysb_states="/usr/sbin/lsnim -a Rstate -a location -t mksysb"
  bid_states="/usr/sbin/lsnim -a Rstate -a location -t bosinst_data"
  res_states="/usr/sbin/lsnim -a Rstate -a location -t resolv_conf"
  
  # lets make pretty prints
  banner="${ECHO} ===================="
  mac_print="${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_NIM_MACS} 'NIM Machines'"
  lpp_print="${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_IMG_REPOS} 'Image Repository (lpp_source)'"
  spot_print="${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_BOOT_SUPPORT} 'Boot Support (SPOT)'"
  sysb_print="${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_SYSBS} 'System Backups (mksysb)'"
  bid_print="${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_BID_FILES} 'BOS Install Data File (bosinst_data)'"
  res_print="${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_RESOLV_CONF} 'Resolution File (resolv_conf)'"

  # Ready to print!
  $mac_print; $banner; $mac_states
  $lpp_print; $banner; $lpp_states
  $spot_print; $banner; $spot_states
  $sysb_print; $banner; $sysb_states
  $bid_print; $banner; $bid_states
  $res_print; $banner; $res_states

  return
}

#-----------------------------------------
# Function name: perform_update
#
#-----------------------------------------

perform_update()
{

  # get parameter values
  typeset source=$1
  typeset no_update=$2
  typeset prefix=
  typeset OLD_SPOT_NAME=


  # create lpp_source w/ fixes
  locale=${EZNIM_FS}
  OLD_SPOT_NAME=${EZNIM_SPOT}
  OLD_SYSB_NAME=${EZNIM_SYSB}

  # if we are not updating clients, then
  # perform SPOT replication
  if [[ -n ${no_update} ]]; then
	${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_CREATING_SPOT} '>>>> Creating the %s SPOT resource from %s.\n(This may take a while.)\n' "${EZNIM_SPOT}2" ${EZNIM_SPOT}
    /usr/sbin/nim -o define -t spot \
	  -a server=master -a location=${locale}/spot \
	  -a source=${EZNIM_SPOT} \
	  ${EZNIM_SPOT}2 >>${EZNIM_LOG} 2>&1

	EZNIM_SPOT="${EZNIM_SPOT}2"
  fi

  # update the NIM master environment
  /usr/sbin/nim_update_all -L ${locale}/lpp_source/$$ \
    -s ${EZNIM_SPOT} -d $source -M ${no_update}

  # apply naming convention for spot
  prefix=`/usr/sbin/lsnim -a oslevel_r ${EZNIM_SPOT} \
    | ${AWK} 'NR==2{print $3}'`
  
  if [[ $prefix = [1-9]* ]] && [[ ${EZNIM_SPOT} != ${prefix}* ]]; then
    nim -o change -a new_name=${prefix}spot_res ${EZNIM_SPOT}

	# update config file
	if [[ $? -eq 0 ]]; then
	  ${GREP} -v "${OLD_SPOT_NAME}" ${EZLOG} >/tmp/eznim.cfg
	  ${ECHO} "export EZNIM_SPOT=${prefix}spot_res" >>/tmp/eznim.cfg
	  ${MV} /tmp/eznim.cfg ${EZLOG}
	fi
  
    # remove the old boot images
    /usr/bin/rm -e /tftpboot/${EZNIM_SPOT}*

    # build the new boot images
    /usr/sbin/nim -Fo check ${prefix}spot_res
  fi

  # apply naming convention for mksysb
  prefix=`/usr/sbin/lsnim -a oslevel_r ${EZNIM_SYSB} \
    | ${AWK} 'NR==2{print $3}'`
  
  if [[ $prefix = [1-9]* ]] && [[ ${EZNIM_SYSB} != ${prefix}* ]]; then
    nim -o change -a new_name=${prefix}master_sysb ${EZNIM_SYSB}

	# update config file
	if [[ $? -eq 0 ]]; then
	  ${GREP} -v "${OLD_SYSB_NAME}" ${EZLOG} >/tmp/eznim.cfg
	  ${ECHO} "export EZNIM_SYSB=${prefix}master_sysb" >>/tmp/eznim.cfg
	  ${MV} /tmp/eznim.cfg ${EZLOG}
	fi
  fi

  return
}

#-----------------------------------------
# Function name: create_backup
#
#-----------------------------------------

create_backup()
{
  CLIENT=
  BACKUP=
  rc=0
  locale="${EZNIM_FS}/mksysb"

  # parse command line args
  while getopts c:b: option
  do
    case $option in
        c) CLIENT=$OPTARG;;
        b) BACKUP=$OPTARG;;
    esac
  done

  # Follow logic
  # -  If passed a client, create backup using nim define
  # -  If passed a location, define it as a backup (if local)
  # -  If passed both, ignore location

  if [[ -n ${CLIENT} ]]; then
    # run NIM define
	/usr/sbin/nim -o define -t mksysb \
	  -a server=master -a source=${CLIENT} \
	  -a location="${locale}/${CLIENT}_sysb" \
	  -a mk_image=yes ${CLIENT}_sysb
	rc=$?

  elif `/usr/bin/file ${BACKUP} 2>/dev/null | ${GREP} -q backup`; then
	# break up name from location
	sysb_name="${BACKUP##*/}"
	sysb_name="${sysb_name%%.*}"

    # define location as mksysb
	/usr/sbin/nim -o define -t mksysb \
	  -a server=master -a location="${BACKUP}" ${sysb_name}
	rc=$?
	if [[ ${rc} -eq 0 ]]; then 
	  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_CREATE_RES} 'Creating %s resource %s...' "mksysb" ${sysb_name}
	  /usr/bin/sleep 1
	  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_DONE} 'done\n'
	else
	  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_CREATE_MISC_RES_FAIL} '...Creation of the %s %s resource failed...\n' ${sysb_name} "mksysb"
	fi

  else
    # Unable to define the mksysb
	${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_ERR_CREATE_SB} 'Error creating system backup image.\n'
	${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_NO_CREATE_SB} 'No mksysb resource will be created.\n' 
	rc=1
  fi

  return $rc
}

#-----------------------------------------
# Function: find_prefix
#
# Description: determine the version_fixlevel 
# prefix to use in resource names based on
# the oslevel of the machine. 
#-----------------------------------------
find_prefix()
{
	maint_level=""

	# See if there is a recommended maintenance level
	maint_level=`oslevel -r 2>/dev/null`
	RC=$?

	# If not, we are at a base level system
	if [[ $RC -eq 1 ]]; then
	{
  		vrm_prefix=`oslevel | /usr/bin/awk -F. '{print $1 $2 $3 $4}' 2>/dev/null`
  		f_prefix=`oslevel | /usr/bin/awk -F. '{print $3 $4}' 2>/dev/null`
  		source_prefix="${vrm_prefix}_${f_prefix}"
		
		#echo "source_prefix is ${source_prefix}"
	}
	else
	{

  		vrm_prefix=`echo $maint_level | /usr/bin/awk -F. '{print $1 $2 $3 $4}' 2>/dev/null`
  		f_prefix=`echo $maint_level | /usr/bin/awk -F. '{print $3 $4}' 2>/dev/null`
  		source_prefix="${vrm_prefix}_${f_prefix}"
	}
	fi  
}

#-----------------------------------------
# Function: get_resource_number
#
# Description: This function finds out 
# what number needs to be appended 
# to the resources created via EZ NIM.
#-----------------------------------------

get_resource_number()
{
  res_type=${1}
  source_prefix=${2}

  counter=1

  new_item=${source_prefix}_${res_type}${counter}

  lsnim -a serves master | awk '{print $3}' | while read old_item; 
  do

	if [[ ${new_item} = ${old_item} ]]; then
		break;
	else
		((counter=counter+1)) 
	fi
  done

  resource_number=${source_prefix}_${res_type}_${counter}

#echo "next name is ${resource_number}"

  return		
}

#=============================================
# MAIN - eznim $@
#=============================================

# always source the eznim.cfg file
# if not found, create it based off
# resources from basic_res_grp
export EZLOG="/var/adm/ras/eznim.cfg"
if [[ -s ${EZLOG} ]]; then
  . ${EZLOG} 2>/dev/null

elif `/usr/sbin/lsnim basic_res_grp >/dev/null 2>&1`; then
  # find resources
  /usr/sbin/lsnim -g basic_res_grp >/tmp/eznim.cfg
  # touch config file
  >${EZLOG}

  # write file contents
  sysb=`${AWK} '/mksysb/{print $3}' /tmp/eznim.cfg`
  ${ECHO} "export EZNIM_SYSB=${sysb%;}" >>${EZLOG}
  spot=`${AWK} '/spot/{print $3}' /tmp/eznim.cfg`
  ${ECHO} "export EZNIM_SPOT=${spot%;}" >>${EZLOG}
  lpp=`${AWK} '/lpp_source/{print $3}' /tmp/eznim.cfg`
  ${ECHO} "export EZNIM_LPP=${lpp%;}" >>${EZLOG}
  loc=`/usr/sbin/lsnim -a location ${spot%;} | ${AWK} '/location/{print $3}'`
  ${ECHO} "export EZNIM_FS=${loc%\/spot*}" >>${EZLOG}

  # now source file
  . ${EZLOG} 2>/dev/null
  
else
  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_ERR_RES_GRP} '\nThe resource group %s is not defined.\nPlease define using desired resources for install - Exiting.\n' "basic_res_grp"
	exit 1
fi

# Exit if no command line parameters
[ -z "$1" ] && exit 1

FUNCTION=$1
shift

$FUNCTION "$@"

exit $?
