#!/bin/bash

#
# 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 '*~' -and -not -path '*.svn*' -and -not -path '*.arch-id*'
}
function install__find_for_log() {
  find "$@" -not -path '*CVS*' -and -not -name '.#*' -and -not -name '*~' -and -not -path '*.svn*' -and -not -path '*.arch-id*'|
  while read line; do echo /$line; done
}

if [[ $1 ]] ; then
  INSTALL_ROOT=$1
else
  INSTALL_ROOT=""
fi
# First clean out all libraries.
echo "Cleaning out all old sorcery libraries..."
SORCERY_LIBS=${INSTALL_ROOT}/var/lib/sorcery/modules
rm -rf $SORCERY_LIBS

SORCERY_INSTALL_LOG=$INSTALL_ROOT/etc/sorcery/install.log
rm -f $SORCERY_INSTALL_LOG
echo /etc/sorcery/install.log > $SORCERY_INSTALL_LOG


echo "Installing scripts and libraries"
# Install the executables
install__find_for_log etc usr/sbin var/lib >> $SORCERY_INSTALL_LOG
install__find_non_volatile etc usr/sbin var/lib | (
  while read SCRIPT; do
    /usr/bin/install --owner=root --group=root --mode=755 -pD "${SCRIPT}" "${INSTALL_ROOT}/${SCRIPT}"
  done
)

# Install the man pages
echo "Installing man pages"
install__find_for_log usr/share/man |while read line; do
echo ${line}.gz
done >> $SORCERY_INSTALL_LOG
install__find_non_volatile usr/share/man | (
  while read DOC; do
    /usr/bin/install --owner=root --group=root --mode=644 -pD "${DOC}"    "${INSTALL_ROOT}/${DOC}"
    gzip  --best  -f "${INSTALL_ROOT}/${DOC}"
  done
)

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

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

# Install the run_compiler script with the same names as gcc
echo "Setting up build symlinks"
BUILD_DIR=${INSTALL_ROOT}/var/lib/sorcery/build
pushd $BUILD_DIR &> /dev/null || exit 1
for COMPILER in "cc" "gcc" "c++" "g++"; do
  ln -sf "run_compiler" "$COMPILER"
  echo $BUILD_DIR/$COMPILER >> $SORCERY_INSTALL_LOG
done
popd &>/dev/null

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/log/sorcery/activity"          

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


#---------------------------------------------------------------------
##=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
##
#---------------------------------------------------------------------
