#!/bin/sh

umask 022
PATH=/usr/bin:/sbin:/usr/sbin

# Create chroot'd area under Solaris 2.5.1 for postfix.
#
# Dug Song <dugsong@UMICH.EDU>

if [ $# -ne 1 ]; then
  echo "Usage: `basename $0` <directory>, e.g.: /var/spool/postfix" ; exit 1
fi

CHROOT=$1
  
# If CHROOT does not exist but parent does, create CHROOT
if [ ! -d ${CHROOT} ]; then
  # lack of -p below is intentional
  mkdir ${CHROOT}
fi
if [ ! -d ${CHROOT} -o "${CHROOT}" = "/" -o "${CHROOT}" = "/usr" ]; then
  echo "$0: bad chroot directory ${CHROOT}"
  exit 2
fi
for dir in etc/default etc/inet dev usr/lib usr/share/lib/zoneinfo ; do
  if [ ! -d ${CHROOT}/${dir} ]; then mkdir -p ${CHROOT}/${dir} ; fi
done
#chmod -R 755 ${CHROOT}

# AFS support.
if [ "`echo $CHROOT | cut -c1-4`" = "/afs" ]; then
  echo '\tCreating memory resident /dev...'
  mount -F tmpfs -o size=10 swap ${CHROOT}/dev
fi

# Setup /etc files.
cp /etc/nsswitch.conf ${CHROOT}/etc
cp /etc/netconfig /etc/resolv.conf ${CHROOT}/etc
cp /etc/default/init ${CHROOT}/etc/default
cp /etc/inet/services ${CHROOT}/etc/inet/services
ln -s /etc/inet/services ${CHROOT}/etc/services
find ${CHROOT}/etc -type f -exec chmod 444 {} \;

# Most of the following are needed for basic operation, except
# for libnsl.so, nss_nis.so, libsocket.so, and straddr.so which are
# needed to resolve NIS names.
cp /usr/lib/ld.so /usr/lib/ld.so.1 ${CHROOT}/usr/lib
for lib in libc libdl libintl libmp libnsl libsocket libw \
    nss_nis nss_nisplus nss_dns nss_files; do
  cp /usr/lib/${lib}.so.1 ${CHROOT}/usr/lib
  rm -f ${CHROOT}/usr/lib/${lib}.so
  ln -s ./${lib}.so.1 ${CHROOT}/usr/lib/${lib}.so
done
cp /usr/lib/straddr.so.2 ${CHROOT}/usr/lib
rm -f ${CHROOT}/usr/lib/straddr.so
ln -s ./straddr.so.2 ${CHROOT}/usr/lib/straddr.so
chmod 555 ${CHROOT}/usr/lib/*

# Copy timezone database.
(cd ${CHROOT}/usr/share/lib/zoneinfo
  (cd /usr/share/lib/zoneinfo; find . -print | cpio -o) | cpio -imdu
  find . -print | xargs chmod 555
)

# Make device nodes. We need ticotsord, ticlts and udp to resolve NIS names.
for device in zero tcp udp ticotsord ticlts; do
  line=`ls -lL /dev/${device} | sed -e 's/,//'`
  major=`echo $line | awk '{print $5}'`
  minor=`echo $line | awk '{print $6}'`
  rm -f ${CHROOT}/dev/${device}
  mknod ${CHROOT}/dev/${device} c ${major} ${minor}
done
chmod 666 ${CHROOT}/dev/*

exit 0
