#!/bin/sh
#
# Some CD releases may miss platform-specific modules.
# Try to find them in Jumpstart repository and upload to the Jumpstart client
#

# debug_is_set && set -x

P=`uname -i`
D=$JS_PLTF_DIR/$TARGET/$P

[ -d /a/platform/$P -a -d /a/usr/platform/$P ] && exit 0

echo "
	WARNING: $P platform-specific directories do not exist
	on this media image - trying to upload the platform data
	from other media release for Solaris $TARGET
"

[ -d $D ] || {
    echo "
	ERROR: $P platform-specific directories are not available
	Installation will be unbootable
    "
    echo Starting shell ; /sbin/sh
    exit 0
}

echo === Copying $P platform specific directories
echo === from $D
cd $D || exit 1


# echo Debugging upl_platform for $TARGET - exit here     ; exit 0


for i in kernel platform usr/kernel usr/platform
do
  mv /a/$i /a/$i.std
  find $i -print | cpio -cdump /a
done

cd /a/platform
ln -s `uname -m` `uname -i`

#
# Little games with devices... For now, set up the rootdisk devices only
#


# echo === Skipping Root Disk Device rebuild in upl_platform === ; exit 0

DRIVER=`ls -l /dev/dsk/$SI_ROOTDISK | awk -F/ '{print $NF}' | cut -d@ -f1 | grep -iv total | sort | uniq | head -1`
MAJOR=`grep "^$DRIVER " /a/etc/name_to_major | awk '{print $2}'`
[ -z "$MAJOR" ] && {
  MAJOR=`expr 1 + \`cat /a/etc/name_to_major | awk '{print $2}' | sort -n | tail -1\``
  echo $DRIVER $MAJOR >>/a/etc/name_to_major
}

set +x

find /devices -name \*${DRIVER}@\* -ls | while read ino ref mode links user group major minor month day time name junk ; do
  newname=/a$name
  [ -c $newname   -o   -b $newname ] && continue
  mknod $newname `echo $mode | cut -c1` $MAJOR $minor
  chmod `ls $name | pkgproto | awk '{print $6}'` $newname
  chown $user:$group $newname
done

