#!/bin/sh
#
# mkchroot 
# Copyright 11/30/00 Sun Microsystems, Inc. All Rights Reserved.
# "@(#)mkchroot	1.5 00/11/30 Sun Microsystems"
# 
trap ""  0 1 2 3 15
CP='cp -pr'
JAVA_HOME=${JAVA_BASE_DIR}
JAVA_HOME_NOSLASH=`echo $JAVA_HOME | sed -e "s/^\///"`
FILE_VERSION=${BASE_DIR}/${PRODUCT_DIR}/lib/SRAversion.properties
STAGE=1
ECHO="gettext mkchroot"

REQD_DISK_SPACE=500000
#PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/java/bin:/var/opt/SUNWips/bin:;export
# Rebranding changes
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/java/bin:/var/opt/SUNWps/bin:;export
LD_LIBRARY_PATH=/lib:/usr/lib:/usr/ucblib:;export
IDSAME_HOME=${IDSAME_BASE_DIR}/SUNWam


GrabConfig() {
  GC_FILE=$1
  GC_KEY=$2
  GC_SEPARATOR=$3

  ANSWER=`grep "^$GC_KEY$GC_SEPARATOR" $GC_FILE | uniq | sed -e "s/$GC_KEY$GC_SEPARATOR//"`
}

GrabConfig $FILE_VERSION "version" "="
VERSION=$ANSWER

echo "mkchroot version $VERSION"
echo

copyWithLinks(){
 cd /
 for i in $*; do  tar cvfh - .$i 2>/dev/null | (cd $ROOT ; tar xvf - 2>/dev/null) >/dev/null; done
 cd $ROOT
}

checkDiskSpace() {
 df -k $1 | grep "^/" |while read fs kb used avail cap mounted ; do
   available=`/bin/expr $REQD_DISK_SPACE \< $avail`
   if [ $available -ne 1 ] ; then
     rmdir $ROOT
     echo
     # echo 'Disk space is not sufficient.  Aborting mkchoot.'
     $ECHO "Disk space is not sufficient.  Aborting mkchoot.\n"
     exit 2
   fi
 done
}

checkStatus() {
  # check if there is a problem in executing the previous command
  if [ "x$?" != "x0" ] ; then

    # if so, abort after cleaning up.
    if [ $STAGE -ne 1 ]; then
      # some extra work needs to be done
      echo Error executing command
      echo Restoring from backup files ...
      cat /tmp/hosts.bak > /etc/hosts
      cat /tmp/nsswitch.bak > /etc/nsswitch.conf
      cat /tmp/vfstab.bak > /etc/vfstab
      echo Removing configuration ...
      rm /tmp/hosts.bak
      rm /tmp/nsswitch.bak
      rm /tmp/vfstab.bak
      umount ${ROOT}/${JAVA_HOME_NOSLASH}
      umount $ROOT/usr/share/lib
      umount $ROOT/usr/lib/mps
      umount $ROOT/proc
      umount $ROOT/dev/fd
      umount $ROOT/dev/random
    fi

    #during stage 1, the abort is simple
    echo
    if [ "x$1" != "x" ]; then
      #echo "Error:$1"
      $ECHO "Error:"
      echo $1
    fi

    cd /;rm -rf $ROOT
    if [ "x$?" != "x0" ] ; then
      # duh! the cleanup failed.
      # echo "Not a clean exit. Please remove $ROOT."
      $ECHO "Not a clean exit. Please remove "
			echo $ROOT
    fi
    exit 2
  fi
}

umask 0
# Run this as user root, group other
if [ `/usr/bin/id | /usr/bin/tr =\( ,, |
      /usr/bin/awk -F, '{print $2}'` != "0" ] ; then
  #echo "Must be run as root."
  $ECHO "Must be run as root.\n"
  exit 1
fi

if [ `/usr/bin/id | /usr/bin/tr =\( ,, |
      /usr/bin/awk -F, '{print $4}'` != "1" ] ; then
  $ECHO "Must be run as group other.  Run \"newgrp other\" and then re-run this script.\n"
  exit 1
fi

# Check file ownership to be root
if [ `ls -la "$0" | /usr/bin/awk '{print $3}'` != "root" ]; then
  $ECHO "Root must own this file.\n"
  exit 1
fi

BATCH=0
ROOT=""

# Check arguments
while [ "$1" != "" ]
do
  if [ "$1" = "-b" ] ; then
    $ECHO "Running in batch mode.\n"
    BATCH=1
  else
    ROOT=$1
  fi
  shift
done

if [ "$ROOT" = "" ]
then
  if [ $BATCH = 1 ] ; then
        $ECHO "No directory specified.\n"
        exit 1;
  else
   $ECHO "Enter the full path name of the directory which will be the chrooted tree:"
   read ROOT 
  fi
fi

if [ $ROOT = "/" ] ; then
  $ECHO "mkchroot cannot be executed on /\n"
  exit 1
fi
if [ X$ROOT = X ] ; then
  $ECHO "mkchroot cannot be executed on /\n"
  exit 1
fi

if [ -d $ROOT ] ; then
	echo "$ROOT\c"
  $ECHO " already exists\n"
  exit 1
fi


$ECHO "Using"
echo " $ROOT \c"
$ECHO "as root.\n"


mkdir -p $ROOT
chmod 555 $ROOT
checkStatus;
$ECHO "Checking available disk space..."
checkDiskSpace $ROOT
$ECHO "done\n"


# Check if mounted setuid
PARTITION=`/usr/sbin/df $ROOT | /usr/bin/tr \( " " | awk '{print $1}'`
OPTIONS=`/usr/sbin/mount | grep "^$PARTITION " | awk '{print $4}'`
if [ "`echo $OPTIONS | /usr/bin/grep setuid`" = "" ]
then
	echo "$PARTITION \c"
  $ECHO "does not seem to be mounted setuid.  Make sure"
  echo "$PARTITION \c"
  $ECHO "is mounted setuid first.\n"
  exit 1 
else
  echo "$ROOT \c"
  $ECHO "is on a setuid mounted partition.\n"
fi

$ECHO "Creating filesystem structure..."
cd $ROOT

mkdir dev
checkStatus
chmod 555 dev
echo "dev \c"

mkdir etc
checkStatus
chmod 555 etc
echo "etc \c"

mkdir sbin
checkStatus
chmod 555 sbin
echo "sbin \c"

mkdir usr
checkStatus
chmod 555 usr
echo "usr \c"

mkdir var
checkStatus
chmod 555 var
echo "var \c"

mkdir proc
checkStatus
chmod 0555 proc
echo "proc \c"

mkdir ./opt
checkStatus
chmod 1777 opt
echo "opt \c"

ln -s usr/bin bin
checkStatus
echo "bin \c"

ln -s usr/lib lib
checkStatus
echo "lib \c"

ln -s var/tmp tmp
checkStatus
echo "tmp \c"

mkdir etc/lib
checkStatus
chmod 555 etc/lib
echo "etc/lib \c"

mkdir usr/platform
checkStatus
chmod 555 usr/platform
echo "usr/platform \c"

mkdir usr/bin
checkStatus
chmod 555 usr/bin
echo "usr/bin \c"

mkdir usr/sbin
checkStatus
chmod 555 usr/sbin
echo "usr/sbin \c"

mkdir usr/lib
checkStatus
chmod 555 usr/lib
echo "usr/lib \c"

mkdir -p usr/openwin/lib
checkStatus
chmod 555 usr/openwin/lib
echo "usr/openwin/lib \c"

mkdir var/opt
checkStatus
chmod 555 var/opt
echo "var/opt \c"

mkdir var/tmp
checkStatus
chmod 1777 var/tmp
echo "var/tmp \c"

mkdir usr/share
checkStatus
chmod 555 usr/share
echo "usr/share \c"

mkdir usr/share/lib
checkStatus
chmod 555 usr/share/lib
echo "usr/share/lib \c"

mkdir dev/fd
checkStatus
chmod 555 dev/fd
echo "dev/fd \c"

# For 64 bit platforms ...
mkdir usr/lib/64
checkStatus
chmod 555 usr/lib/64
echo "usr/lib/64 \c"

mkdir -p usr/lib/lwp/64
checkStatus
chmod -R 555 usr/lib/lwp/64
echo "usr/lib/lwp/64 \c"


$ECHO "done\n"

$ECHO "Creating devices..."

mknod dev/null c 13 2
checkStatus
chmod 666 dev/null
echo "null \c"

mknod dev/tcp c 11 42
checkStatus
chmod 666 dev/tcp
echo "tcp \c"

mknod dev/ticots c 105 0
checkStatus
chmod 666 dev/ticots
echo "ticots \c"

mknod dev/ticlts c 105 2
checkStatus
chmod 666 dev/ticlts
echo "ticlts \c"

mknod dev/ticotsord c 105 1
checkStatus
chmod 666 dev/ticotsord
echo "ticotsord \c"

mknod dev/tty c 22 0
checkStatus
chmod 666 dev/tty
echo "tty \c"

mknod dev/udp c 11 41
checkStatus
chmod 666 dev/udp
echo "udp \c"

mknod dev/zero c 13 12
checkStatus
chmod 666 dev/zero
echo "zero \c"

mknod dev/conslog c 21 0
checkStatus
chmod 666 dev/conslog
echo "conslog \c"

$ECHO "done\n"

$ECHO "Copying/creating etc files..."

$CP /etc/group etc/group
checkStatus
echo "group \c"

$CP /etc/passwd etc
checkStatus
echo "passwd \c"

$CP /etc/shadow etc
checkStatus
echo "shadow \c"

$CP /etc/hosts etc
checkStatus
chmod 444 etc/hosts
echo "hosts \c"

$CP /etc/resolv.conf etc
checkStatus
echo "resolv.conf \c"

cat > etc/netconfig <<EOF
#
# The "Network Configuration" File.
#
# Each entry is of the form:
#
#       <network_id> <semantics> <flags> <protofamily> <protoname> \
#               <device> <nametoaddr_libs>
#
# The "-" in <nametoaddr_libs> for inet family transports indicates
# redirection to the name service switch policies for "hosts" and
# "services". The "-" may be replaced by nametoaddr libraries that
# comply with the SVr4 specs, in which case the name service switch
# will not be used for netdir_getbyname, netdir_getbyaddr,
# gethostbyname, gethostbyaddr, getservbyname, and getservbyport.
# There are no nametoaddr_libs for the inet family in Solaris anymore.
# 
udp        tpi_clts      v     inet    udp    /dev/udp        -
tcp        tpi_cots_ord  v     inet    tcp    /dev/tcp        -
rawip      tpi_raw       -     inet    -      /dev/rawip      -
ticlts     tpi_clts      v    loopback -      /dev/ticlts     straddr.so
ticotsord  tpi_cots_ord  v    loopback -      /dev/ticotsord  straddr.so
ticots     tpi_cots      v    loopback -      /dev/ticots     straddr.so
EOF
checkStatus
chmod 444 etc/netconfig
echo "netconfig \c"

$CP /etc/services etc
checkStatus
echo "services \c"

$CP /etc/nsswitch.conf etc
checkStatus
echo "nsswitch.conf"

$ECHO "done\n"

$ECHO "Copying binaries"
$CP /usr/bin/sh usr/bin/sh
echo ".\c"
$CP /usr/bin/ls usr/bin/ls
echo ".\c"
$CP /usr/bin/cat usr/bin/cat
echo ".\c"
$CP /usr/bin/date usr/bin/date
echo ".\c"
$CP /usr/bin/dirname usr/bin/dirname
echo ".\c"
$CP /usr/bin/mv usr/bin/mv
echo ".\c"
$CP /usr/bin/expr usr/bin/expr
echo ".\c"
$CP /usr/bin/file usr/bin/file
echo ".\c"
$CP /usr/bin/awk usr/bin/awk
echo ".\c"
$CP /usr/bin/grep usr/bin/grep
echo ".\c"
$CP /usr/bin/gettext usr/bin/gettext
echo ".\c"
$CP /usr/bin/echo usr/bin/echo
echo ".\c"
$CP /usr/bin/rm usr/bin/rm
echo ".\c"
$CP /usr/bin/sed usr/bin/sed
echo ".\c"
$CP /usr/bin/sleep usr/bin/sleep
echo ".\c"
$CP /usr/bin/hostname usr/bin/hostname
echo ".\c"
$CP /usr/bin/domainname usr/bin/domainname
echo ".\c"
$CP /usr/bin/cut usr/bin/cut
echo ".\c"
$CP /usr/bin/uname usr/bin/uname
echo ".\c"
$CP /usr/bin/ksh usr/bin/ksh
echo ".\c"
$CP /usr/bin/basename usr/bin/basename
echo ".\c"
$CP /usr/bin/id usr/bin/id
echo ".\c"
$CP /usr/bin/chmod usr/bin/chmod
echo ".\c"
$CP /usr/bin/nohup usr/bin/nohup
echo ".\c"
$CP /usr/bin/pkginfo usr/bin/pkginfo
echo ".\c"
$CP /usr/bin/su usr/bin/su
echo ".\c"
$CP /usr/bin/chown usr/bin/chown
echo ".\c"
$CP /usr/bin/ftp usr/bin/ftp
echo ".\c"
$CP /sbin/ifconfig sbin/ifconfig
echo ".\c"
$CP /usr/sbin/mount usr/sbin/mount
echo ".\c"
$CP /usr/sbin/umount usr/sbin/umount
echo ".\c"
$CP /usr/sbin/mknod usr/sbin/mknod
echo ".\c"
$CP /usr/bin/uniq usr/bin/uniq
echo ".\c"
$CP /usr/bin/tr usr/bin/tr
echo ".\c"

#copying ps
for isalist in `isalist` ; do
  if [ -x /usr/bin/$isalist/ps ] ; then
    if [ ! -d usr/lib/$isalist ] ; then
      mkdir -p usr/lib/$isalist
     # $CP /usr/lib/$isalist/* usr/lib/$isalist
      copyWithLinks /usr/lib/$isalist
    fi
    $CP /usr/bin/$isalist/ps usr/bin
    PSDONE=1
    break
  fi
done
if [ "x$PSDONE" = "x" ] ; then
  $CP /usr/bin/ps usr/bin
fi
echo ".\c"

#copying truss
for isalist in `isalist` ; do
  if [ -x /usr/bin/$isalist/truss ] ; then
    if [ ! -d usr/lib/$isalist ] ; then
      mkdir -p usr/lib/$isalist
      #$CP /usr/lib/$isalist/* usr/lib/$isalist
      copyWithLinks /usr/lib/$isalist
    fi
    $CP /usr/bin/$isalist/truss usr/bin
    TRUSSDONE=1
    break
  fi
done
if [ "x$TRUSSDONE" = "x" ] ; then
$CP /usr/bin/truss usr/bin
fi
echo ".\c"


# These two will be removed later...
$CP /usr/bin/rm usr/bin/rm
$CP /usr/sbin/setmnt usr/sbin/setmnt
chmod 555 sbin/*
chmod 555 usr/bin/*
chmod 555 usr/sbin/*

if [ ! -d $ROOT/usr/ucblib ]
    then
        mkdir -p $ROOT/usr/ucblib
fi
#$CP /usr/ucblib/libucb.so* $ROOT/usr/ucblib/
copyWithLinks /usr/ucblib/libucb.so*
echo ".\c"

# this one needs to go outside the chroot environment
mkdir -p $ROOT/usr/xpg4/bin
$CP /usr/xpg4/bin/id usr/xpg4/bin/id
chmod 755 usr/xpg4/bin/id
$ECHO "done\n"

$ECHO "Copying libraries"
#$CP /usr/platform/* usr/platform
copyWithLinks /usr/platform/*
echo ".\c"
#$CP /usr/lib/ld.so* usr/lib
copyWithLinks /usr/lib/ld.so*
echo ".\c"
#$CP /usr/lib/libc.so* usr/lib
copyWithLinks /usr/lib/libc.so*
echo ".\c"
#$CP /usr/lib/libdl.so* usr/lib
copyWithLinks /usr/lib/libdl.so*
echo ".\c"
#$CP /usr/lib/libintl.so* usr/lib
copyWithLinks /usr/lib/libintl.so*
echo ".\c"
#$CP /usr/lib/libnsl.so* usr/lib
copyWithLinks /usr/lib/libnsl.so*
echo ".\c"
#$CP /usr/lib/libsocket.so* usr/lib
copyWithLinks /usr/lib/libsocket.so*
echo ".\c"
#$CP /usr/lib/librpcsvc.so* usr/lib
copyWithLinks /usr/lib/librpcsvc.so*
echo ".\c"
#$CP /usr/lib/libw.so* usr/lib
copyWithLinks /usr/lib/libw.so*
echo ".\c"
#$CP /usr/lib/libproject* usr/lib
copyWithLinks /usr/lib/libproject*
echo ".\c"
copyWithLinks /usr/lib/libproc*
echo ".\c"
#$CP /usr/lib/libsecdb* usr/lib
copyWithLinks /usr/lib/libsecdb*
echo ".\c"
#$CP /usr/lib/libcmd* usr/lib
copyWithLinks /usr/lib/libcmd*
echo ".\c"
#$CP /usr/lib/libmp.so* usr/lib
copyWithLinks /usr/lib/libmp.so*
echo ".\c"
#$CP /usr/lib/libm.so* usr/lib
copyWithLinks /usr/lib/libm.so*
echo ".\c"
#$CP /usr/lib/libresolv.so* usr/lib
copyWithLinks /usr/lib/libresolv.so*
echo ".\c"
#$CP /usr/lib/nss_dns.so* usr/lib
copyWithLinks /usr/lib/nss_dns.so*
echo ".\c"
#$CP /usr/lib/nss_files.so* usr/lib
copyWithLinks /usr/lib/nss_files.so*
echo ".\c"
#$CP /usr/lib/nss_nis.so* usr/lib
copyWithLinks /usr/lib/nss_nis.so*
echo ".\c"
#$CP /usr/lib/nss_nisplus.so* usr/lib
copyWithLinks /usr/lib/nss_nisplus.so*
echo ".\c"
#$CP /usr/lib/straddr.so* usr/lib
copyWithLinks /usr/lib/straddr.so*
echo ".\c"
#$CP /usr/lib/libthread.so* usr/lib
copyWithLinks /usr/lib/libthread.so*
echo ".\c"
#$CP /usr/lib/libposix4.so* usr/lib
copyWithLinks /usr/lib/libposix4.so*
echo ".\c"
#$CP /usr/lib/libC.so* usr/lib
copyWithLinks /usr/lib/libC.so*
echo ".\c"
#$CP /usr/lib/librt.so* usr/lib
copyWithLinks /usr/lib/librt.so*
echo ".\c"
#$CP /usr/lib/libaio.so* usr/lib
copyWithLinks /usr/lib/libaio.so*
echo ".\c"
#$CP /usr/lib/libelf.so* usr/lib
copyWithLinks /usr/lib/libelf.so*
echo ".\c"
#$CP /usr/lib/libgen.so* usr/lib
copyWithLinks /usr/lib/libgen.so*
echo ".\c"
#$CP /usr/lib/libpthread.so* usr/lib
copyWithLinks /usr/lib/libpthread.so*
echo ".\c"
#$CP /usr/lib/libadm.so* usr/lib
copyWithLinks /usr/lib/libadm.so*
echo ".\c"
#$CP /usr/lib/libX* usr/lib
copyWithLinks /usr/lib/libX*
echo ".\c"

copyWithLinks /usr/lib/libsched.so*
echo ".\c"

chmod 555 usr/lib/*
#$CP /etc/lib/libdl.so* etc/lib
copyWithLinks /etc/lib/libdl.so*
echo ".\c"
#$CP /etc/lib/ld.so* etc/lib
copyWithLinks /etc/lib/ld.so*
echo ".\c"
chmod -R 555 etc/lib/*
#$CP /usr/openwin/lib/libX* usr/openwin/lib
copyWithLinks /usr/openwin/lib/libX*
echo ".\c"
#$CP /usr/openwin/lib/libICE.so* usr/openwin/lib
copyWithLinks /usr/openwin/lib/libICE.so*
echo ".\c"
#$CP /usr/openwin/lib/libSM.so* usr/openwin/lib
copyWithLinks /usr/openwin/lib/libSM.so*
echo ".\c"
#$CP /usr/lib/libCrun.so* usr/lib
copyWithLinks /usr/lib/libCrun.so*
#$CP /usr/lib/libatomic.so* usr/lib
copyWithLinks /usr/lib/libatomic.so*
echo ".\c"
#$CP /usr/lib/libdhcpagent.so* usr/lib
copyWithLinks /usr/lib/libdhcpagent.so*
echo ".\c"
chmod 555 usr/openwin/lib/*
#$CP /usr/lib/libCstd.so* usr/lib
copyWithLinks /usr/lib/libCstd.so*

#For SunOS 5.9
#$CP /usr/lib/librtld_db.so* usr/lib
copyWithLinks /usr/lib/librtld_db.so*
echo ".\c"

#$CP /usr/lib/libmd5.so* usr/lib
copyWithLinks /usr/lib/libmd5.so*
echo ".\c"

# For 64 bit platforms ...
copyWithLinks /usr/lib/64
echo ".\c"
copyWithLinks /usr/lib/lwp/64
echo ".\c"

# Copy in zoneinfo directory
$ECHO "Copying zoneinfo (about 1 MB).."
#$CP /usr/share/lib/zoneinfo usr/share/lib
copyWithLinks /usr/share/lib/zoneinfo
checkStatus
$ECHO "done\n"
# Copy in locale directory

$ECHO "Copying locale info (about 5 MB).........."
#$CP /usr/lib/locale usr/lib/locale
copyWithLinks /usr/lib/locale
checkStatus
$ECHO "done\n"

# creating backup files, if aborting has to be done
# if any of the backup commands don't work, just abort
# its not work taking the risk of messing the system files
$CP /etc/hosts /tmp/hosts.bak
checkStatus `$ECHO "Backing up hosts file failed.  mkchroot aborted!\n"`

$CP /etc/nsswitch.conf /tmp/nsswitch.bak
checkStatus `$ECHO "Backing up nsswitch.conf file failed.  mkchroot aborted!\n"`

$CP /etc/vfstab /tmp/vfstab.bak
checkStatus `$ECHO "Backing up vfstab file failed.  mkchroot aborted!\n"`

STAGE=2

# Add a note to /etc/hosts
echo "# NOTE:  Any changes that are made to this file that would affect
#        the service running in the chrooted directory should also be
#        made to $ROOT/etc/hosts
#
" > /tmp/a.$$
cat /tmp/a.$$ /etc/hosts > /tmp/b.$$
cat /tmp/b.$$ > /etc/hosts
rm -f /tmp/a.$$ /tmp/b.$$

# Add a note to /etc/nsswitch.conf
$ECHO "Adding comments to /etc/nsswitch.conf ..."
echo "# NOTE:  Any changes that are made to this file that would affect
# service running in the chrooted directory should also be
# to $ROOT/etc/nsswitch.conf
#
" > /tmp/a.$$

cat /tmp/a.$$ /etc/nsswitch.conf > /tmp/b.$$
cat /tmp/b.$$ > /etc/nsswitch.conf
rm -f /tmp/a.$$ /tmp/b.$$
$ECHO "done\n"

$ECHO "Creating loopback mount for"
echo "${ROOT}/${JAVA_HOME_NOSLASH} ...\c"


mkdir -p $JAVA_HOME_NOSLASH
mount -F lofs $JAVA_HOME $ROOT/$JAVA_HOME_NOSLASH

if [ $? -ne 0 ] ; then
  echo
  checkStatus `$ECHO "Loopback mounting $JAVA_HOME unsuccessful\n"`
  rm -f /tmp/hosts.bak /tmp/nsswitch.bak /tmp/vfstab.bak
  rm -rf $ROOT
  exit 2
fi
$ECHO "done\n"

$ECHO "Creating loopback mount for"
echo "$ROOT/usr/share/lib ...\c"

mount -F lofs /usr/share/lib $ROOT/usr/share/lib
if [ $? -ne 0 ] ; then
  echo
  checkStatus `$ECHO "Loopback mounting /usr/share/lib unsuccessful\n"`
  rm -f /tmp/hosts.bak /tmp/nsswitch.bak /tmp/vfstab.bak
  rm -rf $ROOT
  exit 2
fi
$ECHO "done\n"

$ECHO "Creating loopback mount for"
echo "$ROOT/usr/lib/mps ...\c"

mkdir usr/lib/mps
mount -F lofs /usr/lib/mps $ROOT/usr/lib/mps
if [ $? -ne 0 ] ; then
  echo
  checkStatus `$ECHO "Loopback mounting /usr/lib/mps unsuccessful\n"`
  rm -f /tmp/hosts.bak /tmp/nsswitch.bak /tmp/vfstab.bak
  rm -rf $ROOT
  exit 2
fi
$ECHO "done\n"

# Needed for jvm
$ECHO "Creating loopback mount for"
echo "$ROOT/proc...\c"
mount -F lofs /proc $ROOT/proc
checkStatus `$ECHO "Loopback mounting /proc unsuccessful.\n"`
$ECHO "done\n"

$ECHO "Creating loopback mount for"
echo "$ROOT/dev/random...\c"
touch $ROOT/dev/random
mount -F lofs /dev/random $ROOT/dev/random
checkStatus `$ECHO "Loopback mounting /dev/random unsuccessful.\n"`
$ECHO "done\n"

if [ $BATCH = 0 ] ; then
 $ECHO "Do you need /dev/fd (if you do not know what it means, press return)[n]:"
 read ANSWER
 if [ "x$ANSWER" = "xy" ] ; then
   $ECHO "Creating loopback mount for"
	 echo "$ROOT/dev/fd/...\c"

   mount -F lofs /dev/fd $ROOT/dev/fd
   checkStatus `$ECHO "Loopback mounting /dev/fd unsuccessful.\n"`
   $ECHO "done\n"
 fi
fi

$ECHO "Updating /etc/vfstab..."
if [ "x$ANSWER" = "xy" ] ; then
echo "# Next three line added for chroot
$JAVA_HOME   -    $ROOT/$JAVA_HOME_NOSLASH lofs    -      no     -
/usr/share/lib   -    $ROOT/usr/share/lib   lofs    -      no     -
/usr/lib/mps   -    $ROOT/usr/lib/mps   lofs    -      no     -
/proc   -       $ROOT/proc      lofs    -       no      -
/dev/random   -       $ROOT/dev/random      lofs    -       no      -
/dev/fd -       $ROOT/dev/fd    lofs    -       no      -" >> /etc/vfstab
else
echo "# Next three line added for chroot
$JAVA_HOME -    $ROOT/$JAVA_HOME_NOSLASH   lofs    -      no     -
/usr/share/lib   -    $ROOT/usr/share/lib   lofs    -      no     -
/usr/lib/mps   -    $ROOT/usr/lib/mps   lofs    -      no     -
/dev/random   -       $ROOT/dev/random      lofs    -       no      -
/proc   -       $ROOT/proc      lofs    -       no      -" >> /etc/vfstab  
fi
checkStatus `$ECHO "Updating vfstab unsuccessful\n"`
$ECHO "done\n"

#(cd usr; ln -s `basename $JAVA_HOME` java)

$ECHO "Creating a"
echo " $ROOT/etc/mnttab \c"
$ECHO "file, based on these loopback mounts.\n"
awk '{print $2,$1}' /etc/mnttab | sed '/^\/ / !d' |
        awk '{print $2,$1}' > $ROOT/tmp/newmnttab
echo "/proc /proc
fd /dev/fd
swap /tmp" >> $ROOT/tmp/newmnttab


#$ECHO "mkchroot successfully done.\n"

#exit 0

#Modification from previous mkipsenv File
$ECHO "Copying SRAP related data ..."
echo
IPS_HOME=${BASE_DIR}/${PRODUCT_DIR}

$ECHO "Using"
echo " $ROOT \c"
$ECHO "as root.\n"
$ECHO "Creating filesystem structure..."
cd $ROOT
mkdir -p ./etc/opt/
chmod 555 ./etc/opt
echo ".\c"
mkdir -p ./var/sadm/pkg
chmod 555 ./var/sadm/pkg
echo ".\c"
mkdir -p ./$IPS_HOME
#$CP /etc/opt/SUNWips ./etc/opt
# Rebranding changes
#copyWithLinks /etc/opt/SUNWips
copyWithLinks /etc/opt/SUNWps
echo ".\c"
copyWithLinks /etc/opt/SUNWam
echo ".\c"
#$CP $IPS_HOME ./$IPS_HOME
copyWithLinks $IPS_HOME
echo ".\c"
mkdir -p ./$IDSAME_HOME
#$CP $IDSAME_HOME ./$IDSAME_HOME
copyWithLinks $IDSAME_HOME
echo ".\c"
#$CP /var/sadm/pkg/SUNWipgw ./var/sadm/pkg
#copyWithLinks /var/sadm/pkg/SUNWipgw
# According to Bala , this may not be necessary and I agree with him - Mridul
#copyWithLinks /var/sadm/pkg/SUNWpsgw
echo ".\c"

mkdir -p ./var/opt 2>&1
chmod 555 ./var/opt
#$CP /var/opt/SUNWips ./var/opt
# Rebranding changes
#copyWithLinks /var/opt/SUNWips
copyWithLinks /var/opt/SUNWps
echo ".\c"

   
# cleaning up some unwanted stuff
rm -rf $ROOT/$IPS_HOME/bin/chroot
echo ".\c"

$ECHO "done\n"
$ECHO "mkchroot successfully done.\n"

exit 0

fi
