#!/bin/bash
INSTALL_ROOT="$(readlink -f "$1")"
d="$(readlink -f "${0%/*}")"
cd "$d" || exit

. ./var/lib/sorcery/modules/liboscompat

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

# 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
mkdir -p $(dirname $SORCERY_INSTALL_LOG)
echo /etc/sorcery/install.log > $SORCERY_INSTALL_LOG


echo "Installing scripts and libraries"
# Install the executables
install__find_non_volatile etc usr/sbin var/lib |
while read src; do
  dst="$INSTALL_ROOT/$src" &&
  install -o 0 -g 0 -m 755 -p -D "$src" "$dst" &&
  echo "$dst"
done >> "$SORCERY_INSTALL_LOG"

# Install the man pages
echo "Installing man pages"
install__find_non_volatile usr/share/man |
while read doc; do
  dst="$INSTALL_ROOT/$doc.gz" &&
  >"$dst" &&
  chown 0:0 "$dst" &&
  chmod 644 "$dst" &&
  gzip -9 < "$doc" > "$dst" &&
  echo "$dst"
done >> "$SORCERY_INSTALL_LOG"

# Install the GNU General Public License
mkdir -p  ${INSTALL_ROOT}/usr/share/doc
install -o root -g root -m 644 -p -D "COPYING"   "${INSTALL_ROOT}/usr/share/doc/sorcery/COPYING"
echo /usr/share/doc/sorcery/COPYING >> $SORCERY_INSTALL_LOG

# Install the ChangeLog
install -o root -g root -m 644 -p -D "ChangeLog"   "${INSTALL_ROOT}/usr/share/doc/sorcery/ChangeLog"
echo /usr/share/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
# optionally put in HOST style compilers
ARCHITECTURE=$(grep ARCHITECTURE /etc/sorcery/local/config | sed 's/[^-]*-\(.*\)}.*/\1/')
HOST_COMPILE=""
if [[ $ARCHITECTURE ]]
then
  archspec=$(find /usr/share/archspecs -type f -name $ARCHITECTURE)
  if [[ -f $archspec ]]
  then
    HOST=$(grep HOST $archspec | sed 's/.*"\(.*\)".*/\1/')
    for COMPILER in "c++" "g++" "gcc"
    do
      HOST_COMPILE="$HOST_COMPILE $HOST-$COMPILER"
    done
  fi
fi
for COMPILER in "cc" "gcc" "c++" "g++" $HOST_COMPILE; do
  ln -sf "run_compiler" "$COMPILER"
  echo $BUILD_DIR/$COMPILER >> $SORCERY_INSTALL_LOG
done
popd &>/dev/null

for DIR in  "${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"; do
  if ! [[ -d $DIR ]]; then
    if [[ -e $DIR ]]; then
      echo "$DIR is not a directory, but already exists!"
      echo "Please correct this and retry!"
      exit 1
    fi
    mkdir -p "$DIR"
  fi
done

# 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

# Create the hooks dir since hooks try to check that the files are properly owned
mkdir -p "${INSTALL_ROOT}/etc/sorcery/hooks"
chown 0:0 "${INSTALL_ROOT}/etc/sorcery/hooks"
chmod 0755 "${INSTALL_ROOT}/etc/sorcery/hooks"


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