#!/bin/sh

# Author : Benoit PAPILLAULT <benoit.papillault@free.fr>
# Creation : 2004/09/22

# Install sorcery stable + stable grimoire in $IMAGE

# we use $IMAGE if defined, or else work/image-stage3
if [ -z "${IMAGE}" ]; then
    IMAGE="work/image-stage3"
fi
echo "using IMAGE=${IMAGE}"

# install sorcery
rm -rf "${IMAGE}/root/tmp" &&
mkdir -p "${IMAGE}/root/tmp" &&
tar jxf sorcery-stable.tar.bz2 -C "${IMAGE}/root/tmp" &&
echo "installing sorcery" &&
mkdir -p "${IMAGE}/dev" &&
mount --bind /dev "${IMAGE}/dev" &&
chroot "${IMAGE}" sh -c "cd /root/tmp/sorcery; ./install" > /dev/null &&
umount "${IMAGE}/dev" &&
rm -rf "${IMAGE}/root/tmp" &&

# install stable grimoire

mkdir -p "${IMAGE}/root/tmp" &&
rm -rf "${IMAGE}/root/tmp/stable.tar.bz2" &&
cp stable.tar.bz2 "${IMAGE}/root/tmp" &&
if [ -d "${IMAGE}/var/lib/sorcery/codex/stable" ]; then
  chroot "${IMAGE}" scribe update stable from file:/root/tmp/stable.tar.bz2
else
  chroot "${IMAGE}" scribe add stable from file:/root/tmp/stable.tar.bz2
fi &&
rm -rf "${IMAGE}/root/tmp/stable.tar.bz2" &&

echo "FROM_URL=http://codex.sourcemage.org/stable.tar.bz2" > \
  "${IMAGE}/var/lib/sorcery/codex/stable/GRIMOIRE"
