#!/bin/bash

SORCERY_VERSION=`/usr/sbin/gaze installed sorcery`

if [ ! -x /usr/sbin/lockexec ] ; then
    cat <<EOS 1>&2

WARNING: You don't appear to have lockexec installed. I'm going to create a
         filthy hack to keep this process going, but you need to cast
	 lockexec asap.

EOS
    echo '#!/bin/sh
cat << EOF 1>&2

This is a filthy hack used to smooth a difficult upgrade process and
it is not the real lockexec. You should cast lockexec as soon as
possible.

EOF
lockfile=$1
touch $lockfile
shift
"$@"
rm $lockfile
' > /usr/sbin/lockexec && chmod 755 /usr/sbin/lockexec

fi

#
# For unknown reasons `$filter=...; find . $filter` doesn't work, so this function
# encapsulates the filters necessary to strip CVS files and EMACS temporary files
function install__find_non_volatile() {
  find $@ -not -type d -and -not -path '*CVS*' -and -not -name '.#*' -and -not -name '*~'
}

# Install the executables
install__find_non_volatile etc usr/sbin var/lib | (
  while read SCRIPT; do
    /usr/bin/install --owner=root --group=root --mode=755 -vpD "${SCRIPT}" "${INSTALL_ROOT}/${SCRIPT}"
  done
)

# Install the man pages
install__find_non_volatile usr/share/man | (
  while read DOC; do
    /usr/bin/install --owner=root --group=root --mode=644 -vpD "${DOC}"    "${INSTALL_ROOT}/${DOC}"
    gzip  --best  -vf "${INSTALL_ROOT}/${DOC}"
  done
)

# Install the GNU General Public License
  /usr/bin/install --owner=root --group=root --mode=644 -vpD "COPYING"   "${INSTALL_ROOT}/usr/doc/sorcery/COPYING"

# Install the ChangeLog
  /usr/bin/install --owner=root --group=root --mode=644 -vpD "ChangeLog"   "${INSTALL_ROOT}/usr/doc/sorcery/ChangeLog"

# Install the run_compiler script with the same names as gcc
BUILD_DIR=${INSTALL_ROOT}/var/lib/sorcery/build
for COMPILER in "cc" "gcc" "c++" "g++"; do
  ln -svf $BUILD_DIR/run_compiler $BUILD_DIR/$COMPILER
done

mkdir -p                                   \
  ${INSTALL_ROOT}/usr/src                  \
  ${INSTALL_ROOT}/var/cache/sorcery        \
  ${INSTALL_ROOT}/var/spool/sorcery        \
  ${INSTALL_ROOT}/var/log/sorcery/compile  \
  ${INSTALL_ROOT}/var/log/sorcery/install  \
  ${INSTALL_ROOT}/var/log/sorcery/md5sum   \
  ${INSTALL_ROOT}/var/log/sorcery/queue    \
  ${INSTALL_ROOT}/var/state/sorcery

# Create other files which should exist, but aren't installed by sorcery
for FILE in                                         \
  ${INSTALL_ROOT}/etc/sorcery/local/config          \
  ${INSTALL_ROOT}/etc/sorcery/local/compile_config  \
  ${INSTALL_ROOT}/var/state/sorcery/packages        \
  ${INSTALL_ROOT}/var/state/sorcery/depends         \
  ${INSTALL_ROOT}/var/state/sorcery/depends.backup  \
  ${INSTALL_ROOT}/var/log/sorcery/activity          

  do
  if  ! [[  -x $FILE  ]]; then
    touch $FILE
    chmod -v +x $FILE
  fi
done

# Convert an old-style grimoire into a scribe managed one.
if ! [ -d ${INSTALL_ROOT}/var/lib/sorcery/codex ]; then
 source ./convert
fi


#---------------------------------------------------------------------
##=back
##
##=head1 LICENSE
##
## This software is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This software is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this software; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
#---------------------------------------------------------------------
