#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_create.sh 1.4.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2008,2013 
# 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 
# @(#)89	1.4.1.2  src/bos/usr/lib/nim/methods/c_create.sh, cmdnim, bos720 5/9/13 14:50:50
#
#   COMPONENT_NAME: cmdnim
#
#   FUNCTIONS: mount_resources
#		set_flags
#
#

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
. ${NIM_METHODS}/c_sh_lib

#---------------------------- local defines     --------------------------------

#---------------------------- module globals    --------------------------------
OPTIONAL_ATTRS="cmd_flags devexports exclude_files fb_script image_data mksysb no_nim_client resolv_conf savewpar secattrs script source wpar_spec type nim_name nim_hostname connect"

#
# Initialize global variables.
#
cmd_flags=""
devexports="" 
exclude_files=""
fb_script="" 
image_data=""
mksysb=""
resolv_conf=""
savewpar=""
script=""
source=""
wpar_spec=""
verbose_output=""
remain_client="yes"

nim_name=""
nim_hostname=""
connect="shell"

create_rc=""

CREATE_CMD=${MKWPAR}

#
# Define mount points for resources.
#
devexports_access="${TMPDIR}/devexports"
exclude_access="${TMPDIR}/exclude_files"
image_data_access="${TMPDIR}/image_data"
resolv_conf_access="${TMPDIR}/resolv_conf"
fbscript_access="${TMPDIR}/fb_script"
mksysb_access="${TMPDIR}/mksysb"
savewpar_access="${TMPDIR}/savewpar"
script_access="${TMPDIR}/script"
secattrs_access="${TMPDIR}/secattrs"
wpar_spec_access="${TMPDIR}/wpar_spec"
nim_script="${TMPDIR}/nim_script"

############################################################################
#
#  Name:        set_flags
#
#  Function:    Checks global variables and sets flags and variables for
#               the command to run.
#
#  Returns:     Nothing
#
#  Parameters:  None
#
############################################################################
function set_flags
{
  # Remain NIM client after install?
  if [[ ${no_nim_client} = "yes" ]]
  then
    remain_client="no"
  fi

  # Show verbose output?
  if [[ ${show_details} = "yes" ]]
  then
    verbose_output="yes"
  fi

}

############################################################################
#
#  Name:        mount_resources
#
#  Function:    If resources were specified, then mount them
#               at specific mount points.
#
#  Returns:     Nothing
#
#  Parameters:  None
#
############################################################################
function mount_resources
{
  # use a devexports resource?
  if [[ -n "${devexports}" ]]
  then
    if [[ ${devexports} = ?*:?* ]]
    then
        nim_mount ${devexports} ${devexports_access}
        devexports_access="${access_pnt}"
    else
        devexports_access="${devexports}"
    fi
  fi

  # use an exclude_files resource?
  if [[ -n "${exclude_files}" ]]
  then
    if [[ ${exclude_files} = ?*:?* ]]
    then
        nim_mount ${exclude_files} ${exclude_files_access}
        exclude_files_access="${access_pnt}"
    else
        exclude_files_access="${exclude_files}"
    fi
  fi

  # use an image_data resource?
  if [[ -n "${image_data}" ]]
  then
    if [[ ${image_data} = ?*:?* ]]
    then
        nim_mount ${image_data} ${image_data_access}
        image_data_access="${access_pnt}"
    else
        image_data_access="${image_data}"
    fi
  fi

  # use a mksysb resource?
  if [[ -n "${mksysb}" ]]
  then
    if [[ ${mksysb} = ?*:?* ]]
    then
        nim_mount ${mksysb} ${mksysb_access}
        mksysb_access="${access_pnt}"
    else
        mksysb_access="${mksysb}"
    fi
  fi


  # use a resolv_conf resource?
  if [[ -n "${resolv_conf}" ]]
  then
    if [[ ${resolv_conf} = ?*:?* ]]
    then
        nim_mount ${resolv_conf} ${resolv_conf_access}
        resolv_conf_access="${access_pnt}"
    else
        resolv_conf_access="${resolv_conf}"
    fi
  fi

  # use a fb_script resource?
  if [[ -n "${fb_script}" ]]
  then
    if [[ ${fb_script} = ?*:?* ]]
    then
        nim_mount ${fb_script} ${fb_script_access}
        fb_script_access="${access_pnt}"
    else
        fb_script_access="${fb_script}"
    fi
  fi

  # use a savewpar resource?
  if [[ -n "${savewpar}" ]]
  then
    CREATE_CMD=${RESTWPAR}
    if [[ ${savewpar} = ?*:?* ]]
    then
        nim_mount ${savewpar} ${savewpar_access}
        savewpar_access="${access_pnt}"
    else
        savewpar_access="${savewpar}"
    fi
  fi

  # use a script resource?
  if [[ -n "${script}" ]]
  then
    if [[ ${script} = ?*:?* ]]
    then
        nim_mount ${script} ${script_access}
        script_access="${access_pnt}"
    else
        script_access="${script}"
    fi
  fi

  # use a secattrs resource?
  if [[ -n "${secattrs}" ]]
  then
    if [[ ${secattrs} = ?*:?* ]]
    then
        nim_mount ${secattrs} ${secattrs_access}
        secattrs_access="${access_pnt}"
    else
        secattrs_access="${secattrs}"
    fi
  fi

  # use a wpar_spec resource?
  if [[ -n "${wpar_spec}" ]]
  then
    if [[ ${wpar_spec} = ?*:?* ]]
    then
        nim_mount ${wpar_spec} ${wpar_spec_access}
        wpar_spec_access="${access_pnt}"
    else
        wpar_spec_access="${wpar_spec}"
    fi
  fi

}


############################################################################
#
#  Name:        create_nimscript
#
#  Function:    Created script performs post-installation customization operations, including:
#                 - copying over any post-installation resources such
#                   as fb_script or resolv_conf
#                 - if remaining a NIM client:
#                    - making inittab entry for nimclient -S running
#                    - adding nimsh or .rhosts access for master
#                 - invoking any allocated scripts
#               The script is invoked with $wpar_name (not necessarily NIM name) and 
#               $wpar_base_dir as args
#
#  Returns:     0 if it created a script, 1 otherwise
#
#  Parameters:  None
#
############################################################################
function create_nimscript
{
  (
  ${CAT} << !!
#!/usr/bin/ksh
# The script should be called with the name of the managed system
# as the first parameter and the path to the base directory for the
# managed system as the second parameter
  if [[ \$# -ne 2 ]] || [[ ! -d \$2 ]] || [[ \$2 = "/" ]]
  then
     emsg=\${${DSPMSG} ksh.cat 67 'The specified directory is not valid.'}
     print -u2 "\${emsg}\\n\"
     exit 1
  fi

  MANAGED_BASEDIR=\$2
!!


  # Was resolv_conf allocated?
  if [[ -n "${resolv_conf}" ]]
  then
     echo "${CP} ${resolv_conf_access} \${MANAGED_BASEDIR}${RESOLV_CONF}"
  fi

  # Was fb_script allocated?
  if [[ -n "${fb_script}" ]]
  then
     echo "${CAT} ${fb_script_access} >> \${MANAGED_BASEDIR}${FIRSTBOOT}"
  fi

  # If remaining a NIM client, set up the inittab, /etc/niminfo, 
  # and /etc/firstboot
  if [[ ${no_nim_client} != "yes" ]]
  then
     # Build /etc/niminfo
     echo 
     echo "${CAT} << !! > \${MANAGED_BASEDIR}${NIMINFO}"
     echo "#------------------ Network Install Manager ---------------"
     echo "# warning - this file contains NIM configuration information"
     echo "#       and should only be updated by NIM"

     echo "export NIM_NAME=${nim_name}"
     echo "export NIM_CONFIGURATION=${type}"
     echo "export NIM_HOSTNAME=${nim_hostname}"
     echo "export NIM_SHELL=${connect}"
     ${EGREP} "export.*NIM_MASTER_PORT=|export.*NIM_REGISTRATION_PORT|export.*NIM_MASTER_HOSTNAME"  ${NIMINFO}
     echo "!!"
     echo

     # This code will set variables to be reused by the script later 
     ${EGREP} "export.*NIM_MASTER_HOSTNAME_LIST=|export.*NIM_MASTER_UID" ${NIMINFO}

	      
     ${CAT} << !! 
     # Remove any nim entries that may currently be in the inittab file.
     # Old entries may be there if this machine was installed from a
     # mksysb, for example.
     # 
     ${CHROOT} \$2 ${RMITAB} nim >/dev/null 2>&1
     ${CHROOT} \$2 ${RMITAB} nimc >/dev/null 2>&1
     ${CHROOT} \$2 ${RMITAB} nimclient >/dev/null 2>&1
     ${CHROOT} \$2 ${RMITAB} nimsh >/dev/null 2>&1
!!

     echo
     echo ${CHROOT} \$2 ${MKITAB} -i cron nimclient:2:once:\"${NIMCLIENT} -S ${STATE_RUNNING} \> /dev/console 2\>\&1\"

     # This code basically comes from c_mk_nimclient
	# configure client for nimsh if connect sez to
	if [[ ${connect} = "nimsh" ]]; then
		echo "echo ${NIMCLIENT} -C >> ${MANAGED_BASEDIR}${FIRSTBOOT}"
		echo ${CHROOT} \$2 ${MKITAB} -i rctcpip nimsh:2:wait:\"/usr/bin/startsrc -g nimclient\"
	elif [[ ${connect} = "nimsh (secure)" ]]; then
		echo "echo ${NIMCLIENT} -c >> ${MANAGED_BASEDIR}${FIRSTBOOT}"
		echo ${CHROOT} \$2 ${MKITAB} -i rctcpip nimsh:2:wait:\"/usr/bin/startsrc -a \"-c\" -g nimclient\"
	else
             ${CAT} << !!
		# initialize the /.rhosts file
		if [[ -n \${NIM_MASTER_HOSTNAME_LIST} ]]
		then
			for i in \${NIM_MASTER_HOSTNAME_LIST}
			do
				echo "\${i} \${NIM_MASTER_UID:-root}" >>\${MANAGED_BASEDIR}/.rhosts
			done
		else
			echo "\${NIM_MASTER_HOSTNAME} \${NIM_MASTER_UID:-root}" >>\${MANAGED_BASEDIR}/.rhosts
		fi
!!
	fi

        ${CAT} << !!
	# make sure all the NIM_HOSTS are in /etc/hosts
	for i in ${NIM_HOSTS}
	do
		# separate IP address from hostname
		addr=\${i%%:*}
		hname=\${i##*:}

		# add the hostnames not already in /etc/hosts
		${EGREP} -q \"^\${addr}.*${hname}" \${MANAGED_BASEDIR}${HOSTS} 2>/dev/null || \
			echo \"\${addr}\t\${hname}" >> \${MANAGED_BASEDIR}${HOSTS}
	done
!!
     
  fi
  ) > ${nim_script}

  # Do we need a script? 
  if [[ -n "${resolv_conf}" ]] || [[ -n "${fb_script}" ]] || \
     [[ ${no_nim_client} != "yes" ]]
  then
      if [[ "${verbose_output}" = "yes" ]]
      then
         echo "=== nim_script contents ==="
         ${CAT} ${nim_script}
         echo "==========================="
      fi

      ${CHMOD} 700 ${nim_script}
      return 0
  else
      return 1
  fi
}

############################################################################
#
#  Name:        c_create
#
#  Function:    Sets up access to remote resources, then calls
#               mkwpar (no savewpar) or restwpar (savewpar)
#
#  Returns:     Nothing
#
#  Parameters:  None
#
############################################################################

# signal processing
trap cleanup 0
trap err_signal 1 2 11 15

# NIM initialization
nim_init

# set parameters from command line
while getopts :a:qvf c
do
  case ${c} in

    a)    # validate the attr ass
          parse_attr_ass "${OPTARG}"

	  # include the assignment for use in this environment
          eval ${variable}=\"${value}\"
          ;;

    f)    # force - does not do anything at this point
          ;;

    q)    # show attr info
          cmd_what
          exit 0
          ;;

    v)    # verbose mode (for debugging)
	  verbose_output="yes"
          set -x
          for i in $(typeset +f)
          do
            typeset -ft $i
          done
          ;;

    \?)   # unknown option
          error ${ERR_BAD_OPT} ${OPTARG}
          ;;

  esac
done

# check for missing attrs
ck_attrs

shift $((OPTIND - 1))

# set flags based on input attrs
set_flags

# mount resources to local access points.
mount_resources

# create script to initialize NIM client files in WPAR
create_nimscript || nim_script=

# set the following variable so that errors are appended to err_info
# attribute on the client object.
# UPDT_CL_ERR_INFO=yes

if [[ $CREATE_CMD = "${MKWPAR}" ]]
then
${MKWPAR} \
 ${cmd_flags:-${mksysb:+-t}} \
 ${devexports:+-b $devexports_access} \
 ${image_data:+-L image_data=$image_data_access} \
 ${mksysb:+-B $mksysb_access} \
 ${nim_script:+-k $nim_script} \
 ${secattrs:+-S secfile=$secattrs_access} \
 ${wpar_spec:+-f $wpar_spec_access} \
 -n $@ 2>${ERR}
 
else
${RESTWPAR} \
 ${cmd_flags} \
 ${devexports:+-B $devexports_access} \
 ${image_data:+-i $image_data_access} \
 ${nim_script:+-K $nim_script} \
 ${wpar_spec:+-w $wpar_spec_access} \
 -f ${savewpar_access} \
 -n $@ 2>${ERR}
fi

# Save the return code from the create command.
create_rc=$?

if [[ ${create_rc} -ne 0 ]]
then
   err_from_cmd ${CREATE_CMD}
fi

exit ${create_rc}

