#!/bin/sh
#
# Andrei.Ryjov  AKA  aryzhov@yahoo.com  Mon Jan 19 17:43:02 CET 2004
#

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

if egrep -s '^PRODDIR=' /cdrom/.cdtoc 2>&- ; then
  ProdMPoint=/cdrom/`grep '^PRODDIR=' /cdrom/.cdtoc 2>/dev/null | cut -d= -f2`
  ProdDir=`ls -d /cdrom/CDs/$TARGET/*/Product/. 2>/dev/null`
  [ `ls $ProdMPoint | grep '^SUNW' | wc -l` -ge 100 ] && {
    echo $ProdMPoint already contains Solaris packages - using it, no MediaMount
  } || {
    echo Mounting Product directory
    [ -n "$ProdDir" ] || Start_Shell $0
    mount -f lofs $ProdDir $ProdMPoint || Start_Shell $0
  }
else
  #
  # Overlay mount the real CD image
  #
  mount -O -f lofs $JS_MEDIA_DIR/CDs/$TARGET /cdrom || Start_Shell  $0  "Could not overlay /cdrom"
  # umount /cdrom
  # mount -o ro -f lofs $JS_MEDIA_DIR/CDs/$TARGET /cdrom || Start_Shell  $0  "Could not overlay /cdrom"
fi

# StartShell "From the end of MountMedia"

/usr/sbin/install.d/parse_dynamic_clustertoc
