#--------------------------------------------------------------------
## Library for doing cross-install stuff. It is used to cast spells
## into a non-ordinary location, for chroot servers, for example.
## @Copyright Copyright (C) 2004 The Source Mage Team 
## @Note The description may be wrong.
#--------------------------------------------------------------------
#!/bin/bash
#
# Where is the documentation of this lib??? (erics)
#

#---------------------------------------------------------------------
##
## This menu function is called only if the CROSS_INSTALL is turned on,
## it provides the user with a choice of the default INSTALL_ROOT,
## TRACK_ROOT, STATE_ROOT, and someday CODEX_ROOT
##
#---------------------------------------------------------------------
function install_root_menu() {

  debug "libcrossinstall" "cross_install_menu is being built now..."
  local C_HELP="Fill in your own preferred track root location."
  local I_HELP="Fill in your own preferred install root location."
  local S_HELP="Fill in your own preferred state root location."
  local T_HELP="Fill in your own preferred track root location."

  while

    COMMAND=`eval $DIALOG '  --title "Install Root Menu"                     \
                             --item-help                                     \
                             --ok-label      "Select"                        \
                             --cancel-label  "Exit"                          \
                             --menu                                          \
                             ""                                              \
                             0 0 0                                           \
                             "C"  "Set install cache location"     "$C_HELP" \
                             "I"  "Set install root location"      "$I_HELP" \
                             "S"  "Set state root location"        "$S_HELP" \
                             "T"  "Set track root location"        "$T_HELP"'`

  do

    case $COMMAND in
      C)  set_install_cache_menu  ;;
      I)  set_install_root_menu   ;;
      S)  set_state_root_menu     ;;
      T)  set_track_root_menu     ;;
    esac

  done

}

#---------------------------------------------------------------------
##
## This menu function allows the user to fill in an INSTALL_CACHE location.
##
#---------------------------------------------------------------------
function set_install_cache_menu() {

  debug "libcrossinstall" "set_install_cache_menu - is starting..."
  local PROMPT="Please enter the install cache location where cache tarballs will be stored"
  if  NEW_INSTALL_CACHE=`eval $DIALOG '  --ok-label  "Commit"         \
                                    --inputbox                   \
                                    "$PROMPT"                    \
                                    0 0  "$INSTALL_CACHE"'`
  then
    INSTALL_CACHE=${NEW_INSTALL_CACHE%/}
    remove_config $LOCAL_CONFIG "INSTALL_CACHE"
    modify_config  $LOCAL_ROOTS_CONFIG "INSTALL_CACHE"  "$INSTALL_CACHE" 
  fi
}

#---------------------------------------------------------------------
##
## This menu function allows the user to fill in an INSTALL_ROOT location.
##
#---------------------------------------------------------------------
function set_install_root_menu() {

  debug "libcrossinstall" "set_install_root_menu - is starting..."
  local PROMPT="Please enter the install root location where you want file installed"
  if  NEW_INSTALL_ROOT=`eval $DIALOG '  --ok-label  "Commit"         \
                                    --inputbox                   \
                                    "$PROMPT"                    \
                                    0 0  "$INSTALL_ROOT"'`
  then
    INSTALL_ROOT=${NEW_INSTALL_ROOT%/}
    remove_config  $LOCAL_CONFIG "INSTALL_ROOT"
    modify_config  $LOCAL_ROOTS_CONFIG "INSTALL_ROOT"  "$INSTALL_ROOT" 
  fi
}

#---------------------------------------------------------------------
##
## This menu function allows the user to fill in an STATE_ROOT location.
##
#---------------------------------------------------------------------
function set_state_root_menu() {

  debug "libcrossinstall" "set_state_root_menu - is starting..."
  local PROMPT="Please enter location where you want state files to be"
  if  NEW_STATE_ROOT=`eval $DIALOG '  --ok-label  "Commit"         \
                                    --inputbox                 \
                                    "$PROMPT"                  \
                                    0 0  "$STATE_ROOT"'`
  then
    STATE_ROOT=${NEW_STATE_ROOT%/}
    remove_config  $LOCAL_CONFIG "STATE_ROOT"
    modify_config  $LOCAL_ROOTS_CONFIG "STATE_ROOT"  "$STATE_ROOT"
  fi
}

#---------------------------------------------------------------------
##
## This menu function allows the user to fill in an TRACK_ROOT location.
##
#---------------------------------------------------------------------
function set_track_root_menu() {

  debug "libcrossinstall" "set_track_root_menu - is starting..."
  local PROMPT="Please enter the location where you want files tracked relative to"
  if  NEW_TRACK_ROOT=`eval $DIALOG '  --ok-label  "Commit"         \
                                    --inputbox                 \
                                    "$PROMPT"                  \
                                    0 0  "$TRACK_ROOT"'`
  then
    TRACK_ROOT=${NEW_TRACK_ROOT%/}
    modify_config  $LOCAL_ROOTS_CONFIG "TRACK_ROOT"  "$TRACK_ROOT"
  fi
}


#------------------------------
## Will install sorcery in install_root directory, by running
## the install script bundled with the tarball
#------------------------------
function replicate_sorcery() {
  SOURCE=sorcery-$SORCERY_BRANCH.tar.bz2
  SOURCE_DIRECTORY=$INSTALL_ROOT/$BUILD_DIRECTORY/sorcery

  mk_source_dir  $SOURCE_DIRECTORY              &&
  cd $INSTALL_ROOT/usr/src                     &&
  bzip2  -cdf   $FILENAME                       |
  tar --owner=root --group=root -xf /dev/stdin  &&
  cd      $SOURCE_DIRECTORY                     &&
  echo `pwd`                                    &&
  ./install  $INSTALL_ROOT

  if [ $? -eq 0 ] ; then
    rm_source_dir
  else
    if [[  $CLEAN_SOURCE == on ]]; then
      rm_source_dir
    fi
  fi
}

#-------------
## Do we know why this is commented out?
## Do we know why it's still here?
##
## Its commented out because it was a product of the old way
## install_root/cross_install was put together. I left it commented out
## just to be more conservative than killing it out-right. Its also
## basically implemented in a more robust way above.
##
## I suspect I'll remove it in 1.12 since no one has even mentioned the
## idea of installing sorcery itself somewhere else and running it from
## there. (afk 2004-9-30)
#-------------
#cross_install_sorcery()
#{
	#
	#message -n "${MESSAGE_COLOR}Setting up Sorcery for cross-install environment."
	#message -n "${DEFAULT_COLOR}"
	#
	#mkdir -p ${INSTALL_ROOT}/etc/init.d/runlevels/{%DEV,%1,%2,%3,%4,%5,%S}
	#mkdir -p ${INSTALL_ROOT}/etc/{sorcery,sysconfig/network}
	#mkdir -p ${INSTALL_ROOT}/{boot,home,root,dev,lib,bin,sbin,mnt,proc}
	#mkdir -p -m 1777 ${INSTALL_ROOT}/tmp
	#mkdir -p ${INSTALL_ROOT}/var/state/sorcery
	#mkdir -p ${INSTALL_ROOT}/var/cache/sorcery
	#mkdir -p ${INSTALL_ROOT}/usr/{src,sbin,bin,lib}
	#mkdir -p ${INSTALL_ROOT}/usr/share/info
        #ln -sf ./share/info ${INSTALL_ROOT}/usr/info
	#mkdir -p ${INSTALL_ROOT}/usr/share/man/{man1,man2,man3,man4,man5,man6,man7,man8}
        #ln -sf ./share/man ${INSTALL_ROOT}/usr/man
  #mkdir -p ${INSTALL_ROOT}/var/lock
  #mkdir -p ${INSTALL_ROOT}/var/run
	#mkdir -p ${INSTALL_ROOT}/var/log/sorcery/compile/
	#mkdir -p ${INSTALL_ROOT}/var/log/sorcery/install/
	#mkdir -p ${INSTALL_ROOT}/var/log/sorcery/md5sum/
	#mkdir -p ${INSTALL_ROOT}/var/log/sorcery/queue/
	#mkdir -p ${INSTALL_ROOT}/var/lib/sorcery/codex/
	#mkdir -p ${INSTALL_ROOT}/var/spool/{mail,sorcery}
	#mkdir -p -m 1777 ${INSTALL_ROOT}/var/tmp
	#cp -ruv /etc/sorcery/{accounts,config,groups,mirrors,version,cabal,dialogrc,licenses,uid,compile_config,gid,update} ${INSTALL_ROOT}/etc/sorcery
        #mkdir -p ${INSTALL_ROOT}/etc/sorcery/local/depends
        #cp -f /etc/sorcery/local/depends/README ${INSTALL_ROOT}/etc/sorcery/local/depends
        #cp /etc/sysconfig/init ${INSTALL_ROOT}/etc/sysconfig
        #touch ${INSTALL_ROOT}/var/state/sorcery/{depends,packages,triggers}
        #cp -ruv /var/lib/sorcery/{archspecs,excluded,protected,subroutines,build,modules,solo,sustained} ${INSTALL_ROOT}/var/lib/sorcery/
        #cp -p /usr/sbin/{cast,dispel,gaze,invoke,scribbler,scribe,sorcery,summon,vcast,xsorcery} ${INSTALL_ROOT}/usr/sbin
        #cp -p /usr/share/man/man8/{cast.8*,dispel.8*,invoke.8*,scribbler.8*,scribe.8*,sorcery.8*,summon.8*} ${INSTALL_ROOT}/usr/share/man/man8
        #cp -p /usr/share/man/man1/gaze.1* ${INSTALL_ROOT}/usr/share/man/man1/
        #cp -p /usr/share/man/man5/grimoire.5* ${INSTALL_ROOT}/usr/share/man/man5/
#
#}

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