#!/bin/bash -p
[ $# -ge 2 ] || { echo "$0 requires at least 2 args" 1>&2;exit 1; }
#set -x
CHROOT=$1
SLK_VERSION=$(cat /etc/slackware-version|cut -d' ' -f2)
[ $SLK_VERSION = 14.2 ] && SOLIBS=glibc-solibs || SOLIBS=aaa_glibc-solibs
[ -n "$MKCHROOT_IGNORE_LN_ERRORS" ] && LNE='2>/dev/null' || LNE=
unset T      # Temp file list
U=/dev/null  # Temp install script
i=$(uname -m)
case $i in
x86_64)
  ARCH="$i|x86"
  ;;
i686)
  ARCH="$i|i586|i486|x86"
  ;;
i586)
  ARCH="$i|i486|x86"
  ;;
i486)
  ARCH="$i|x86"
  ;;
*)
  ARCH="$i"
  ;;
esac

while [ $# -gt 1 ]
do
  shift
  PKG=$1
  P=$(ls /var/log/packages/$PKG* |
    grep -E "$PKG-[rv]{0,1}[0-9][^-]*-([^-]*-){0,1}($ARCH|noarch)")
  [ -f "$P" ] || { echo "$PKG finds only $P" >&2; exit 1; }

  # Get (formerly) doinst.sh name
  S=$(echo $P | sed 's+/packages/+/scripts/+')

  # The only reason to install man pages is to keep doinst.sh happy.
  # So don't install them if doinst.sh is unavailable
  [ -r $S ] && j= || j='|man'

  [ -z "$VERBOSE_ADDCHROOT" ] || echo "Adding $PKG" >&2

  # Check for specials
  if [ \
    $PKG = glibc \
    -o $PKG = $SOLIBS \
    -o $PKG = bash \
    -o $PKG = devs \
    -o $PKG = etc \
    -o $PKG = network-scripts \
    -o $PKG = sysvinit \
    ]
  then
    R=$S
    S=/dev/null
    T=$(tempfile -s .mkchroot)
    if [ "$PKG" = bash ]
    then
      # Install bash manually and only use the main script to do locale stuff
      mkdir -p $CHROOT/bin
      (cd $CHROOT/bin
        eval ln -f /bin/bash $LNE \|\| cp -a /bin/bash .
        ln -sf bash sh)
      echo "FILE LIST" >$T
      echo >>$T
      cat $P|grep -E ^usr/share >>$T
      echo usr/bin/bash >>$T   # symlink
    fi
    if [ $PKG = glibc -o $PKG = $SOLIBS ]
    then
      # Remove incoming/ prefix in file list
      cat $P | sed -e 's+/incoming/+/+' >$T
    fi
    if [ "$PKG" = devs ]
    then
      # Only install those entries that exist in /dev, and do it here
      # (because /dev is always mounted, so hard linking fails)
      cat $P | grep -E ^dev/ |
        while read i
        do
          if [ -e /$i ]
          then
            if [ -d /$i ]
            then
              mkdir -p $CHROOT/$i
            else
              cp -a /$i $CHROOT/$i
            fi
          fi
        done
      rm $T
      unset T
      continue
    fi
    if [ "$PKG" = etc -o "$PKG" = network-scripts ]
    then
      # Don't want to run doinst.sh
      cat $P > $T
    fi
    if [ "$PKG" = sysvinit ]
    then
      # Only want to make the symlinks in doinst.sh
      cat $P > $T
      U=$(tempfile -s .mkchroot)
      cat $R|grep -E '^[(]' > $U
    fi
    P=$T
  fi

  tail -n +$(($(grep -n "^FILE LIST" $P | cut -f1 -d:) + 2)) $P |
    grep -E -v "^(install|usr/(doc|info$j))" | sed -e 's/[.]new$//' |
    while read i
  do
    if [ -d /$i -a ! -L /$i ]
    then
      mkdir -p $CHROOT/$i
    else
      eval ln -f /$i $CHROOT/$i $LNE \|\| cp -a /$i $CHROOT/$i
    fi
  done

  # Apply doinst.sh if possible
  if [ -s $S ]
  then
    mkdir -p $CHROOT/install
    cat $S | grep -E -v -e '^[[:space:]]*config [^()]' -e usr/doc |
      sed -e s/[.]new// >$CHROOT/install/doinst.sh
    [ $SLK_VERSION = 14.2 ] ||
    {
      # util-linux ends up with an empty if (thanks to PAM)
      if [ $PKG = util-linux ]
      then
        sed -i '/^for configfile in/,/^$/{D}' $CHROOT/install/doinst.sh
        sed -i '/^if \[ -r etc/,/^fi$/{D}' $CHROOT/install/doinst.sh
      fi
    }
    (cd $CHROOT;sh install/doinst.sh)
    rm -r  $CHROOT/install
  elif [ -s $U ]
  then
    (cd $CHROOT;sh $U)
    rm $U
    U=/dev/null
  fi

  # Remove temporary file if one created
  [ -z "$T" ] || { rm -f $T; unset T; }
done
