#!/bin/bash
#--------------------------------------------------------------------
## Library for doing install_root stuff. It is home to menu's for
## install_root and related variables. Future install_root specific
## and cross install related code might live here.
## @Copyright Copyright (C) 2004 The Source Mage Team 
## @Note The description may be wrong.
#--------------------------------------------------------------------

#---------------------------------------------------------------------
##
## 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
## This expects the user to have the sorcery tarball already downloaded
## and in $SOURCE_CACHE.
#------------------------------
function replicate_sorcery() {
  SOURCE=$SOURCE_CACHE/sorcery-$SORCERY_BRANCH.tar.bz2
  SOURCE_DIRECTORY=$BUILD_DIRECTORY/sorcery

  if ! test -f $SOURCE ; then
    message "Cant find source in $SOURCE, please update or download sorcery"
    return 1
  fi

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

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


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