#!/bin/bash

# requires: /etc/sorcery/local/depends/linux - file to specify linux version to build in the system

function setup_target() {
  in_start
}

function install_screen() {
  local depcy
  while read depcy ;do
    need_dependency ${depcy%%\#*} ||
    {
      echo "Not all dependencies fulfilled, refusing to finish install"
      return 1
    }
  done <$DATA_DIR/deps-required

  # Check for optional dependencies and run ${name}_auto if needed
  while read depcy ;do
    depcy=${depcy%%\#*} # Strips trailing spaces
    check_dependency "$depcy"               ||
    ! declare -F "${depcy}_auto" >/dev/null || # Skip if function is undefined
    ${depcy}_auto
  done <$DATA_DIR/deps-optional

  in_finish
}

# This one returns a nice list of libraries that a binary is linked against,
# perfect for feeding into cp. Thanks to Seth for this one!
in_libscan() {
  ldd "$1" 2> /dev/null |
   cut -d'>' -f2 |
   cut -d' ' -f2 |
   grep '^/'
   echo /lib/ld-2.* # change this on glibc major version bumps.
   # included so we always have something for cp to copy
}

in_install_prog() {
  local prog progpath
  for prog in $* ;do
    progpath=$( which $prog )         &&
    cp --parents -f $progpath $TARGET &&
    cp --parents -f $( in_libscan $progpath ) $TARGET
  done
}

in_start() {

  echo "Starting installation of smgl to disk"
  mkdir -p ${TARGET} #Though, when this one doesn't exist, we're in trouble

  mkdir -p $TARGET/var/state/sorcery
  >$TARGET/var/state/sorcery/packages #start with no spells installed

  # Set up directories
  si_install_spell smgl-fhs

  local dirctry perms owner group
  while read dirctry perms owner group ;do
    if [[ "${dirctry:0:1}" != "#" ]] ;then #ignore comment lines
      install -d -o ${owner:-root} -g ${group:-root} -m ${perms:-0755} \
        ${TARGET}/$dirctry
    fi
  done <${DATA_DIR}/directories

  local symtarget
  while read dirctry symtarget ;do
    if [[ "${dirctry:0:1}" != "#" ]] ;then #ignore comment lines
      ln -sf $dirctry ${TARGET}/$symtarget
    fi
  done <${DATA_DIR}/symlinks

  # do /proc and /dev - lazy
  try_run mount --rbind /dev ${TARGET}/dev
  try_run mount -t proc proc $TARGET/proc

  # Install sorcery
  pushd /tmp >/dev/null                                    &&
  tar -xjf /var/cache/sorcery/sorcery-$SORCERY_VERSION.tar.bz2 &&
  cd sorcery                                               &&
  try_run -e "installing sorcery" ./install ${TARGET}      &&
  popd >/dev/null                                          &&
  rm -rf /tmp/sorcery                                      &&

  # Copy sources
  try_run cp --parents -f /var/spool/sorcery/* ${TARGET}   &&

  # Install grimoires
  mkdir -p ${TARGET}/var/lib/sorcery/codex                 &&
  try_run tar -xjf /var/lib/sorcery/codex/$GRIMOIRE_VERSION.tar.bz2 \
    -C ${TARGET}/var/lib/sorcery/codex                     &&
  echo "GRIMOIRE_DIR[0]=/var/lib/sorcery/codex/$GRIMOIRE_VERSION" \
       >${TARGET}/etc/sorcery/local/grimoire
  echo "FROM_URL=http://codex.sourcemage.org/$GRIMOIRE_VERSION.tar.bz2" \
       >$TARGET/var/lib/sorcery/codex/$GRIMOIRE_VERSION/GRIMOIRE
  cut -d: -f2 /var/state/sorcery/packages | tail -n 1 \
       >$TARGET/var/state/sorcery/$GRIMOIRE_VERSION.lastupdate
  # Store date for ChangeLog, we hope the /v/s/s/packages date is reasonably
  #   close to ISO creation date (it currently is equal -Bear, 2005-12-03)

  # Install alien config files
  try_run cp --parents -f \
    /etc/{passwd,group,shadow,ld.so.conf,issue,modules,modules.conf,nsswitch.conf,shells} \
    ${TARGET}
  try_run chmod 0400 $TARGET/etc/shadow
  try_run chown root:root $TARGET/etc/shadow

  # Install binaries that may be needed (for whatever it's worth,
  #  we'll be waiting for the basesystem to be there for most purposes anyway)
  in_install_prog bash ldconfig bzip2 tar

  chroot_cmd ldconfig

  si_queue basesystem
  si_queue_now Iso.Compile_env #make, gcc etc
  si_queue_now Iso.Basics #This one ends up first

  si_queue $( cat $DATA_DIR/install-list | sed 's/#.*$//' |
    grep -v '^[[:blank:]]*$' )

  fill_dependency install_start
}

in_finish() {
  # Wait for instalations to finish
  si_flush
  # Install /etc files
  pushd ${FINALFILES} >/dev/null
  cp --parents -r -f * $TARGET
  popd >/dev/null

  # TODO: install bootloader - here?
  cp /etc/sourcemage_version ${TARGET}/etc/sourcemage-release
  echo "Installed from CD using installer v. $INSTALLER_VERSION on $(date)" \
    >>${TARGET}/etc/sourcemage-release
  ln -sf sourcemage-release ${TARGET}/etc/release
  run_dialog --msgbox "The system and all the configuration you have selected
have been installed. The only thing left to do is to enter
the password for the root account." 0 0
  try_run -e "password for root" chroot_cmd passwd root
  if confirm "It is highly recommended to create and use a regular
(non-root) user account for all work, and only su to root
when neccessary. Do you want to create an user now?" ;then
    /usr/sbin/fancy-useradd.sh $TARGET
  fi
  fill_dependency install_finish
}
