#!/bin/sh
#
# Andrei.Ryjov  AKA  aryzhov@excite.com  Wed Nov 15 11:22:34 MET 2000
#
# The order in the profiles list is important so that we
# get more and more specific with each successive overwrite
#
# First and last lines are the least and the most specific ones, respectively.
# Please insert other profile choices inbetween
#
# Empty lines and even the informal comments are OK -
# such files don't exist, anyway
#
# Algorithm of profile generation may be changed in the future
# to something more intelligent, for instance, the profiles could be
# constructed from pieces representing the disk slicing, packages,
# and begin/finish/postinstall scripts - all as separate separate
# independant choices
#

MYLINES="^#!MediaLocation"
echo Searching for media location overwrites
MediaResource=`egrep "$MYLINES" $SI_PROFILE | awk '{print $2}'`
MediaAddPath=`egrep "$MYLINES" $SI_PROFILE | awk '{print $3}'`
NMedias=`echo $MediaResource | wc -w`
case `echo $NMedias` in

  0) ;;

  1)
    #
    # Try to mount on temporary location first
    #
    echo Media location override found: $MediaResource
    if echo $MediaResource | egrep -s ':/' ; then
      #
      # Looks like NFS path
      #
      SERVER=`echo $MediaResource | cut -d: -f1`
      M_PATH=`echo $MediaResource | cut -d: -f2`
      [ "$M_PATH" = "=" ] && {
	O_PATH=`mount | grep '^/cdrom on' | awk '{print $3}' | cut -d: -f2`
	O_SERV=`mount | grep '^/cdrom on' | awk '{print $3}' | cut -d: -f1`
	[ "$SERVER" = "$O_SERV" ] && { echo Same server $O_SERV - not remounting ; exit 0 ; }
	MediaResource=$SERVER:$M_PATH
      }
      CHECK="ping $SERVER"

    elif echo $MediaResource | egrep -s '^/dev/dsk/' ;then
      #
      # Looks like local device (disk?)
      #
      RDEV=`echo $MediaResource | sed 's/\/dsk\//\/rdsk\//'`
      CHECK="dd if=$RDEV of=/dev/null count=3"
    else
      CHECK=/bin/false
    fi

    #
    # We also can specify a correction path to media location
    # (where real .cdtoc, and directories like Patches and Packages reside)
    # This path is specified as third argument in #!MediaLocation pseudo-comment
    # in Profile
    #
    # We lofs-mount this path, if it exists
    #

    umount /cdrom

    $CHECK >/dev/null 2>&1 &&
      mkdir /tmp/.mnt.$$ &&\
	mount -o ro $MediaResource /tmp/.mnt.$$ && \
	  mount -o ro -f lofs /tmp/.mnt.$$/$MediaAddPath /cdrom || Start_Shell  $0

  ;;

  *)
    echo "
	ERROR:
	  More than one MediaLocation in $SI_PROFILE:
	    $MediaResource
    "
    cat $SI_PROFILE
    Start_Shell
  ;;

esac

ProdMPoint=/cdrom/`grep '^PRODDIR=' /cdrom/.cdtoc | cut -d= -f2`
ProdDir=/cdrom/CDs/$TARGET/*/Product
[ `ls $ProdMPoint | grep '^SUNW' | wc -l` -ge 100 ] && {
  echo $ProdMPoint already contains Solaris packages - using it, no MediaMount
} || {
  echo Mounting Product directory
  mount -f lofs $ProdDir $ProdMPoint || Start_Shell $0
  /usr/sbin/install.d/parse_dynamic_clustertoc
}

