#!/bin/sh
HD=$HOME/hasher
NEEDS="vim-console less rpm-utils patchutils zsh"
COPY=""
RCS=".vim* .zprofile .zsh_aliases .zshenv .zsh_bind .zshrc .dirc* bin/gnuls bin/undeb .terminfo"
NEW=""
VERB=1
SH=1
SOPT=""
INIT=""
I3=""

add_to_file() { # filename content
  mkdir -p $HD/chroot/.in/rcs
  cp "$1" $HD/chroot/.in/rcs/"$1"
  echo "$2" >>  $HD/chroot/.in/rcs/"$1"
}

rm -rf $HD/chroot/.in/rcs

while getopts IXxbs3d:i:c:r:v opt
do
  SH=""
  case $opt in
  3)	I3="i386" ;;
  s)	SH=1 ;;
  v)	VERB=$(($VERB+1)) ; test "$VERB" -ne 3 || set -x ;;
  b)	COPY=""; NEEDS=""; RCS="" ;;
  x)	NEEDS="$NEEDS xauth"; SHOPT="$SHOPT -Y" ;;
  X)	NEEDS="$NEEDS xorg-x11-devel"; SHOPT="$SHOPT -Y" ;;
  I)	INIT=1 ;;
  i)	NEEDS="$NEEDS $OPTARG" ;;
  r)	RCS="$RCS $OPTARG" ;;
  c)	COPY="$COPY $OPTARG" ;;
  d)	HD="$OPTARG" ;;
  esac
done
shift `expr $OPTIND - 1`

test -z "$*" || COPY="$COPY $*"

test -z "$INIT" || $I3 hsh "$HD" --initroot-only

test -z "$NEEDS" || $I3 hsh-install $HD $NEEDS

test -z "$RCS" || (
cd $HOME
tar cf $HD/chroot/.in/rcs.tar `eval ls -d $RCS || :`
test -d "$HD/chroot/.in/rcs" &&
(
  cd "$HD/chroot/.in/rcs"
  tar rmf $HD/chroot/.in/rcs.tar . --exclude=.
) || :
)
mkdir -p $HD/chroot/.in/pass
$I3 hsh-run $HD -- tar -x -C /usr/src -f /.in/rcs.tar
test -z "$COPY" || cp -a $COPY $HD/chroot/.in/pass/
$I3 hsh-run $HD -- find /.in/pass -maxdepth 1 -type f -exec cp {} /usr/src/RPM/SOURCES \;
test -z "$SH" || {
test -x "$HD/chroot/bin/zsh" && SHOPT="$SHOPT --shell=/bin/zsh" || :
$I3 hsh-shell $HD $SHOPT
}
