#!/bin/bash
#
# Copyright (C) 2002-2003 by NCHC, Steven Shiau, K. L. Huang
# (steven _at_ nchc org tw, klhaung _at_ gmail com)
#
# This program 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, or (at your option)
# any later version.
#
# This program 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 program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

# Load DRBL setting and functions
# Setting
# Source function library.
[ -f /etc/rc.d/init.d/functions ] && . /etc/rc.d/init.d/functions

# Source DRBL setting
. /opt/drbl/conf/drbl.conf

# Source the drbl related functions
. $DRBL_SCRIPT_PATH/sbin/drbl-functions

# functions
check_if_repository_in_blacklist() {
  # yum_repo_blacklists is read from drbl.conf
  local url_tmp="$1"
  for iu in $url_tmp; do
    # If the url is in blacklist (like: http://ftp.isu.edu.tw), some problems! Terminated!
    if [ -n "$(echo "$yum_repo_blacklists" | grep -E "\<$iu")" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      if [ -n "$(echo "$iu" | grep -E "http://ftp.isu.edu.tw")" ]; then 
       # For some known site, output the reason
       echo "Since http://ftp.isu.edu.tw uses page by page output, it it NOT compatable with DRBL scripts."
      fi
      echo "The repository $iu does NOT work with DRBL setup, please modify the yum config file in /etc/yum.repos.d/ to avoid use $iu. If you use mirrorlist, change to use baseurl and assign other repository instead of $iu!."
      echo "Program terminated!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      exit 1
    fi
  done
}
setup_RH_yum() {
# 1.
# OS AYO repository
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_os_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_os_ayo_repository_0"
echo "$msg_os_ayo_repository_download_fedora_redhat_com"
echo "$msg_os_ayo_repository_use_existing_yum_config"
echo "$msg_os_ayo_repository_enter_by_yourself"
echo -n "[0] "
read os_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_os_yum_repo_answer $OS_Version $os_ayo_repo_answer 

# drbl AYO repository
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_drbl_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_drbl_ayo_repository_0"
echo "$msg_drbl_ayo_repository_1"
echo "$msg_drbl_ayo_repository_2"
echo "$msg_drbl_ayo_repository_enter_by_yourself"
echo -n "[0] "
read drbl_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_drbl_yum_repo_answer $drbl_ayo_repo_answer

# ask some RPM and  apt repository options, such as drbl-testing, drbl-unstable
ask_and_parse_rpm_ayo_repo_options

generate_fedora_yum_conf

generate_drbl_yum_conf

} # end of setup_RH_yum

setup_RH_apt() {
# 1.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_os_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_os_ayo_repository_0"
echo "$msg_os_ayo_repository_1"
#echo "$msg_ayo_repository_2"
echo "$msg_os_ayo_repository_3"
#echo "$msg_ayo_repository_4"
echo "$msg_os_ayo_repository_5"
echo -n "[0] "
read os_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_os_apt_repo_answer $OS_Version $os_ayo_repo_answer 

#
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_drbl_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_drbl_ayo_repository_0"
echo "$msg_drbl_ayo_repository_1"
echo "$msg_drbl_ayo_repository_2"
echo "$msg_drbl_ayo_repository_3"
#echo "$msg_ayo_repository_4"
echo "$msg_drbl_ayo_repository_5"
echo -n "[0] "
read drbl_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_RH_drbl_apt_repo_answer $drbl_ayo_repo_answer

# ask some RPM and  apt repository options, such as drbl-testing, drbl-unstable
ask_and_parse_rpm_ayo_repo_options

# install wget if not installed, which is always necessary
rpm -q wget &>/dev/null
if [ "$?" -gt 0 ]; then 
  echo "$msg_no_wget_get_it"
  WGET_idx=$(echo $OS_Version | tr "\." "_")
  # we will get the variables like wget_rpm_RH8_0, wget_rpm_FC1, wget_rpm_MDK9_2
  eval WGET_RPM=\$wget_rpm_$WGET_idx
  for irpm in $RH_RPMS_os_update_dir; do
    echo "Try to install wget from $url_os_coreroot/$irpm/$WGET_RPM..."
    # TODO: make it work with file://...
    rpm -ivh --quiet $url_os_coreroot/$irpm/$WGET_RPM &>/dev/null
    RETVAL=$?
    [ $RETVAL -eq 0 ] && break
  done
  # check if wget installed
  rpm -q wget &>/dev/null
  if [ "$?" -gt 0 ]; then
   echo "$msg_wget_installed_failure"
  else
   echo "$msg_wget_installed_successful"
  fi
fi
# check if apt is installed or not
rpm -q apt &>/dev/null
if [ "$?" -gt 0 ]; then 
   echo "$msg_install_apt"
   # install apt
   echo "$msg_no_apt_get_it"
   case "$ayo_repo_answer" in
   # if local apt repository, do not use url in the network 
   3)
     # local apt repository
     rpm -Uvh $url_drbl_collect_root/RPMS.drbl-collect/apt-[0-9]*.i386.rpm
     RETVAL=$?
     if [ "$RETVAL" -gt 0 ]; then 
        echo "$msg_Failed_to_install $pkg_file..."
        return $RETVAL
     fi
     ;;
   *)
     get_inst_rpm -u $url_drbl_collect_root/RPMS.drbl-collect/ -p apt -a i386
     rc=$?
     check_rpm_install $rc apt
   esac
else
   echo "$msg_delimiter_star_line"
   echo "$msg_apt_already_installed"
   echo "$msg_delimiter_star_line"
fi
# Clean the "Architecture "i[356]86"; "in apt.conf first,
# later we will insert Architecture "i386" to /etc/apt/apt.conf here,
perl -p -i -e "s/Architecture *\"i[356]86\".*;//g" /etc/apt/apt.conf

# backup apt sources.list if it's found.
echo "$msg_delimiter_star_line"
backup_apt_sources_list

# set the apt sources.list
# OS
case "$os_ayo_repo_answer" in
    3) 
      #local harddisk dir
cat <<-APT_END > /etc/apt/sources.list
rpm copy:$url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
#rpm-src copy:$url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
APT_END
    ;;
    *)
cat <<-APT_END > /etc/apt/sources.list
rpm $url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
#rpm-src $url_os/$os_ayo_path . $core_dir updates $FRESHRPMS
APT_END
esac

# DRBL
case "$drbl_ayo_repo_answer" in
    3) 
      #local harddisk dir
cat <<-APT_END >> /etc/apt/sources.list
rpm copy:$url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
#rpm-src copy:$url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
APT_END
    ;;
    *)
cat <<-APT_END >> /etc/apt/sources.list
rpm $url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
rpm $url_drbl_collect_root . drbl-collect
#rpm-src $url_drbl/$drbl_ayo_path . drbl-stable $DRBL_TEST $DRBL_UNSTABLE $CJK_REPOSITORY $DRBL_EXTRA 
#rpm-src $url_drbl_collect_root . drbl-collect
APT_END
esac

# append some examples to sources.list 
append_RH_apt_sources_list_example

echo "--------------------------------------------------------------------"
echo "$msg_enjoy_apt"

} # setup_RH_apt

install_drbl_RH() {
# function to install Redhat/Fedora DRBL

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
[ ! -d "/tftpboot/nbi_img" ] && mkdir -p /tftpboot/nbi_img

# 1.
# setup apt/yum first
# parse os to set the repository
parse_os_to_set_repository

# import RPM key for yum, the default setting of yum enables key check.
[ "$installer" = "yum" ] && import_rpm_key
echo "$msg_delimiter_star_line"

if [ -z "$select_repository" ]; then
  # default setting, maybe overwritten by command option
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_do_you_want_set_yum_repo"
  echo "$msg_set_yum_conf_yes_prompt"
  echo "$msg_set_yum_conf_no_prompt"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read set_repo_ans 
  case "$set_repo_ans" in
     y|Y|[yY][eE][sS])
       select_repository="yes"
       ;;
     *)
       select_repository="no"
       ;;
  esac
fi

# Set the fedora repository.
if [ "$installer" = "yum" ]; then
  # FC1-4: fedora.repo
  # FC5: fedore-core.repo
  if [ -e "/etc/yum.repos.d/fedora-core.repo" ]; then
    FC_CORE_REPO="fedora-core.repo"
  elif [ -e "/etc/yum.repos.d/fedora.repo" ]; then
    FC_CORE_REPO="fedora.repo"
  else
    # default one:
    FC_CORE_REPO="fedora-core.repo"
  fi
fi

if [ "$select_repository" = "yes" ]; then 
  # For RH8/9.0/FC1-2, already set $select_repository=yes, so this will be run
  setup_${OS_type}_${installer}
  install_curl_etc_via_${installer}_if_necessary
else
  # This only for yum
  # put drbl-core yum repos conf
  [ ! -d "/etc/yum.repos.d" ] && mkdir -p /etc/yum.repos.d

  # Check if the repository setting exists
  # TODO: need beter method to check.
  if [ ! -f "/etc/yum.repos.d/$FC_CORE_REPO" -a ! -f "/etc/yum.repos.d/CentOS-Base.repo" ]; then
     echo "$msg_delimiter_star_line"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "No yum repository setting found!" 
     echo "You must set the yum repository in /etc/yum.repos.d/!"
     echo "Program terminated!!!"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     echo "$msg_delimiter_star_line"
     exit 1
  fi

  # backup the old setting
  repo_lists="drbl.repo drbl-testing.repo drbl-unstable.repo drbl-extra.repo"
  for ifile in $repo_lists; do
    [ -f "/etc/yum.repos.d/$ifile" ] && mv -f /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
  done
  cp -f $DRBL_SCRIPT_PATH/setup/yum-repos/drbl*.repo /etc/yum.repos.d/

  # ask some RPM and apt repository options, such as drbl-testing, drbl-unstable
  ask_and_parse_rpm_ayo_repo_options
  # turn on some branches if set
  [ -n "$DRBL_TEST" ] && perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-testing.repo
  [ -n "$DRBL_UNSTABLE" ] && perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-unstable.repo
  # [ -n "$DRBL_EXTRA" ] && perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-extra.repo

  install_curl_etc_via_yum_if_necessary

  if [ -z "$url_os" ]; then
    # Not found, use the default one...
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "The repository in the system is not found, use DRBL default one."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    parse_${OS_type}_os_${installer}_repo_answer $OS_Version default
    parse_${OS_type}_drbl_${installer}_repo_answer default
    echo "Use yum repository in $url_os_coreroot"
    check_if_repository_in_blacklist $url_os
  fi
fi

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# parse the setting of client archi.
parse_client_archi_setting

# It's important to remove the cache of apt, otherwise 
# the RPM::Architecture will not take effect. check this:
# http://distro2.conectiva.com.br/pipermail/apt-rpm/2003-November/002015.html
# ... RPM::Architecture, is what he's looking for. His problem was that the cache was built before he set the architecture. Doing "rm -f /var/cache/apt/*.bin; apt-get update" would make it work.

if [ "$installer" = "apt" ]; then
  echo "$msg_clean_apt_cache"
  for ifile in /var/cache/apt/*.bin; do
     rm -f /var/cache/apt/$ifile
  done
fi

# Update the apt data with repository.
# if it's yum, skip this, later yum will do it. Because yum update will also
# do upgrade...
[ "$installer" = "apt" ] && $inst_prog update

# 2.
# check if apt is integrity... Ex: original MDK 9.2 is buggy...
if [ "$installer" = "apt" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_check_apt_integrity"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  check_apt_integrity
fi

# 3.
# We will upgrade system first.
# Try to find if the i386 glibc and openssl are installed or not
# If they are installed, hold them not to be upgraded to i686
echo "$msg_delimiter_star_line"
if [ -z "$upgrade_system_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_upgrade_system_question"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read upgrade_system_answer
fi

case "$upgrade_system_answer" in 
   y|Y|[yY][eE][sS])
      do_upgrade_system_for_RH
      ;;
esac

# 4.
echo "$msg_delimiter_star_line"
# Force to install all the kernel selected.
echo "$msg_install_kernel_for_clients ..."
echo "$msg_search_kernel_in_ayo kernel${SMP_OPTION} ..."

install_RH_MDK_client_kernel

# 5.
# Install the necessary packages for DRBL 
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_install_DRBL_necesary_files"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"
echo "$msg_install_dhcp_tftp_etc"

# wget maybe not installed, we will check it is installed only when apt is not installed, so if apt/yum is installed while wget is not installed, get_inst_rpm() will failed! Add wget here.
pkgs_for_RH="dhcp tftp-server nfs-utils ypserv ypbind yp-tools mkinitrd ntp firstboot iptables wget mkpxeinitrd-net etherboot partimage freedos clonezilla dialog initscripts rsync parted reiserfs-utils tcpdump bc grub dos2unix ddrescue curl lftp openssh-server openssh-clients coreutils gzip bzip2 nc file ethtool net-tools drbl-ntfsprogs lzop"
pkgs_to_query="lvm2"
echo -n "Searching if $pkgs_to_query available... "
case "$installer" in
  yum)
     for ipkg in $pkgs_to_query; do
       if [ -n "$(yum list $ipkg |grep -E "^$ipkg")" ]; then
         echo -n "Found $ipkg package in repository..."
         pkgs_for_RH="$pkgs_for_RH $ipkg"
       fi
     done
     ;;
  apt)
     for ipkg in $pkgs_to_query; do
       if [ -n "$(apt-cache pkgnames $ipkg)" ]; then
         echo -n "Found $ipkg package in repository..."
         pkgs_for_RH="$pkgs_for_RH $ipkg"
       fi
     done
     ;;
esac
echo " done!"

$inst_prog -y install $pkgs_for_RH
# TODO
RETVAL=$?
check_apt_install $RETVAL

# 6.
# After the dist upgrade, check the ARCH for glibc, openssl
glibc_ARCH=""
openssl_ARCH=""
rpm -q --qf '%{ARCH}' glibc > /dev/null && glibc_ARCH=`rpm -q --qf '%{ARCH}' glibc`
rpm -q --qf '%{ARCH}' openssl > /dev/null && openssl_ARCH=`rpm -q --qf '%{ARCH}' openssl`
rpm -q --qf '%{ARCH}' openssl-perl > /dev/null && openssl_perl_ARCH=`rpm -q --qf '%{ARCH}' openssl-perl`

install_client_archi_glibc_openssl_RH

echo "$msg_delimiter_star_line"
# 7.
# 7.a
# ugly...might be not my fault...
# for some distribution some version, we have to force upgrading some packages.
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_try_to_update_some_pkgs"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
case "$OS_Version" in
  "RH8.0"|"RH9")
        # This one only works for RH8
        $inst_prog -y install redhat-config-xfree86
        ;;
  "FC1")
        # gpm is buggy, can not work with clonezilla, so upgrage to mine
	# in drbl repository
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install gpm redhat-config-xfree86
        ;;
  "FC4")
        # in FC4, sfdisk, rpm and nc are buggy, 
	# sfdisk can not work with clonezilla,
	# https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=159418
	# Original nc is buggy since it will skip some input tail...
	# So upgrage
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install rpm util-linux nc
        ;;
  FC*)
        # FC2 use different name "system-config-display" instead of "redhat-config-xfree86"
        echo "$msg_OS_version: $OS_Version."
	echo "$msg_force_to_upgrade_some_pkgs"
	$inst_prog -y install system-config-display
        ;;
esac

# netinstall
if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_install_net_install_images"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  $inst_prog -y install $net_install_pkgs
  # TODO
  RETVAL=$?
  check_apt_install $RETVAL
fi

# 7.b
# Creat dev tarball in $drbl_pkgdir,
# which will be copied to /tftpboot/node_root/etc/diskless-image 
# in deploy script
tmp_dev_dir="$(mktemp -d -q /tmp/devtmp.XXXXXX)"
dev_wgettmp="$(mktemp -d -q /tmp/dev_wgettmp.XXXXXX)"
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir

echo "$msg_delimiter_star_line"
echo "$msg_creating_dev"

# get dev
echo "$msg_get_dev"
     dev_got=0
     for ipkg in dev MAKEDEV; do
      Is_ipkg_avail=""
      for idir in $RH_RPMS_os_update_dir; do
	Is_ipkg_avail="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_os_coreroot/$idir/ | grep -E "^$ipkg-[0-9]+.*.i386.rpm" 2>/dev/null)"
	if [ -n "$Is_ipkg_avail" ]; then
	  echo "Downloading $ipkg rpm from $url_os_coreroot/$idir/..." 
          wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$dev_wgettmp" -A "$ipkg-[0-9]*.i386.rpm" $url_os_coreroot/${idir}/
        fi
        # once it's done, skip the next search
	if [ "$(ls $dev_wgettmp/$ipkg-[0-9]*.i386.rpm 2> /dev/null)" ]; then
          echo "Got the $ipkg rpm!"
	  dev_got=1
          break
	else
          [ -n "$verbose" ] && echo "\"$ipkg\" is not found in $url_os_coreroot/$idir/."
        fi
      done
      # if we can not download dev rpm, then skip the MAKEDEV download.
      [ "$dev_got" = 0 ] && break
     done
(
  if [ -n "$(ls $dev_wgettmp/dev[-_]*.i386.rpm 2>/dev/null)" -a -n "$(ls $dev_wgettmp/MAKEDEV[-_]*.i386.rpm 2>/dev/null)" ]; then
    cd $tmp_dev_dir
    echo -n "Creating the device files for DRBL client..."
    # To avoid the duplicated rpms... just find the latest one.
    latest_dev="$($DRBL_SCRIPT_PATH/bin/list_latest_rpm $dev_wgettmp/ | grep -E "^dev[-_].*.i386.rpm")"
    latest_MAKEDEV="$($DRBL_SCRIPT_PATH/bin/list_latest_rpm $dev_wgettmp/ | grep -E "^MAKEDEV[-_].*.i386.rpm")"
    rpm2cpio $dev_wgettmp/$latest_dev | cpio -idm
    rpm2cpio $dev_wgettmp/$latest_MAKEDEV | cpio -idm
    cd $tmp_dev_dir/dev && tar czf $drbl_pkgdir/dev.tgz *
    echo "done!"
  else
    echo "dev and MAKEDEV rpm are not found... The DRBL client will use udev..."
    # we must install udev...
    if ! rpm -q --quiet udev; then
      echo -n "Try to install udev..."
      $inst_prog -y install udev
      echo "done!"
    fi
    if ! rpm -q --quiet udev; then
      echo "No udev, and no pre-packaged devices file!!! Program terminated!"
      exit 1
    fi
  fi
)
echo "$msg_clean_tmp_dev"
[ -d "$tmp_dev_dir" ] && rm -rf $tmp_dev_dir
[ -d "$dev_wgettmp" ] && rm -rf $dev_wgettmp

# 8.
# make the nbi/pxe files
create_pxe_nbi_files

# 9.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_create_files_for_PXELINUX"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
create_files_for_PXELINUX

echo "$msg_delimiter_star_line"
echo "$msg_Done!"
} # end of install_drbl_RH

uninstall_drbl_RH() {
# check if apt is installed or not
if ! rpm -q --quiet apt && ! rpm -q --quiet yum; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$msg_no_apt_already_uninstall"
   echo "$msg_program_stop"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
fi

# if apt is not installed, the installer must be yum
if ! rpm -q --quiet apt; then
   installer="yum"
   inst_prog="yum"
fi

# Set the fedora repository.
if [ "$installer" = "yum" ]; then
  # FC1-4: fedora.repo
  # FC5: fedore-core.repo
  if [ -e "/etc/yum.repos.d/fedora-core.repo" ]; then
    FC_CORE_REPO="fedora-core.repo"
  elif [ -e "/etc/yum.repos.d/fedora.repo" ]; then
    FC_CORE_REPO="fedora.repo"
  else
    # default one:
    FC_CORE_REPO="fedora-core.repo"
  fi
fi

# rm the YP securenets
$DRBL_SCRIPT_PATH/sbin/drbl-yp-securenets clean

#
# skip removing dialog, rsync, curl... which are usually necessary.
# TODO, separate ntp, it seems that to remove ntp together will fail in FC3 
# with yum.
$inst_prog remove dhcp tftp-server nfs-utils ypserv ypbind yp-tools etherboot partimage freedos clonezilla dos2unix ddrescue drbl-ntfsprogs
$inst_prog remove ntp

$inst_prog remove $net_install_pkgs

# rename some config files if they still exist
for icfg in dhcpd.conf dhcpd.conf.etherboot.include exports ntp.conf sysconfig/iptables; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done

# remove the stale file
[ -f "/etc/init.d/ocs" ] && rm -f /etc/init.d/ocs

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_setup_answer
case "$drbl_setup_answer" in 
   y|Y|[yY][eE][sS])
      rpm -e drbl
   ;;
esac
if [ "$installer" = "apt" ]; then
  echo "$msg_remove_apt_question"
  echo -n "[y/N] "
  read apt_answer
  case "$apt_answer" in 
     y|Y|[yY][eE][sS])
        rpm -e apt
        [ -d /var/cache/apt/ ] && echo "$msg_cleaning_apt_cache" && rm -rf /var/cache/apt/
     ;;
  esac
elif [ "$installer" = "yum" ]; then
  echo "$msg_remove_yum_repo_question"
  echo -n "[y/N] "
  read yum_repo_answer
  case "$yum_repo_answer" in 
   y|Y|[yY][eE][sS])
     repo_lists="drbl.repo drbl-testing.repo drbl-unstable.repo drbl-extra.repo $FC_CORE_REPO fedora-updates.repo"
     for ifile in $repo_lists; do
       [ -f "/etc/yum.repos.d/$ifile" ] && mv -v /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
     done
  esac
fi

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_RH

#
setup_MDK_urpmi() {
# Setting
echo "$msg_delimiter_star_line"

# 1.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_os_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_os_ayo_repository_0"
echo "$msg_os_ayo_repository_1_mdk"
echo "$msg_os_ayo_repository_2_mdk"
echo "$msg_os_ayo_repository_5"
echo -n "[0] "
read mdk_os_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_MDK_os_ayo_repo_answer $mdk_os_ayo_repo_answer

echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_which_drbl_ayo_repository"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_drbl_ayo_repository_0"
echo "$msg_drbl_ayo_repository_1_mdk"
echo "$msg_drbl_ayo_repository_2_mdk"
echo "$msg_drbl_ayo_repository_5"
echo -n "[0] "
read mdk_drbl_ayo_repo_answer
echo "$msg_delimiter_star_line"
parse_MDK_drbl_ayo_repo_answer $mdk_drbl_ayo_repo_answer

# ask some RPM and  apt repository options, such as drbl-testing, drbl-unstable
ask_and_parse_rpm_ayo_repo_options

# Import the pub GPG keys
for ik in /etc/RPM-GPG-KEYS/*.asc; do
  key_version=$(basename $ik | sed -e "s/.asc//g")
  if ! rpm -q gpg-pubkey-$key_version &>/dev/null; then
    echo -n "Importing the public GPG keys from $ik... "
    rpm --import $ik
    echo "done!"
  fi
done

# TODO: find a method to keep the existing OS uprmi, but overwrite the DRBL urpmi
[  -f "/etc/urpmi/urpmi.cfg" ] && cp -v /etc/urpmi/urpmi.cfg /etc/urpmi/urpmi.cfg.drblsave
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "Removing the old urpmi setting..."
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"
urpmi.removemedia -a

echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "Adding urpmi source..."
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"
# OS
urpmi.addmedia MDK${MDK_VER_PATH}-core $url_os/$os_ayo_path/$MDK_VER_PATH/i586/$media/$main_rpmdir
urpmi.addmedia MDK${MDK_VER_PATH}-contrib $url_os/$os_ayo_path/$MDK_VER_PATH/i586/$media/$contrib_rpmdir $contrib_opt
urpmi.addmedia MDK${MDK_VER_PATH}-jpackage $url_os/$os_ayo_path/$MDK_VER_PATH/i586/$media/$jpackage_rpmdir $jpackage_opt
urpmi.addmedia MDK${MDK_VER_PATH}-updates $url_os/$os_ayo_path/updates/${update_extra_code_name}$MDK_VER_PATH/$update_rpmdir

# DRBL
urpmi.addmedia drbl-stable $url_drbl/$drbl_ayo_path/base with synthesis.hdlist.drbl.cz
if [ -n "$DRBL_TEST" ]; then
  urpmi.addmedia drbl-testing $url_drbl/$drbl_ayo_path/base with synthesis.hdlist.drbl-testing.cz
fi
if [ -n "$DRBL_UNSTABLE" ]; then
  urpmi.addmedia drbl-unstable $url_drbl/$drbl_ayo_path/base with synthesis.hdlist.drbl-unstable.cz
fi

echo "--------------------------------------------------------------------"
echo "The urpmi setting is finished."

} # setup_MDK_urpmi

install_drbl_MDK() {
# function to install MDK DRBL

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ ${NETWORKING} = "no" ] && exit 0

# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
[ ! -d "/tftpboot/nbi_img" ] && mkdir -p /tftpboot/nbi_img

# 1.
# setup urpmi first
# parse os to set the repository
parse_os_to_set_repository

if [ "$select_repository" = "yes" ]; then 
  setup_${OS_type}_${installer}
else
  # we need $url_os, "$os_ayo_path"and "$url_os_coreroot for kernel and dev
  parse_${OS_type}_os_ayo_repo_answer default $OS_Version
  parse_${OS_type}_drbl_ayo_repo_answer default
  echo "Use urpmi repository in $url_os_coreroot"
fi

# Now only for urpmi only, no more apt in MDK
inst_prog="urpmi"
inst_opt="--auto --force"
inst_action=""

# we need curl to parse url to get kernel.
# we need lftp to parse the yum repo config if the repo is in ftp://
$inst_prog $inst_opt $inst_action lftp curl

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# for MDK, although it's only i586, but for mdk10.1/10.2, i686 is necessary for
# SMP. MDK does NOT provide i586-smp kernel
parse_client_archi_setting

# 2.
# We will upgrade system first.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_upgrade_system_question"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[y/N] "
read upgrade_system_answer
case "$upgrade_system_answer" in 
  y|Y|[yY][eE][sS])
      do_upgrade_system_for_MDK
      ;;
esac

# 3.
echo "$msg_delimiter_star_line"
# Force to install all the kernel selected.
echo "$msg_install_kernel_for_clients ..."
echo "$msg_search_kernel_in_ayo kernel${SMP_OPTION} ..."

install_RH_MDK_client_kernel

# 4.
# Install the necessary packages for DRBL 
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_delimiter_star_line"
echo "$msg_install_DRBL_necesary_files"
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
pkgs_for_MDK="dhcp-server tftp-server nfs-utils ypserv ypbind yp-tools mkinitrd ntp iptables wget mkpxeinitrd-net etherboot partimage freedos clonezilla cdialog initscripts rsync parted drakxtools pciutils autologin reiserfsprogs tcpdump bc grub dos2unix ddrescue curl lftp openssh-server openssh-clients coreutils gzip bzip2 nc file ethtool net-tools drbl-ntfsprogs lzop"
pkgs_to_query="lvm2"
# Check if lvm2 is available, if so, add it, we need this for 
# clonezilla to save or restore LVM2 PV/VG/LV.
# Check lzop also.
for ipkg in $pkgs_to_query; do
  if urpmq $ipkg &>/dev/null; then
    pkgs_for_MDK="$pkgs_for_MDK $ipkg"
  fi
done

$inst_prog $inst_opt $inst_action $pkgs_for_MDK

# netinstall
# Get the available netinstall rpms:
# urpmq will output the resutl to stderr... Strange! Anyway, redirect to stdout
netinstall_for_urpmi="$(urpmq netinstall 2>&1 | grep -iE "(\-mandrake-|\-mandriva-|\-redhat-|\-fedora-|\-debian-|\-CentOS-|\-ubuntu-|\-suse-|\-opensuse-)" | sort)"
eval net_install_pkgs=\$netinstall_for_${installer}

if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "Installing some Linux distribution network installation images..."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  $inst_prog $inst_opt $inst_action $net_install_pkgs
fi

# 5. dev for client machines
# For MDK 9.2... no dev rpm...It's necessary to make one before run
# this... (Just create the tarball from some Mandrake machine)
# It's ugly here...
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir
if [ -f $drbl_setup_path/files/$OS_Version/dev.$OS_Version.tgz ]; then
  cp -f $drbl_setup_path/files/$OS_Version/dev.$OS_Version.tgz $drbl_pkgdir/dev.tgz
else
  # we must install udev...
  if ! rpm -q --quiet udev; then
    echo -n "Try to install udev..."
    $inst_prog $inst_opt $inst_action udev
    echo "done!"
  fi
  if ! rpm -q --quiet udev; then
    echo "No udev, and no pre-packaged devices file!!! Program terminated!"
    exit 1
  fi
fi

# 6.
# make the nbi/pxe files
create_pxe_nbi_files

# 7.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_create_files_for_PXELINUX"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
create_files_for_PXELINUX

echo "$msg_delimiter_star_line"
echo "$msg_Done!"

} # end of install_drbl_MDK

uninstall_drbl_MDK() {
# check if apt is installed or not
if ! rpm -q --quiet apt && ! rpm -q --quiet urpmi; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$msg_no_apt_already_uninstall"
   echo "$msg_program_stop"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
fi

# if apt is not installed, the installer must be urpmi
if ! rpm -q --quiet apt; then
   installer="urpmi"
   uninst_prog="urpme"
   uninst_action=""
else
   installer="apt"
   uninst_prog="apt-get"
   uninst_action="remove"
fi

# Get the installed netinstall rpms, this is special for MDK urpme... 
# for RH/FC, it's easier... just use regular expression package name (like rh-.*-netinstall)
net_install_pkgs="$(rpm -qa | grep -iE "(\-mandrake-|\-mandriva-|\-redhat-|\-fedora-|\-debian-|\-CentOS-|\-ubuntu-|\-suse-|\-opensuse-)" | sort)"

# rm the YP securenets
$DRBL_SCRIPT_PATH/sbin/drbl-yp-securenets clean

# Do not uninstall curl, usually necessary
$uninst_prog $uninst_action dhcp-server tftp-server nfs-utils ypserv ypbind yp-tools etherboot mkpxeinitrd-net etherboot partimage freedos clonezilla ddrescue dos2unix drbl-ntfsprogs
$uninst_prog $uninst_action ntp

[ -n "$net_install_pkgs" ] && $uninst_prog $uninst_action $net_install_pkgs

# rename some config files if they exist
for icfg in dhcpd.conf dhcpd.conf.etherboot.include exports ntp.conf sysconfig/iptables; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done

# remove the stale file
[ -f "/etc/init.d/ocs" ] && rm -f /etc/init.d/ocs

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_setup_answer
case "$drbl_setup_answer" in 
  y|Y|[yY][eE][sS])
    rpm -e drbl
   ;;
esac

echo "$msg_remove_urpmi_media_question"
echo -n "[y/N] "
read urpmi_media_answer
case "$urpmi_media_answer" in 
   y|Y|[yY][eE][sS])
     urpmi.removemedia -a
   ;;
esac

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_MDK

uninstall_drbl_DBN() {
# function to uninstall Debian DRBL
[ -e /etc/lsb-release ] && . /etc/lsb-release
if [ "$DISTRIB_ID" = "Ubuntu" ]; then
  # Ubuntu, not Debian.
  # Force to set LC_ALL=C, otherwise in Ubuntu, it will show a lot warning messages in debconf like
  # perl: warning: Setting locale failed.
  # perl: warning: Please check that your locale settings:
  # LANGUAGE = "zh_TW:zh:en_US:en",
  # LC_ALL = "zh_TW.Big5",
  # LANG = "zh_TW.UTF-8"
  # are supported and installed on your system.
  # perl: warning: Falling back to the standard locale ("C").
  export LC_ALL=C
fi
# rm the YP securenets
$DRBL_SCRIPT_PATH/sbin/drbl-yp-securenets clean

#
# do not remove rsync, bc, iptables, curl since this will remove a lots in B2D.
packages_to_be_removed="dhcp3-server tftpd-hpa nfs-kernel-server nis ntp wget lftp gawk udpcast"
#packages_from_drbl is loaded from conf/drbl.conf

apt-get --purge remove $packages_to_be_removed $packages_from_drbl
# Separate the dhcp3-server/dhcp3-common as an another package to be removed, othewise something went wrong in Woody. Do not put --purge 
apt-get remove dhcp3-common dhcp3-server

[ -n "$net_install_pkgs" ] && apt-get --purge remove $net_install_pkgs

# rename some config files if they exist
for icfg in exports ntp.conf drbl-nat.up.rules; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done
for icfg in dhcpd.conf dhcpd.conf.etherboot.include; do
  [ -f /etc/dhcp3/$icfg ] && mv -f /etc/dhcp3/${icfg} /etc/dhcp3/${icfg}.drblsave
done

# remove the stale file
[ -f "/etc/init.d/ocs" ] && rm -f /etc/init.d/ocs

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_setup_answer
case "$drbl_setup_answer" in 
  y|Y|[yY][eE][sS])
    dpkg --purge drbl
   ;;
esac

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_DBN

uninstall_drbl_SUSE() {
# function to uninstall SuSE DRBL
export LC_ALL=C
# rm the YP securenets
$DRBL_SCRIPT_PATH/sbin/drbl-yp-securenets clean

# do not remove curl, usually necessary
packages_to_be_removed="dhcp-server tftp nfs-utils ypserv ypbind yp-tools xntp mkpxeinitrd-net etherboot partimage freedos clonezilla tcpdump dos2unix ddrescue lftp ethtool drbl-ntfsprogs"
[ "$installer" = "apt" ] && apt-get --purge remove $packages_to_be_removed
[ "$installer" = "yum" ] && yum remove $packages_to_be_removed

if [ -n "$net_install_pkgs" ]; then
  [ "$installer" = "apt" ] && apt-get --purge remove $net_install_pkgs
  [ "$installer" = "yum" ] && yum remove $net_install_pkgs
fi

# rename some config files if they exist
for icfg in dhcpd.conf dhcpd.conf.etherboot.include exports ntp.conf; do
  [ -f "/etc/${icfg}" ] && mv -f /etc/${icfg} /etc/${icfg}.drblsave 
done

# remove the stale file
[ -f "/etc/init.d/ocs" ] && rm -f /etc/init.d/ocs
[ -f "/etc/apt/apt.conf.d/drbl-arch.conf" ] && rm -f /etc/apt/apt.conf.d/drbl-arch.conf

if [ "$installer" = "apt" ]; then
  echo "$msg_remove_apt_question"
  echo -n "[y/N] "
  read apt_answer
  case "$apt_answer" in 
     y|Y|[yY][eE][sS])
        rpm -e apt-libs apt
        [ -d /var/cache/apt/ ] && echo "$msg_cleaning_apt_cache" && rm -rf /var/cache/apt/
     ;;
  esac
elif [ "$installer" = "yum" ]; then
  echo "$msg_remove_yum_repo_question"
  echo -n "[y/N] "
  read yum_repo_answer
  case "$yum_repo_answer" in 
   y|Y|[yY][eE][sS])
     # we won't touch the existing one, just copied by drbl.
     repo_lists="drbl.repo drbl-testing.repo drbl-unstable.repo drbl-extra.repo opensuse-${OS_VER_NO}.repo opensuse-updates-${OS_VER_NO}.repo"
     for ifile in $repo_lists; do
       [ -f "/etc/yum.repos.d/$ifile" ] && mv -v /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
     done
  esac
fi

echo "$msg_remove_drbl_setup_question"
echo -n "[y/N] "
read drbl_setup_answer
case "$drbl_setup_answer" in 
  y|Y|[yY][eE][sS])
    rpm -e drbl
   ;;
esac

# clean tftpboot and misc files.
clean_tftpboot_and_misc

echo "$msg_Done!!!"

} # end of uninstall_drbl_SUSE

# common functions for DRBL in RedHat/Fedora/Mandrake Linux.

#
apt_install_result_msg() {
  RETVAL="$1"
  if [ "$RETVAL" -gt 0 ]; then 
    echo "$msg_failed_to_install_pkgs"
    echo "$msg_please $msg_browse $drbl_url_zh_TW $msg_or $drbl_url_en_US $msg_for_more_details. "
    echo "$msg_press_ctrl_c_stop"
    read
    exit 1 
  fi
}
#
check_apt_install() {
  local RETVAL=$1
  if [ "$RETVAL" -gt 0 ]; then 
    echo "$msg_failed_to_install_pkgs"
    echo "$msg_please $msg_browse $drbl_url_zh_TW $msg_or $drbl_url_en_US $msg_for_more_details. "
    echo "$msg_press_ctrl_c_stop"
    read
    exit 1 
  fi
}

#
check_rpm_install() {
  local RETVAL=$1
  local pkg=$2
  if [ "$RETVAL" -gt 0 ]; then
  echo "XXXXXXX        XXXXXXX       XXXXXXX"
  echo "$msg_Failed_to_install \"$pkg\"!!!"
  echo "$msg_check_network_and_url $url"
  exit 1
  fi
}

# define a function get_inst_rpm to get and install rpm package which 
# apt can not process now. ie. only i386 rpms
get_inst_rpm() {
  # Note! the last / in $url must be kept!!!
  # example: get_inst_rpm -u http://os.nchc.org.tw/... -p glibc -a i386
  # If -f|--url-pkg is set, the rpm name is specified, so we will NOT use -A '*...rpm" for wget.
  # Note: -f|--url-pkg (no wildcard) and "-u|--url, -p|--pkg, -a|--arch" (wildcard) are two different ways to assign the package. They conflict each other.
  local specified_name="no"
  local url_tmp pkg arch rpm_opt dir=""
  while [ $# -gt 0 ]; do
    case "$1" in
     -f|--url-pkg)
        # full name (path and name, no wildcard)
        shift; url_pkg="$1"
        specified_name="yes"
	url_tmp="$(dirname $url_pkg)"
	# Note! This pkg is like: glibc-2.4-25.i586.rpm
	pkg="$(basename $url_pkg)"
	shift;;
     -u|--url)
        shift; url_tmp="$1"
	shift;;
     -p|--pkg)
        shift; pkg="$1"
	shift;;
     -a|--arch)
        shift; arch="$1"
	shift;;
     -r|--rpm-opt)
        shift; rpm_opt="$1"
	shift;;
     *)
        break ;;
    esac
  done
  pkg_tmp_dir="$(mktemp -d -q /tmp/$pkg.XXXXXX)"
  # try to append the version for regular expression if pkg without version number filter.
  if [ "$specified_name" = "no" ]; then
    if [ -n "$pkg" -a -z "$(echo $pkg | grep -E "\-[0-9]")" ]; then
      pkg="$pkg-[0-9]"
    fi
  fi
  case "$url_tmp" in
  http://*|ftp://*)
    if [ "$specified_name" = "yes" ]; then
      wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$pkg_tmp_dir" $url_pkg
    else
      # use wildcard
      wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$pkg_tmp_dir" -A "$pkg*.$arch.rpm" $url_tmp/
    fi
    ;;
  file://*)
    # TODO: maybe use link (ln -fs ) ?
    dir="$(echo $url_tmp | sed -e "s|file://||g")"
    if [ "$specified_name" = "yes" ]; then
      cp -f "$dir/$pkg" $pkg_tmp_dir
    else
      # use wildcard
      cp -f "$dir/$pkg*.$arch.rpm" $pkg_tmp_dir
    fi
    ;;
  esac
  pkg_file="$(ls $pkg_tmp_dir/$pkg* 2>/dev/null)"
  if [ -n "$pkg_file" ]; then
    rpm -Uvh $rpm_opt $opt $pkg_file
    RETVAL=$?
    if [ "$RETVAL" -gt 0 ]; then 
       if [ -z "$(echo $pkg | grep -E "\<glibc\>")" ]; then
         # UGLY!!!
         # For glibc, with --force, although the reinstallation is successful,
	 # but the return code is always 1. ignore that.
         echo "$msg_Failed_to_install $pkg_file..."
       fi
    fi
  else
    RETVAL=1
  fi
  [ -d "$pkg_tmp_dir" ] && rm -rf $pkg_tmp_dir
  return $RETVAL
} # end of get_inst_rpm
# force to run apt-get install, this will force to install the necessary packages
check_apt_integrity() {
   apt-get -f --yes install
}

#
install_RH_MDK_client_kernel() {
# here we have 2 method to install kernel, 1 is apt, 2 using rpm directly.
local kgot
local KEXTRA=""
local avail_krpm
# we use curl and other script to find the latest kernel
LATEST_KTMP="$(mktemp /tmp/latest_ktmp.XXXXXX)"

# For Fedora Core 5 with x86_64 arch, all kernels have SMP support, even though the name will not contain smp.
case "$OS_Version" in
   FC[5-9])
     # Remove the -smp tag in kernel package name if it's x86_64 FC5 or later. 
     if [ "$ARCH" = "x86_64" ]; then
       echo "Note: For Fedora Core 5 or newer version with x86_64 arch, all kernels have SMP support, even though the name will not contain smp."
       SMP_OPTION=""
     fi
     ;;
esac

# Makdrake 10.1/10.2 uses kernel-i586-up-1GB for 586 CPU,
# The kernel rpm kernel-2.6.11.6mdk-1-1mdk.i586.rpm is only working 
# with i686 CPU.
# BUG REPORT, but they do NOT care!
# So I have to put a special case here. 
# http://qa.mandrakesoft.com/show_bug.cgi?id=14581
# http://qa.mandrakesoft.com/show_bug.cgi?id=15383
# SHIT!!! Why MDK 10.1/10.2 kernel arch does NOT consistent with rpm name ?
case "$OS_Version" in
     MDK10.[1-2]|MDV*)
       case "$client_archi_set" in
        i[35]86)
	    if [ -z "$SMP_OPTION" ]; then
              KEXTRA="-i586-up-1GB"
              echo "Since there is a bug in Mandarke 10.1, Mandriva 2005 or later version, we need to use the kernel rpm package "kernel${KEXTRA}" for i586 CPU client!"
            fi
            ;;
       esac
     ;;
esac

# set the OS and update dir
eval RPMS_os_update_dir=\$${OS_type}_RPMS_os_update_dir

klist_no=
i=1
for idir in $RPMS_os_update_dir; do
  kgot="$($DRBL_SCRIPT_PATH/bin/list_latest_rpm $url_os_coreroot/${idir}/  | grep -E "^kernel${KEXTRA}${SMP_OPTION}[#-]2\.[0-9]+\.[0-9]+" | grep -v "test" | sed -e "s/\.i[356]86.rpm//g" -e "s/\.x86_64.rpm//g")" 
  # store the url to use later.
  klist_ar[$i]="$url_os_coreroot/${idir}/"
  i=$((i+1))
  # RPMS.os and RPMS.core might be symbolic link, so maybe the result will
  # duplicate. To avoid this, we filter it first.
  if [ -n "$kgot" ]; then
     for ikgot in $kgot; do
      if ! grep -q $ikgot $LATEST_KTMP; then
	# make a tag so that we know the kernel is from ?
        if [ -n "$(echo $idir | grep -i updates)" ]; then
          ktag="(in updates)"
	else
          ktag="(in release)"
        fi
	klist_no=$(($klist_no + 1))
        echo "${klist_no}: $ikgot $ktag" >> $LATEST_KTMP
      fi
     done
  fi
done

# If no kernel is found, show some method
if [ -z "$(cat $LATEST_KTMP 2>/dev/null)" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$msg_no_kernel_rpm_is_available ${klist_ar[@]}"
  echo "$msg_txt_check:"
  echo "$msg_internet_connection"
  [ "$installer" = "yum" ] && echo "$msg_use_baseurl_in_yum_centos"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

#
chosen_kernel_for_client=""
while [ -z "$chosen_kernel_for_client" ]; do
  # We use the release kernel as the default one, since FC3/4 the kernel changes too aggressive in updates.
  # Maybe there are more than 1 kernel image in the directory, like in MDK 10.1
  # 1: kernel-2.4.28.0.rc1.6mdk-1-1mdk (in updates)
  # 2: kernel-2.6.8.1.25mdk-1-1mdk (in updates)
  # 3: kernel-2.4.27.0.pre2.1mdk-1-1mdk (in release)
  # 4: kernel-2.6.8.1.12mdk-1-1mdk (in release)
  latest_krn_in_release="$(awk -F" " '/in release/ {print $2}' $LATEST_KTMP | $DRBL_SCRIPT_PATH/bin/pkg-ver-latest)"
  default_release="$(awk -F":" "/$latest_krn_in_release/ {print \$1}" $LATEST_KTMP)"
  # If only 1 kernel available, just use that.
  if [ "$(wc -l $LATEST_KTMP | awk -F" " '{print $1}')" -eq 1 ]; then
    chosen_ker="$default_release"
  else
    echo "$msg_available_kernel:"
    cat $LATEST_KTMP
    echo "$msg_which_kernel_do_you_perfer"
    echo -n "[$default_release] "
    read chosen_ker
    [ -z "$chosen_ker" ] && chosen_ker="$default_release"
    echo "$msg_you_choose $chosen_ker."
  fi
  chosen_kernel_for_client="$(awk -F" " "/^$chosen_ker:/ {print \$2}" $LATEST_KTMP)"
  if [ -z "$chosen_kernel_for_client" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "$msg_not_available_enter_again"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  fi
done
[ -f "$LATEST_KTMP" ] && rm -f $LATEST_KTMP

echo "${msg_chosen_kernel}: $chosen_kernel_for_client"
# get the version and release of kernel, strip the kernel[#-] or kernel-smp[#-]
drbl_kernel_ver="$(echo $chosen_kernel_for_client | sed -e s/kernel${KEXTRA}${SMP_OPTION}[#-]//g)"

if [ -n "$(echo $OS_Version | grep -i "MD[KV]")" ]; then
  # For Mandrake only.
  # drbl_kernel_ver looks like: 2.4.22-28mdk
  # the rpm version tag is like 2.4.22.28mdk...ha... - and . different...
  # It's different than what is in RedHat/Fedora...For RH/FC, they are consistent
  # so replace it!
  last_tag=$(echo $drbl_kernel_ver | awk -F'.' '{print $NF}')
  drbl_kernel_ver=$(echo $drbl_kernel_ver | sed -e "s/\.$last_tag/-$last_tag/")

  # Makdrake 10.1/10.2 uses kernel-i586-up-1GB for 586 CPU,
  # The kernel rpm kernel-2.6.11.6mdk-1-1mdk.i586.rpm is only working 
  # with i686 CPU.
  # BUG REPORT, but they do NOT care!
  # So I have to put a special case here. 
  # http://qa.mandrakesoft.com/show_bug.cgi?id=14581
  # http://qa.mandrakesoft.com/show_bug.cgi?id=15383
  # SHIT!!! Why MDK 10.1/10.2 kernel arch does NOT consistent with rpm name ?
  case "$OS_Version" in
       MDK10.[1-2]|MDV*)
         case "$client_archi_set" in
          i[35]86)
	    if [ -z "$SMP_OPTION" ]; then
              KEXTRA="-i586-up-1GB"
              drbl_kernel_ver=${drbl_kernel_ver}${KEXTRA}
	    fi
              ;;
         esac
       ;;
  esac
fi

[ -z "$drbl_kernel_ver" ] && echo "Unable to find an appropriate kernel for client! Program terminated!!!" && exit 1

# UGLY!!! find a better way to know drbl_kernel_ver for SMP...
[ -n "$SMP_OPTION" ] && drbl_kernel_ver=${drbl_kernel_ver}smp
# drbl_kernel_ver looks like: 2.4.22-1.2199.nptl
# for SMP, it looks like: 2.4.22-1.2199.nptlsmp

# Install kernel
# Create the necessary dirctories, UGLY...
# the /tftpboot/node_root/tmp/boot is for link only, refer to the /tftpboot/node_root/etc/rc.d/rc.sysinit, the code "# For DRBL, create the directory /tmp/boot, so that the link /tmp/boot /boot...
mkdir -p /tftpboot/node_root/tmp/boot/ /tftpboot/node_root/var/{lib,tmp} /tftpboot/nodes /tftpboot/node_root/lib/modules/
(cd /tftpboot/node_root; ln -fs tmp/boot boot)

# if we can not find the installed kernel for clients, or the kernel arch not
# fit the requirement, install it.
if [ ! -d "$drbl_common_root/lib/modules/$drbl_kernel_ver" -a \
     ! -f "$drbl_common_root/lib/modules/$drbl_kernel_ver/modules.dep" ] ||
   [ "$(cat $pxecfg_pd/client_kernel_arch.txt 2>/dev/null)" != "$client_archi_set" ]
then 
   # if we find the installed kernel in common root, install the kernel and
   # create the modules.dep
   echo "$msg_install_kernel_might_take_several_minutes"

   # if the latest kernel exists in the server, and its ARCH fits client, 
   # just copy it
   # if not, use rpm --force to install it.
   # client arch maybe: i386/i586/best_optimization
   # we must convert the apt-cache result (only in redhat/mandrake) 
   # kernel#2.6.10-1.760_FC3 -> kernel-2.6.10-1.760_FC3 if querying by rpm -q
   latest_drbl_kernel="$(echo $chosen_kernel_for_client | tr "#" "-")"
   rpm -q --quiet $latest_drbl_kernel && server_kernel_ARCH="$(rpm -q --qf '%{ARCH}' $latest_drbl_kernel)"

   if [ "$client_archi_set" = "best_optimization" -o "$server_kernel_ARCH" = "$client_archi_set" ] && [ -d "/lib/modules/$drbl_kernel_ver" ]; then
    echo -n "Found latest kernel \"$latest_drbl_kernel\" in the server, and its architecture \"$server_kernel_ARCH\" meets the client's requirement ($client_archi_set). Copy it to clients' common root template..."
    # files like:
    #/boot/System.map-2.6.10-1.760_FC3
    #/boot/config-2.6.10-1.760_FC3
    #/boot/vmlinuz-2.6.10-1.760_FC3
    rsync -a /boot/*$drbl_kernel_ver /tftpboot/node_root/boot
    
    #/lib/modules/2.6.10-1.760_FC3
    rsync -a /lib/modules/$drbl_kernel_ver /tftpboot/node_root/lib/modules/

    # make a tag in the system for client's kernel arch.
    echo "$client_archi_set" > /tftpboot/nbi_img/client_kernel_arch.txt
    echo "done!"

   else
    # sync the RPM db for the node_root, cheat apt-get... otherwise apt-get will
    # try to install a base system in /tftpboot/node_root, we do not need that.
    rsync -a /var/lib/rpm /tftpboot/node_root/var/lib/
    
     # using RPM to install kernel. 
     # Try to find it's in RPMS.core or RPMS.updates
     # Find the architecture first.
     
     # filename got from apt-cache is different from that in the url, 
     # we have to replace like
     # kernel#2.6.10-1.766_FC3*.i586.rpm -> kernel-2.6.10-1.766_FC3*.i586.rpm
     chosen_kernel_for_client_RPM_FORMAT="$(echo $chosen_kernel_for_client | sed -e "s/#/-/g")"
     case "$client_archi_set" in
      "best_optimization")
        # optimization, it could be i386/i586/i686/athlon
        # Makdrake 10.1/10.2 uses kernel-i586-up-1GB for 586 CPU,
        # The kernel rpm kernel-2.6.11.6mdk-1-1mdk.i586.rpm is only working 
        # with i686 CPU.
	# STUPID!!! We have to force the rpm package tag (KARCH_RPM) as i586, 
	# but actually it for i686 (KARCH). SHIT!!!
        case "$OS_Version" in
          MDK10.[1-2]|MDV*)
	     KARCH=i686
             KARCH_RPM=i586
             ;;
          *)
	     # for other cases, KARCH=KARCH_RPM, they are consistent!!!
             KARCH="$($DRBL_SCRIPT_PATH/bin/check_kernel_cpu_arch `uname -r`)"
	     # if not found (maybe user make his own kernel, 
	     # but did NOT put config in /boot), set it as i686 as default
	     [ -z "$KARCH" ] && KARCH=i686
	     KARCH_RPM=$KARCH
             ;;
        esac
        ;;
      "i386")
        # for i386... this is not available for FC2/3, but we will force it
        # to be i586 before this...
        KARCH=i386
	KARCH_RPM=$KARCH
        ;;
      *)
        # for i586
        KARCH=i586
	KARCH_RPM=$KARCH
        ;;
     esac
     # install it
     avail_krpm=""
     for idir in $RPMS_os_update_dir; do
       avail_krpm="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_os_coreroot/${idir} | grep "^$chosen_kernel_for_client_RPM_FORMAT.${KARCH_RPM}.rpm" 2>/dev/null)"
       rc=1
       if [ -n "$avail_krpm" ]; then
         kgot_url_rpm="$url_os_coreroot/${idir}/${avail_krpm}"
         echo "Installing $KARCH $chosen_kernel_for_client_RPM_FORMAT from $kgot_url_rpm. Downloading..."
         #rpm -ivh --root /tftpboot/node_root/ --force --nodeps --noscripts $kgot_url_rpm
         get_inst_rpm -f $kgot_url_rpm -r "--root /tftpboot/node_root/ --force --nodeps --noscripts"
         rc=$?
       fi
       # once it's done, skip the next search
       if [ "$rc" -eq 0 ]; then
         # make a tag in the system for client's kernel arch.
         echo "$KARCH" > $pxecfg_pd/client_kernel_arch.txt
         break
       else
	 [ -n "$verbose" ] && echo "$chosen_kernel_for_client_RPM_FORMAT is not found in $url_os_coreroot/${idir}."
       fi
     done
     # in case when kernel rpm installation fails.
     check_rpm_install $rc $chosen_kernel_for_client_RPM_FORMAT
     #;;
    #esac

    # clean the rpm db to avoid confusing.
    [ -d "/tftpboot/node_root/var/lib/rpm/" ] && rm -rf /tftpboot/node_root/var/lib/rpm/

    # UGLY... find a better to strip the tail for MDK kernel
    # for MDK kernel-2.6.8.1.24mdk-1-1mdk, however the kernel tag is 2.6.8.1-24mdk
    # we need to strip the -1-1mdk
    drbl_kernel_ver="$(echo $drbl_kernel_ver | sed -e "s/-1-1mdk//g")"
    
    # create modules.dep...
    if [ -e "/tftpboot/node_root/boot/System.map-$drbl_kernel_ver" ]; then
       create_depmod_env /tftpboot/node_root
       /usr/sbin/chroot /tftpboot/node_root/ depmod -ae -F /boot/System.map-$drbl_kernel_ver $drbl_kernel_ver
       #clean_depmod_env /tftpboot/node_root
    else
       echo "Can not find /tftpboot/node_root/boot/System.map-$drbl_kernel_ver $drbl_kernel_ver! Program terminated!!!"
       exit 1 
    fi
   fi
else 
   echo "$client_archi_set" > $pxecfg_pd/client_kernel_arch.txt
   echo "$msg_latest_kernel_is_already_installed"
fi
} # end of install_RH_MDK_client_kernel

#
create_files_for_PXELINUX() {
echo "Copying pxelinux.0, menu.c32, chain.c32 and memdisk to /tftpboot/nbi_img..."

[ ! -d /tftpboot/nbi_img/pxelinux.cfg ] && mkdir -p /tftpboot/nbi_img/pxelinux.cfg

# make a version tag in /tftpboot/nbi_img
pxelinux_src_path="$(dirname $pxelinux_file)"
cp -af $pxelinux_src_path/VERSION /tftpboot/nbi_img/PXELINUX_VERSION

# These files (pxelinux.0, menu.c32, chain.c32, memdisk...) are very important.
# Hence we process here separately.

# pxelinux.0
if [ -f $pxelinux_file ]; then
   cp -af $pxelinux_file /tftpboot/nbi_img
else
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_Warning! $pxelinux_file $msg_NOT_found!!! $msg_PXE_NOT_work"
   echo "$msg_press_enter_to_continue"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   read
fi
# for simple menu
if [ -f $pxelinux_simple_menu ]; then
   cp -af $pxelinux_simple_menu /tftpboot/nbi_img
else
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_Warning! $pxelinux_simple_menu $msg_NOT_found!!! $msg_PXE_NOT_work"
   echo "$msg_press_enter_to_continue"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   read
fi
# for chain loader
if [ -f $pxelinux_chain_file ]; then
   cp -af $pxelinux_chain_file /tftpboot/nbi_img
else
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_Warning! $pxelinux_chain_file $msg_NOT_found!!! $msg_PXE_NOT_work"
   echo "$msg_press_enter_to_continue"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   read
fi
# memdisk
if [ -f $pxelinux_memdisk_file ]; then
   cp -af $pxelinux_memdisk_file /tftpboot/nbi_img
else
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_Warning! $pxelinux_memdisk_file $msg_NOT_found!!! $msg_PXE_NOT_work"
   echo "$msg_press_enter_to_continue"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   read
fi

# memtest86+
# make a version tag in /tftpboot/nbi_img
echo "Copying memtest86+ to /tftpboot/nbi_img..."
memtest86_src_path="$(dirname $memtest86_file)"
cp -af $memtest86_src_path/VERSION /tftpboot/nbi_img/MEMTEST86+_VERSION

if [ -f "$memtest86_file" ]; then
   cp -af $memtest86_file /tftpboot/nbi_img
else
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_Warning! $memtest86_file $msg_NOT_found!!!"
   echo "$msg_press_enter_to_continue"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   read
fi

# For etherboot PXE image (Especially necessary for sis900 NIC)
# clean the old ones
rm -f /tftpboot/nbi_img/eb-*-etherboot-pci.zpxe
# get the all-in-one etherboot zpxe and sis900 zpxe filename, we use sort and head to choose only one.
etherboot_pxe="$(LC_ALL=C unalias ls 2> /dev/null; ls -f /tftpboot/etherboot-*/eb-*-etherboot-pci.zpxe 2>/dev/null | sort -r | head -n1)"
sis900_pxe="$(LC_ALL=C unalias ls 2> /dev/null; ls -f /tftpboot/etherboot-*/sis900.zpxe 2>/dev/null | sort -r | head -n1)"
cp -af $sis900_pxe $etherboot_pxe /tftpboot/nbi_img/

# create the pxelinux default
$DRBL_SCRIPT_PATH/sbin/generate-pxe-menu -s "$PXE_SERIAL_OUTPUT" -c "$CONSOLE_OUTPUT"

# create the freedos image, which a clean one.
# if user wants to insert files, use 
# the script /opt/drbl/sbin/insert-file-fdos.sh
# [ -f kernel.sys ] && rm -f kernel.sys
if [ -f "$fdos_img_src" ]; then
   # 2005/4/4 since etherboot 5.4.0 is released, no more NBI.
   # img_mt=`mktemp -d fdos_img.XXXXXX`
   # mount -t vfat -o loop $fdos_img_src $img_mt
   # cp -f $img_mt/kernel.sys .
   # umount $img_mt
   # mknbi-fdos --output=/tftpboot/nbi_img/$fdos_nbi_output kernel.sys $fdos_img_src
   cp -f $fdos_img_src /tftpboot/nbi_img/$fdos_img_output
   # [ -d "$img_mt" ] && rm -rf $img_mt
   # clean the unnecessary file
   # [ -f kernel.sys ] && rm -f kernel.sys
else
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_Warning! $fdos_img_src $msg_NOT_found!!! $msg_FreeDOS_NOT_work"
   echo "$msg_press_enter_to_continue"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

fi
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_Done!"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
} # end of create_files_for_PXELINUX

#
create_pxe_nbi_files() {
# make the nbi/pxe files
# 1st, clean the old nbi/pxe files
if [ "$(ls -alF /tftpboot/nbi_img/boot* 2> /dev/null)" != "" ]; then
   echo "$msg_delimiter_star_line"
   echo "$msg_remove_old_nbi"
   rm -f /tftpboot/nbi_img/boot*
fi

echo "$msg_delimiter_star_line"
echo "$msg_create_nbi_files"
# put and create the kernel and initrd for DRBL clients, this will be done by
# mknic-nbi
echo "$msg_latest_kernel_for_clients $drbl_kernel_ver"

# check if mknic-nbi is installed or not.
if [ ! -x "$DRBL_SCRIPT_PATH/sbin/mknic-nbi" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$msg_no_drbl_script"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi
# create the NBI and PXE image for DRBL clients
# modules will be copied when user runs drblpush
case "$SMP_OPTION" in
	"-smp")
         echo "Running $DRBL_SCRIPT_PATH/sbin/mknic-nbi --kernel $drbl_kernel_ver --all --smp --no-modules"
         $DRBL_SCRIPT_PATH/sbin/mknic-nbi --kernel $drbl_kernel_ver --all --smp --no-modules
         ;;
	 *)
         echo "Running $DRBL_SCRIPT_PATH/sbin/mknic-nbi --kernel $drbl_kernel_ver --all --no-modules"
         $DRBL_SCRIPT_PATH/sbin/mknic-nbi --kernel $drbl_kernel_ver --all --no-modules
esac
echo "$msg_Done!"
} # end of create_pxe_nbi_files

#
ask_if_setup_proxy() {
echo "$msg_delimiter_star_line"
DOMAINNAME=`/bin/dnsdomainname`
if [ -n "$DOMAINNAME" ]; then
    DEFAULT_PROXY="proxy.$DOMAINNAME"
fi

if [ -z "$set_proxy_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_set_proxy"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read set_proxy_answer
fi

case "$set_proxy_answer" in
    [yY]|[yY])
      echo "$msg_http_proxy_server"
      echo -n "[$DEFAULT_PROXY] "
      read http_proxy_server_name
      echo "$msg_http_proxy_port"
      echo -n "[3128] "
      read http_proxy_server_port
      if [ -z  "$http_proxy_server_name" ]; then
          http_proxy_server_name="proxy.$DOMAINNAME"
      fi
      if [ -z  "$http_proxy_server_port" ]; then
          http_proxy_server_port="3128"
      fi

      # use the one use input for DEFAULT value. 
      echo "$msg_ftp_proxy_server"
      echo -n "[$http_proxy_server_name] "
      read ftp_proxy_server_name
      echo "$msg_ftp_proxy_port"
      echo -n "[$http_proxy_server_port] "
      read ftp_proxy_server_port
      if [ -z  "$ftp_proxy_server_name" ]; then
          ftp_proxy_server_name="$http_proxy_server_name"
      fi
      if [ -z  "$ftp_proxy_server_port" ]; then
          ftp_proxy_server_port="$http_proxy_server_port"
      fi

      echo "$msg_delimiter_star_line"
      echo "$msg_http_proxy_you_set: http://$http_proxy_server_name:$http_proxy_server_port"
      echo "$msg_ftp_proxy_you_set:  http://$ftp_proxy_server_name:$ftp_proxy_server_port"
      echo "$msg_delimiter_star_line"

      export http_proxy=http://$http_proxy_server_name:$http_proxy_server_port
      export ftp_proxy=ftp://$ftp_proxy_server_name:$ftp_proxy_server_port
      ;;
    *)
esac
} # end of ask_if_setup_proxy

#
ask_and_parse_rpm_ayo_repo_options() {
echo "$msg_delimiter_star_line"
if [ -z "$drbl_unstable_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_drbl_unstable_option"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read drbl_unstable_answer
fi
  
# If unstable is chosen, we must set drbl_test_answer to yes.
case "$drbl_unstable_answer" in
  y|Y|[yY][eE][sS])
    drbl_test_answer="y"
    ;;
  *)
    if [ -z "$drbl_test_answer" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_drbl_test_option"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      echo -n "[y/N] "
      read drbl_test_answer
    fi
    ;;
esac

# [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
# echo "$msg_drbl_extra_option"
# [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
# echo -n "[y/N] "
# read drbl_extra_answer

# parse it
case "$drbl_test_answer" in
    [yY]|[yY][eE][Ss])
      DRBL_TEST="drbl-testing"
      ;;
    *)
      DRBL_TEST=""
esac
case "$drbl_unstable_answer" in
    [yY]|[yY][eE][Ss])
      DRBL_UNSTABLE="drbl-unstable"
      ;;
    *)
      DRBL_UNSTABLE=""
esac
# case "$drbl_extra_answer" in
#     [yY]|[yY][eE][Ss])
#       DRBL_EXTRA="drbl-extra"
#       ;;
#     *)
#       DRBL_EXTRA=""
# esac

} # end of ask_and_parse_rpm_ayo_repo_options

ask_and_parse_client_extra_setting() {
# netinstall options
if [ -z "$drbl_netinstall_answer" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_drbl_netinstall_option"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read drbl_netinstall_answer
fi

# SMP clients ?
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
if [ -z "$drbl_SMP_answer" ]; then
  echo "$msg_smp_clients_RH"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read drbl_SMP_answer
fi

# forget about this... we did NOT compile some module for a long time.
#[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
#echo "$msg_drbl_driver"
#[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
#echo -n "[Y/n] "
#read driver_answer

# console output
if [ -z "$console_output_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_serial_console_option"
  echo "$msg_know_nothing_serial_console"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read console_output_answer
fi

case "$console_output_answer" in
    [yY]|[yY])
      echo "$msg_serial_console_only"
      echo -n "[y/N] "
      read serial_console_only
      echo "$msg_redirect_to_which_port"
      echo "$msg_text_Enter \"1\" for COM1, \"2\" for COM2, \"3\" for COM3, \"4\" for COM4,"
      echo -n "[1] "
      read console_port
      echo "$msg_serial_console_speed"
      echo "$msg_text_Enter \"1\" for 9600, \"2\" for 19200, \"3\" for 38400."
      echo -n "[3] "
      read console_port_speed
      ;;
    *)
esac

# parse it
case "$console_output_answer" in
    [yY]|[yY][eE][Ss])
      case "$console_port" in
          2)
            PXE_CONSOLE_PORT="1"
            CONSOLE_PORT="ttyS1"
            ;;
          3)
            PXE_CONSOLE_PORT="2"
            CONSOLE_PORT="ttyS2"
            ;;
          4)
            PXE_CONSOLE_PORT="3"
            CONSOLE_PORT="ttyS3"
            ;;
          *)
            PXE_CONSOLE_PORT="0"
            CONSOLE_PORT="ttyS0"
      esac
      case "$console_port_speed" in
          1)
            CONSOLE_PORT_SPEED="9600"
            ;;
          2)
            CONSOLE_PORT_SPEED="19200"
            ;;
          *)
            CONSOLE_PORT_SPEED="38400"
      esac
      case "$serial_console_only" in
      [yY]|[yY][eE][Ss])
        # note! The latter console will dominate the whole boot message
        CONSOLE_OUTPUT="console=tty0 console=$CONSOLE_PORT,"$CONSOLE_PORT_SPEED"n81"
	;;
      *)
        CONSOLE_OUTPUT="console=$CONSOLE_PORT,"$CONSOLE_PORT_SPEED"n81 console=tty0 "
      esac
      PXE_SERIAL_OUTPUT="serial $PXE_CONSOLE_PORT $CONSOLE_PORT_SPEED"
      echo "------------------------------------------------------"
      echo "$msg_serial_console_parameter: $CONSOLE_OUTPUT"
      echo "------------------------------------------------------"
      ;;
    *)
      CONSOLE_OUTPUT=""
      PXE_SERIAL_OUTPUT=""
esac
case "$drbl_SMP_answer" in
    [yY]|[yY][eE][Ss])
      SMP_OPTION="-smp"
      ;;
    *)
      SMP_OPTION=""
esac

# netinstall
case "$drbl_netinstall_answer" in
    [yY]|[yY][eE][Ss])
      DRBL_NETINSTALL="yes"
      ;;
    *)
      DRBL_NETINSTALL="no"
esac

} # end of ask_and_parse_client_extra_setting

#
backup_apt_sources_list() {
  if [ -f /etc/apt/sources.list ]; then
    echo "$msg_backup_apt_sources"
    mv -v /etc/apt/sources.list /etc/apt/sources.list.orig
  fi
}

#
clean_tftpboot_and_misc() {
  echo "$msg_delimiter_star_line"
  echo "$msg_cleaning_tftpboot_and_misc"
  echo "$msg_delimiter_star_line"
  # clean the nodes link, like 192.168.1.1...
  find /tftpboot/ -maxdepth 1 -type l -exec rm -f {} \;
  # we won't to affect those netstall RPMS, amybe they are not removed previuosly. .
  find /tftpboot/nbi_img/ ! -name "*netinstall*" ! -type d -exec rm -f {} \;
  [ -d "/tftpboot/nodes/" ] && rm -rf /tftpboot/nodes
  [ -d "/tftpboot/node_root/" ] && rm -rf /tftpboot/node_root
  [ -d "$drbl_syscfg" ] && rm -rf $drbl_syscfg
  [ -f "$ocsroot/clonezilla.lock" ] && rm -f $ocsroot/clonezilla.lock
  [ -f /etc/yp.conf ] && mv -v /etc/yp.conf /etc/yp.conf.drblsave
  [ -f /etc/ypserv.conf ] && mv -v /etc/ypserv.conf /etc/ypserv.conf.drblsave
  [ -f /var/yp/securenets ] && mv -v /var/yp/securenets /var/yp/securenets.drblsave
  [ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
} # end of clean_tftpboot_and_misc()

append_RH_apt_sources_list_example() {
cat <<-APT_END >> /etc/apt/sources.list
# Examples:
# DRBL for RH9 in drbl.sourceforge.net, only DRBL packages, no RH9 packages
#rpm http://drbl.sourceforge.net/redhat/apt redhat/9/i386 drbl drbl-testing
#rpm-src http://drbl.sourceforge.net/redhat/apt redhat/9/i386 drbl drbl-testing

# DRBL for FC1 in drbl.sourceforge.net, only DRBL packages, no FC1 packages
#rpm http://drbl.sourceforge.net/fedora/apt/ fedora/linux/1/i386 drbl drbl-testing
#rpm-src http://drbl.sourceforge.net//fedora/apt/ fedora/linux/1/i386 drbl drbl-testing

# DRBL for RH9 in free.nchc.org.tw, including DRBL packages and RH9 pacakges
#rpm http://free.nchc.org.tw//redhat/apt/ redhat/9/i386 os updates drbl
#rpm-src http://free.nchc.org.tw//redhat/apt/ redhat/9/i386 os updates drbl

# DRBL for FC1 in linux.nchc.org.tw, including DRBL packages and FC1 pacakges
#rpm http://free.nchc.org.tw//fedora/apt/ fedora/linux/1/i386 core updates drbl  
#rpm-src http://free.nchc.org.tw//fedora/apt/ fedora/linux/1/i386 core updates drbl  

### Dag Apt Repository for Red Hat Fedora Core 
#rpm http://apt.sw.be fedora/3/en/i386 dag
#rpm http://apt.sw.be fedora/2/en/i386 dag
#rpm http://apt.sw.be fedora/1/en/i386 dag

### Dag Apt Repository for Red Hat 9 (rh90)
#rpm http://apt.sw.be redhat/9/en/i386 dag

### Dag Apt Repository for Red Hat 8.0 (rh80)
#rpm http://apt.sw.be redhat/8.0/en/i386 dag

### Fedora Legacy Respository
# Red Hat Linux 8.0:
# rpm http://download.fedoralegacy.org/apt redhat/8.0/i386 os updates legacy-utils
# Red Hat Linux 9:
# rpm http://download.fedoralegacy.org/apt redhat/9/i386 os updates legacy-utils
# Fedora Core 1:
# rpm http://download.fedoralegacy.org/apt fedora/1/i386 os updates legacy-utils
# Fedora Core 2:
# rpm http://download.fedoralegacy.org/apt fedora/2/i386 os updates legacy-utils
APT_END

} # end of append_RH_apt_sources_list_example

parse_os_to_set_repository() {
# If the OS_Version is not supported, exit
if [ -z "$OS_Version" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$msg_not_determine_OS"
  echo "$msg_is_not_supported"
  echo "$msg_press_ctrl_c_stop!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  read
  exit 1 
fi

case "$OS_Version" in
   "RH8.0"|"RH9")
     # set the official release number... 8.0, 9...
     RH_VER=${OS_Version:2}
     #DIST="redhat"
     echo "$msg_OS_version: $OS_Version"
     # core_dir is for the core program, for RH8 and 9, RPMS.os, set it as "os"
     core_dir="os"
     # RH_RPMS_os_update_dir indicates in apt repository, 
     # where OS and updates pakcage (rpms) exist,
     # We will use it to search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     # freshrpms:
     #http://ayo.freshrpms.net/redhat/9/i386/updates/RPMS/
     #http://ayo.freshrpms.net/redhat/9/i386/os/RPMS
     # opensource (normal)
     #http://free.nchc.org.tw/redhat/linux/9/en/os/i386/RedHat/RPMS/
     #http://free.nchc.org.tw/redhat/linux/updates/9/en/os/i386
     #http://free.nchc.org.tw/redhat/linux/updates/9/en/os/i586
     #http://free.nchc.org.tw/redhat/linux/updates/9/en/os/i686
     RH_RPMS_os_update_dir="updates/$RH_VER/en/os/i386 updates/$RH_VER/en/os/i586 updates/$RH_VER/en/os/i686 updates/$RH_VER/en/os/noarch $RH_VER/en/os/i386/RedHat/RPMS $RH_VER/i386/updates/RPMS $RH_VER/i386/os/RPMS"
     ;;
   FC*)
     FC_VER=${OS_Version:2}
     #DIST="fedora"
     echo "$msg_OS_version: $OS_Version"
     # core_dir is for the core program, for FC1, RPMS.core, set it as "core"
     core_dir="core"
     # RH_RPMS_os_update_dir indicates in apt repository, 
     # where OS and updates pakcage (rpms) exist,
     # We will use it to search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     # freshrpms:
     #http://ayo.freshrpms.net/fedora/linux/3/i386/updates/RPMS/
     #http://ayo.freshrpms.net/fedora/linux/3/i386/core/RPMS/
     RH_RPMS_os_update_dir="updates/$FC_VER/$ARCH/ $FC_VER/$ARCH/os/Fedora/RPMS/ $FC_VER/$ARCH/updates/RPMS/ $FC_VER/$ARCH/os/RPMS/"
     ;;
   CO*)
     CO_VER=${OS_Version:2}
     echo "$msg_OS_version: $OS_Version"
     # RH_RPMS_os_update_dir indicates in apt repository, 
     # where OS and updates pakcage (rpms) exist,
     # We will use it to search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     RH_RPMS_os_update_dir="$CO_VER/updates/$ARCH/RPMS $CO_VER/os/$ARCH/CentOS/RPMS/"
     ;;
   MDK9.2|MDK10.0)
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_OS_version: $OS_Version"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     #http://free.nchc.org.tw/mandrake/apt/ mandrake/linux/9.2/i586 core updates drbl drbl-testing drbl-unstable

     # sources.list
     # rpm $url_os/official/$MDK_VER_PATH/i586/Mandrake/ base/hdlist RPMS
     # Mandrake updates
     # rpm $url_os/official/updates/$MDK_VER_PATH/ base/hdlist RPMS
     MDK_VER_PATH=${OS_Version:3}
     media="Mandrake"
     media_info="base"
     main_rpmdir="RPMS"
     contrib_rpmdir="RPMS2"
     # in MDK 9.2, urpmi is buggy. It will use synthesis.hdlist.cz for contrib and jpackage if "with ../base/synthesis.* is not set!!!" So we have to set contrib_opt and jpackage_opt
     contrib_opt="with ../base/synthesis.hdlist2.cz"
     jpackage_rpmdir="RPMS3"
     jpackage_opt="with ../base/synthesis.hdlist3.cz"
     main_updates="base"
     update_rpmdir="RPMS"
     # MDK_RPMS_os_update_dir indicates in rpm repository, 
     # where OS and update rpms exists
     # We will use MDK_RPMS_os_update_dir for search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     MDK_RPMS_os_update_dir="updates/$MDK_VER_PATH/$update_rpmdir $MDK_VER_PATH/i586/$media/$main_rpmdir"
     ;;
   MDK10.[1-2]|MDV*)
     [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
     echo "$msg_OS_version: $OS_Version"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     # sources.list
     # rpm http://free.nchc.org.tw//mandrake/official/10.1/i586/media media_info/hdlist main
     # Mandrake contrib
     # rpm http://free.nchc.org.tw//mandrake/official/10.1/i586/media media_info/hdlist2 contrib
     # Mandrake jpackage
     # rpm http://free.nchc.org.tw//mandrake/official/10.1/i586/media media_info/hdlist3 jpackage
     # Mandrake updates
     # rpm http://free.nchc.org.tw//mandrake/official/updates/10.1/ main_updates/media_info/hdlist main_updates
     MDK_VER_PATH=${OS_Version:3}
     # SHIT!!! Why Mandriva uses different path name 2006.0!!! With extra ".0" ?
     [ "$OS_Version" = "MDV2006.0" ] && MDK_VER_PATH="2006.0"
     media="media"
     media_info="media_info"
     main_rpmdir="main"
     contrib_rpmdir="contrib"
     # MDK 10.1 or later... let urpmi auto get, so do not set contrib_opt & jackage_opt
     contrib_opt=""
     jpackage_rpmdir="RPMS3"
     jpackage_opt=""
     jpackage_rpmdir="jpackage"
     main_updates="main_updates/media_info"
     update_rpmdir="main_updates"
     # SHIT!!! why Mandrake use differnet name for 2005 (LE2005) in updates ?
     if [ "$OS_Version" = "MDV2005" ];then
	update_extra_code_name="LE"
     else
	update_extra_code_name=""
     fi
     # MDK_RPMS_os_update_dir indicates in rpm repository, 
     # where OS and update rpms exists
     # We will use MDK_RPMS_os_update_dir for search glibc & openssl only, not
     # for apt repository (not for /etc/apt/sources.list)
     MDK_RPMS_os_update_dir="updates/$MDK_VER_PATH/$update_rpmdir $MDK_VER_PATH/i586/$media/$main_rpmdir"
     ;;
   *)
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "$OS_Version $msg_is_not_supported"
     echo "$msg_press_ctrl_c_stop"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     read
     exit 1 
esac

#
ask_if_setup_proxy

} # parse_os_to_set_repository

#
parse_client_archi_setting() {
# Apply the client_archi_ans for x86_64 and SMP
# x86_64 added, we will use best_optimization for x86_64.
[ "$ARCH" = "x86_64" -o "$ARCH" = "amd64" ] && client_archi_ans=2

# if SMP for client -> best_optimization.
# Not SMP -> ask user.
case "$drbl_SMP_answer" in
 [yY]|[yY][eE][Ss])
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_delimiter_star_line"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   echo "$msg_smp_optimization_kernel"
   client_archi_ans="2"
   echo -n "$msg_press_enter_to_continue"
   read 
   ;;
esac

# if $client_archi_ans is not set (i.e. NOT x86_64, NOT SMP), ask user.
if [ -z "$client_archi_ans" ]; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_delimiter_star_line"
   echo "$msg_apt_optimization_question"
   echo "$msg_optimization_level_0"
   echo "$msg_optimization_level_1"
   echo "$msg_optimization_level_2"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$msg_N_note"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_note!!! $msg_different_level_machine_prompt"
   echo "$msg_diff_arch_explain"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   echo "$msg_not_sure_better_1"
   echo -n "[1] "
   read client_archi_ans
   echo "$msg_delimiter_star_line"
fi
case "$client_archi_ans" in
   [0-9])
       echo "The CPU arch you specify: $client_archi_ans"
       ;;
   *)
       client_archi_ans=1
       ;;
esac

# check if the answer fits to the distribution
# This is based on the kernel rpm: kernel-.*.i[356]86.rpm
# client_archi_ans: (1) i386 (2) i586 (3) optimization
# for RH8/9, FC1 -> i386, i586, i686 are available
# for FC2/3 -> i586, i686 are available, i386 is not available
# for CO4/4.1/4.2 -> i586, i686 are available, i386 is not available
# for MDK -> i586 and i686 (a bug) are available, i386 is not available.
# for SUSE -> i586, i686 are available, i386 is not available
# for Debian woody/sarge/breezy -> i386, i686 are available, i586 is not available
# for Debian etch/dapper -> i486, i686 are available, i386 is not available
if [ "$client_archi_ans" = "0" ]; then
  case "$OS_Version" in
  FC[2-9]|MD[KV]*|SUSE*|CO*)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
        echo "$msg_change_opt_from_i386_to_i586"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        client_archi_ans="1"
        ;;
  esac
fi
# for Debian woody/sarge/breezy-> i386 and i686 are available, i586 is not available.
if [ "$client_archi_ans" = "1" ]; then
  case "$OS_Version" in
  DBN*)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
        echo "$msg_change_opt_from_i586_to_i386"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        client_archi_ans="0"
        ;;
  esac
fi
# 
case "$client_archi_ans" in
 2)
   # use default, let apt-rpm decide, so we do not have to change anything in
   # /etc/apt/apt.conf
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_same_optimization"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   client_archi_set="best_optimization"
   ;;
 0)
   # for i386... this is not available for FC2...
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_no_optimization"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   [ -f "/etc/apt/apt.conf" ] && perl -p -i -e "s/^.*Install-Options.*\"\".*;/    Install-Options \"\"; Architecture \"i386\";/" /etc/apt/apt.conf
   client_archi_set="i386"
   ;;
 *)
   # for apt to use upto i586, the change will be in /etc/apt/apt.conf
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_i586_optimization"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   [ -f "/etc/apt/apt.conf" ] && perl -p -i -e "s/^.*Install-Options.*\"\".*;/    Install-Options \"\"; Architecture \"i586\";/" /etc/apt/apt.conf
   client_archi_set="i586"
   ;;
esac

} # end of parse_client_archi_setting

#
install_client_archi_glibc_openssl_RH() {
echo "$msg_delimiter_star_line"
case "$client_archi_set" in
    "best_optimization")
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_optimization_is_on"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      ;;
    *)
    # for i586 and i386, we force to use i386 version: glibc, openssl and opensssl-perl (for CentOS 4.3)
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_optimization_is_off"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

      # I. glibc
      if [ -n "$glibc_ARCH" -a "$glibc_ARCH" != "i386" ]; then 
       installed_glibc=`rpm -qa |grep ^glibc-[0-9]`
       [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
       echo "$msg_install_i386_of $installed_glibc..."
       echo "$msg_searching_glibc_in_ayo..."
       [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       for irpm in $RH_RPMS_os_update_dir; do
	 # Before installing, check if it's available. This is due to a bug in
	 # FC4. rpm in FC4 will not able to return to command line if package
	 # is not available. So we have to check it first.
         Is_i386_glibc_available="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_os_coreroot/$irpm/ | grep "^${installed_glibc}.i386.rpm" 2>/dev/null)"
	 if [ -n "$Is_i386_glibc_available" ]; then
           echo "Installing i386 glibc from $url_os_coreroot/$irpm/${installed_glibc}.i386.rpm..." 
	   # since rpm does not support file://..., we use get_inst_rpm
	   #rpm -Uvh --force --quiet $url_os_coreroot/$irpm/${installed_glibc}.i386.rpm 2>/dev/null
	   get_inst_rpm -f $url_os_coreroot/$irpm/${installed_glibc}.i386.rpm -r "--force --quiet"
	 fi

         # it seems that even if rpm successfully install glibc with 
	 # -Uvh --force, the return code is still 1... 
	 # So we have to use rpm -q --qf to check
         #RETVAL=$?
         #[ $RETVAL -eq 0 ] && break
         rpm -q --qf '%{ARCH}' glibc > /dev/null && glibc_ARCH_now=`rpm -q --qf '%{ARCH}' glibc`
         [ "$glibc_ARCH_now" = "i386" ] && break
       done
       # add warning if i386 rpm is not installed
       if [ "$glibc_ARCH_now" != "i386" ]; then
          [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
          echo "Warning! Unable to find the i386 ${installed_glibc} in the repository!!! The DRBL client will fail to boot if different CPU arch from that of DRBL server!"
          [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       fi
       # sleep to make rpm db ok.
       sleep 5
       # to mv the i686 lib which should not exists in i386 glibc packags
       # if they are not cleaned.
       lib_bk="/lib/i686 /lib/tls /usr/lib/i686"
       for idir in $lib_bk; do
         if [ -d "$idir" ]; then
           [ -d "$idir.bak" ] && rm -rf $idir.bak
           mv -f $idir $idir.bak
         fi
       done
      fi
      # II. openssl
      if [ -n "$openssl_ARCH" -a "$openssl_ARCH" != "i386" ]; then 
       installed_openssl=`rpm -qa |grep ^openssl-[0-9]`
       [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
       echo "$msg_install_i386_of $installed_openssl..."
       echo "$msg_searching_openssl_in_ayo..."
       [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       # 
       RETVAL=1
       for irpm in $RH_RPMS_os_update_dir; do
         # search the openssl rpms in $RH_RPMS_os_update_dir
         Is_i386_openssl_available="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_os_coreroot/$irpm/ | grep "^${installed_openssl}.i386.rpm" 2>/dev/null)"
	 if [ -n "$Is_i386_openssl_available" ]; then
           echo "Installing i386 openssl from $url_os_coreroot/$irpm/${installed_openssl}.i386.rpm..." 
	   # since rpm does not support file://..., we use get_inst_rpm
	   #rpm -Uvh --force --quiet $url_os_coreroot/$irpm/${installed_openssl}.i386.rpm 2>/dev/null
	   get_inst_rpm -f $url_os_coreroot/$irpm/${installed_openssl}.i386.rpm -r "--force --quiet"
           RETVAL=$?
         fi
         [ $RETVAL -eq 0 ] && break
       done
       # add warning if i386 rpm is not installed
       if [ $RETVAL -gt 0 ]; then
          [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
          echo "Warning! Unable to find the i386 ${installed_openssl} in the repository!!! The client maybe can not be accessed via ssh!!!"
          [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       fi
      fi
      # III. openssl-perl (from CentOS 4.3)
      if [ -n "$openssl_perl_ARCH" -a "$openssl_perl_ARCH" != "i386" ]; then 
       installed_openssl_perl=`rpm -qa |grep ^openssl-perl-[0-9]`
       [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
       echo "$msg_install_i386_of $installed_openssl_perl..."
       echo "$msg_searching_openssl_perl_in_ayo..."
       [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       # 
       RETVAL=1
       for irpm in $RH_RPMS_os_update_dir; do
         # search the openssl_perl rpms in $RH_RPMS_os_update_dir
         Is_i386_openssl_perl_available="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_os_coreroot/$irpm/ | grep "^${installed_openssl_perl}.i386.rpm" 2>/dev/null)"
         if [ -n "$Is_i386_openssl_perl_available" ]; then
           echo "Installing i386 openssl_perl from $url_os_coreroot/$irpm/${installed_openssl_perl}.i386.rpm..." 
	   # since rpm does not support file://..., we use get_inst_rpm
           #rpm -Uvh --force --quiet $url_os_coreroot/$irpm/${installed_openssl_perl}.i386.rpm 2>/dev/null
	   get_inst_rpm -f $url_os_coreroot/$irpm/${installed_openssl_perl}.i386.rpm -r "--force --quiet"
           RETVAL=$?
         fi
         [ $RETVAL -eq 0 ] && break
       done
       # add warning if i386 rpm is not installed
       if [ $RETVAL -gt 0 ]; then
          [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
          echo "Warning! Unable to find the i386 ${installed_openssl_perl} in the repository!!! The client maybe can not be accessed via ssh!!!"
          [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
       fi
      fi
esac
} # end of install_client_archi_glibc_openssl_RH

#
install_client_archi_glibc_etc_SUSE() {
#
local url_tmp="" pkg_tmp=""
echo "$msg_delimiter_star_line"
case "$client_archi_set" in
    "best_optimization")
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_optimization_is_on"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      ;;
    *)
    # for i586 and i386, we force to use i586 version: glibc, db...
      [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
      echo "$msg_optimization_is_off"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      for ipkg in $i686_pkg_check_list_SUSE; do
        # i686_pkg_check_list_SUSE is: glibc glibc-devel db db-devel...
        # since "-" can not ba a variable name, replace it as "_"
        ipkg_arch_var="$(echo $ipkg | sed -e "s/-/_/g")"
        eval ipkg_ARCH=\$${ipkg_arch_var}_ARCH
        [ -z "$ipkg_ARCH" ] && continue
	echo "$ipkg arch: $ipkg_ARCH."
        if [ "$ipkg_ARCH" != "i586" ]; then 
         echo "Finding the i586 $ipkg in apt repository..."
         case "$installer" in
          apt)
           available_ipkg_rpm="$($DRBL_SCRIPT_PATH/bin/parse_apt_url_get_rpm.sh -a RPMS.updates -a RPMS.base -r i586 --no-specific-ver ${ipkg} 2>/dev/null)"
	   # The obtained $available_ipkg_rpm is like: http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/i586/glibc-2.4-25.i586.rpm
           if [ -n "$available_ipkg_rpm" ]; then
             echo "Installing i586 ${ipkg}: $available_ipkg_rpm..."
	     # since rpm does not support file://..., we use get_inst_rpm
             #rpm -Uvh --force --quiet $available_ipkg_rpm 2>/dev/null
	     get_inst_rpm -f $available_ipkg_rpm -r "--force --quiet"
           fi
	   ;;
          yum)
	   available_ipkg_rpm="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_os_coreroot/$OS_PATH_IN_YUM_REPO/inst-source/suse/i586/ | grep -E "^$ipkg-[0-9]+.*.i586.rpm" 2>/dev/null)"
           if [ -n "$available_ipkg_rpm" ]; then
             echo "Installing i586 ${ipkg}: $available_ipkg_rpm..."
	     # since rpm does not support file://..., we use get_inst_rpm
             # rpm -Uvh --force --quiet $url_os_coreroot/$OS_PATH_IN_YUM_REPO/inst-source/suse/i586/$available_ipkg_rpm 2>/dev/null
	     get_inst_rpm -f $url_os_coreroot/$OS_PATH_IN_YUM_REPO/inst-source/suse/i586/$available_ipkg_rpm -r "--force --quiet"
           fi
	   ;;
         esac
        
         # it seems that even if rpm successfully install glibc with 
         # -Uvh --force, the return code is still 1... 
         # So we have to use rpm -q --qf to check, not by return code.
         rpm -q --qf '%{ARCH}' ${ipkg} > /dev/null && ipkg_ARCH_now=`rpm -q --qf '%{ARCH}' ${ipkg}`
         [ "$ipkg_ARCH_now" = "i586" ] && continue
         # add warning if i586 rpm is not installed
         if [ "$ipkg_ARCH_now" != "i586" ]; then
            [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
            echo "Warning! Unable to find the i586 ${ipkg} in the repository!!! The DRBL client will fail to boot if different CPU arch from that of DRBL server!"
            [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
         fi
         if [ "$ipkg" = "glibc" ]; then
          # sleep to make rpm db ok.
          sleep 5
          # to mv the i686 lib which should not exists in i386 glibc packags
          # if they are not cleaned.
          lib_bk="/lib/i686 /lib/tls /usr/lib/i686"
          for idir in $lib_bk; do
            if [ -d "$idir" ]; then
              [ -d "$idir.bak" ] && rm -rf $idir.bak
              mv -f $idir $idir.bak
            fi
          done
         fi
        fi
      done
esac
} # end of install_client_archi_glibc_etc_SUSE

# TODO, maybe merge parse_rh_yum_repo_answer() and parse_rh_apt_repo_answer()
parse_RH_os_yum_repo_answer() {
local os_name="$1"
local answer="$2"
case "$answer" in
    1)
      # download.fedora.redhat.com
      url_os="http://download.fedora.redhat.com"
      os_ayo_path="/pub/fedora/linux/core"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    2) # TODO
      # mirrors.kernel.org
      url_os="http://mirrors.kernel.org"
      os_ayo_path="/fedora/core/"
      url_os_coreroot="$url_os/$os_ayo_path"

      # TODO
      # mirrorlist from fedora.redhat.com, but how to install glibc/openssl ?
      # or enter by user himeself
      ;;
    4)
      # skip the yum config, but we will use the following url_os... for glibc/kernel inst
      use_existing_yum_config="yes"

      url_os="http://free.nchc.org.tw"
      os_ayo_path="/fedora/linux/core/"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    5)
      #enter by user
      url_os_default="http://mirrors.kernel.org"
      os_ayo_path_default="/fedora/core/"
      echo "Please enter the hostname or IP address of yum reposity with http or ftp prefix..."
      echo -n "[$url_os_default] "
      read url_os
      [ -z "$url_os" ] && url_os="$url_os_default"

      echo "Enter the path where directory \"$FC_VER\" and \"updates\" exist..."
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_os="http://free.nchc.org.tw"
      case "$os_name" in
	FC*)
          os_ayo_path="/fedora/linux/core/"
	  ;;
	CO*)
          os_ayo_path="/centos/"
	  ;;
      esac
      url_os_coreroot="$url_os/$os_ayo_path"
esac
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_os, $os_ayo_path"
echo "------------------------------------------------------"

} # end of parse_RH_os_yum_repo_answer

#
parse_RH_drbl_yum_repo_answer() {
local answer="$1"
# TODO x86_64: change i386 -> x86_64 ? or it works ?
case "$answer" in
    1)
      # diskless.nchc.org.tw
      url_drbl="http://diskless.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
      ;;
    2)
      # drbl.sf.net
      url_drbl="http://drbl.sourceforge.net"
      drbl_ayo_path="drbl-core/i386"
      ;;
    5)
      #enter by user
      url_drbl_default="http://drbl.sourceforge.net"
      drbl_ayo_path_default="/drbl-core/i386/"
      echo "Please enter the hostname or IP address of yum reposity with http or ftp prefix where DRBL packages exists..."
      echo -n "[$url_drbl_default] "
      read url_drbl
      [ -z "$url_drbl" ] && url_drbl="$url_drbl_default"

      echo "Enter the path where directory \"RPMS.drbl\" exists..." 
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && drbl_ayo_path="$drbl_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_drbl="http://free.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
esac
url_drbl_rpmroot="$url_drbl/$drbl_ayo_path/"
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_drbl, $drbl_ayo_path"
echo "------------------------------------------------------"

} # parse_RH_drbl_yum_repo_answer

#
parse_RH_os_apt_repo_answer() {
local os_ver="$1"
local answer="$2"
local ver_no="${os_ver:2}"
case "$answer" in
    1)
      # freshrpms
      url_os="http://ayo.freshrpms.net"
      case "$os_ver" in
        RH*)
          url_os_coreroot="$url_os/redhat/"
          os_ayo_path="redhat/$RH_VER/i386"
          ;;
        FC*)
          url_os_coreroot="$url_os/fedora/linux/"
          os_ayo_path="fedora/linux/$FC_VER/i386"
  	  ;;
      esac
      FRESHRPMS="freshrpms"
      ;;
    3) 
      # local repository
      url_os="/"
      url_os_coreroot_default="/opt/mirror/fedora/linux/core"
      os_ayo_path_default="/opt/mirror/fedora/apt/fedora/linux/$ver_no/i386"
      echo "Enter the path where directory \"$ver_no\" and \"updates\" exist."
      echo -n "[$url_os_coreroot_default] "
      read url_os_coreroot
      [ -z "$url_os_coreroot" ] && url_os_coreroot="$url_os_coreroot_default"
      while [ ! -e "$url_os_coreroot/$ver_no" ]; do
	echo "No such directory \"$url_os_coreroot/$ver_no\" or \"$url_os_coreroot/updates/$ver_no\"!" 
	echo "Please enter it again!"
        read url_os_coreroot
      done

      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"
      while [ ! -e "$os_ayo_path/RPMS.$core_dir" ] || [ ! -e "$os_ayo_path/RPMS.drbl" ]; do
	echo "No such directory $os_ayo_path/RPMS.$core_dir or $os_ayo_path/RPMS.drbl!" 
	echo "Please enter it again!"
        read os_ayo_path
      done
      ;;
    5) 
      # enter by user
      url_os_default="http://ayo.freshrpms.net"
      os_coreroot_default="/fedora/linux/"
      os_ayo_path_default="/fedora/linux/$ver_no/i386"
      echo "Enter the hostname or IP address of $os_ver packages reposity with http or ftp prefix."
      echo -n "[$url_os_default] "
      read url_os
      [ -z "$url_os" ] && url_os="$url_os_default"

      echo "Enter the path where directory \"$ver_no\" and \"updates\" exist."
      echo -n "[$os_coreroot_default] "
      read os_coreroot
      [ -z "$os_coreroot" ] && os_coreroot="$os_coreroot_default"
      url_os_coreroot="$url_os/$os_coreroot"

      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_os="http://free.nchc.org.tw"
      case "$os_ver" in
        RH*)
          url_os_coreroot="$url_os/redhat/linux/"
          os_ayo_path="redhat/apt/redhat/linux/$RH_VER/i386"
          ;;
        FC*)
          url_os_coreroot="$url_os/fedora/linux/core/"
          os_ayo_path="fedora/apt/fedora/linux/$FC_VER/i386"
  	  ;;
      esac
esac
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_os, $url_os_coreroot, $os_ayo_path"
echo "------------------------------------------------------"

} # parse_RH_os_apt_repo_answer

parse_RH_drbl_apt_repo_answer() {
# url_drbl_rpmroot is where the RPMS.drbl, RPMS.drbl-testing, RPMS.drbl-unstable... exist
# url_os_rpmroot is where the RPMS.os, RPMS.core, RPMS.update... exist
# try to set url_drbl_rpmroot and url_os, we need 
# url_drbl_rpmroot to get wget and apt
# url_os_rpmroot to get glibc/openssl/kernel
# os_ayo_path is under $url_os where RPMS.os/RPMS.core exists
# drbl_ayo_path is under $url_drbl where RPMS.drbl exists
local answer="$1"
case "$answer" in
    1)
      # diskless.nchc.org.tw
      url_drbl="http://diskless.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
      ;;
    2)
      # drbl.sf.net
      url_drbl="http://drbl.sourceforge.net"
      drbl_ayo_path="drbl-core/i386"
      ;;
    3) 
      # local repository
      url_drbl_default="/"
      drbl_ayo_path_default="/drbl-core/i386"

      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && os_ayo_path="$drbl_ayo_path_default"
      while [ ! -e "$drbl_ayo_path/RPMS.$core_dir" ] || [ ! -e "$drbl_ayo_path/RPMS.drbl" ]; do
	echo "No such directory $drbl_ayo_path/RPMS.$core_dir or $drbl_ayo_path/RPMS.drbl!" 
	echo "Please enter it again!"
        read drbl_ayo_path
      done
      ;;
    5) 
      # enter by user
      url_drbl_default="http://drbl.sourceforge.net"
      drbl_ayo_path_default="/drbl-core/i386"

      echo "Enter the hostname or IP address of DRBL packages reposity with http or ftp prefix."
      echo -n "[$url_drbl_default] "
      read url_drbl
      [ -z "$url_drbl" ] && url_drbl="$url_drbl_default"
      echo "Enter the path where directory RPMS.$core_dir, RPMS.updates... exist."
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && drbl_ayo_path="$drbl_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_drbl="http://free.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
esac
url_drbl_rpmroot="$url_drbl/$drbl_ayo_path/"
case "${OS_Version}" in
   FC*)
      dist="fedora"
      ;;
   RH*)
      dist="redhat"
      ;;
esac
url_drbl_collect_root="$url_drbl/drbl-core/pre-drbl/${dist}/linux/${OS_Version:2}/i386"
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_drbl, $drbl_ayo_path"
echo "------------------------------------------------------"
} # parse_RH_drbl_apt_repo_answer

#
parse_MDK_os_ayo_repo_answer() {
# url_drbl_rpmroot is where the RPMS.drbl, RPMS.drbl-testing, RPMS.drbl-unstable... exist
# url_os_rpmroot is where the rpm files (lernel-2.6.8-24mdk-1-1mdk.i586.rpm)... exist
# try to set url_drbl_rpmroot and url_os, we need 
# url_drbl_rpmroot to get wget and apt
# url_os_rpmroot to get glibc/openssl/kernel
# url_os_rpmroot is to the path where "official" exists.
local answer="$1"
case "$answer" in
    1)
      # mdk.linux.org.tw
      url_os="ftp://mdk.linux.org.tw"
      os_ayo_path="/pub/mandrake-linux/official/"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    2)
      # distro.ibiblio.org
      url_os="http://distro.ibiblio.org"
      os_ayo_path="/pub/Linux/distributions/mandrake/Mandrakelinux/official/"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    4)
      # TODO... make $use_existing_yum_config work in urpmi.addmedia
      # skip the setting of yum, but we will add this for glibc/kernel inst.
      use_existing_urpmi_config="yes"
      url_os="http://free.nchc.org.tw"
      os_ayo_path="/mandrake/official/"
      url_os_coreroot="$url_os/$os_ayo_path"
      ;;
    5)
      #enter by user
      url_os_default="http://distro.ibiblio.org"
      os_ayo_path_default="/pub/Linux/distributions/mandrake/Mandrakelinux/official/"
      echo "Please enter the hostname or IP address of OS packages reposity with http or ftp prefix..."
      echo -n "[$url_os_default] "
      read url_os
      [ -z "$url_os" ] && url_os="$url_os_default"

      echo "Enter the path where directories \"${OS_Version:3}\" and \"updates\"exist..." 
      echo -n "[$os_ayo_path_default] "
      read os_ayo_path
      [ -z "$os_ayo_path" ] && os_ayo_path="$os_ayo_path_default"

      url_os_coreroot=$url_os/$os_ayo_path
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_os="http://free.nchc.org.tw"
      os_ayo_path="/mandrake/official/"
      url_os_coreroot="$url_os/$os_ayo_path"
esac
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_os, $url_os_coreroot"
echo "------------------------------------------------------"

} # end of parse_MDK_os_ayo_repo_answer

#
parse_MDK_drbl_ayo_repo_answer() {
# url_drbl_rpmroot is where the RPMS.drbl, RPMS.drbl-testing, RPMS.drbl-unstable... exist
# url_os_rpmroot is where the rpm files (lernel-2.6.8-24mdk-1-1mdk.i586.rpm)... exist
# try to set url_drbl_rpmroot and url_os, we need 
# url_drbl_rpmroot to get wget and apt
# url_os_rpmroot to get glibc/openssl/kernel
# url_os_rpmroot is to the path where "official" exists.
local answer="$1"
case "$answer" in
    1)
      # diskless.nchc.org.tw
      url_drbl="http://diskless.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
      ;;
    2)
      # drbl.sf.net
      url_drbl="http://drbl.sourceforge.net"
      drbl_ayo_path="drbl-core/i386"
      ;;
    5)
      #enter by user
      url_drbl_default="ftp://free.nchc.org.tw"
      drbl_ayo_path_default="/drbl-core/i386"
      echo "Please enter the hostname or IP address of apt reposity with http or ftp prefix..."
      echo -n "[$url_drbl_default] "
      read url_drbl
      [ -z "$url_drbl" ] && url_drbl="$url_drbl_default"

      echo "Enter the path where directory RPMS.drbl exist..." 
      echo -n "[$drbl_ayo_path_default] "
      read drbl_ayo_path
      [ -z "$drbl_ayo_path" ] && drbl_ayo_path="$drbl_ayo_path_default"
      ;;
    *)
      # default one, use free.nchc.org.tw
      url_drbl="http://free.nchc.org.tw"
      drbl_ayo_path="drbl-core/i386"
esac
url_drbl_rpmroot="$url_drbl/$drbl_ayo_path/"
echo "------------------------------------------------------"
echo "$msg_select_url_path: $url_drbl, $drbl_ayo_path"
echo "------------------------------------------------------"

} # end of parse_MDK_drbl_ayo_repo_answer
#

generate_fedora_yum_conf() {
# backup old file
echo "$msg_delimiter_star_line"
echo "Backuping the repo files..."
if [ "$use_existing_yum_config" = "yes" ]; then
  # we won't touch the existing $FC_CORE_REPO fedora-updates.repo
  for ifile in drbl.repo drbl-testing.repo drbl-unstable.repo drbl-extra.repo ; do
   [ -f "/etc/yum.repos.d/$ifile" ] && mv -f /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
  done
else
  for ifile in $FC_CORE_REPO fedora-updates.repo drbl.repo drbl-testing.repo drbl-unstable.repo ; do
   [ -f "/etc/yum.repos.d/$ifile" ] && mv -f /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
  done
fi
echo "done!"

# Turn off the testing and devel repository
echo -n "Turn off the testing and devel repository..."
for ifile in fedora-updates-testing.repo fedora-devel.repo; do
 [ -f "$/etc/yum.repos.d/$ifile" ] &&  perl -p -i -e "s/^enabled=.*/enabled=0/g" /etc/yum.repos.d/$ifile
done
echo "done!"

if [ "$use_existing_yum_config" != "yes" ]; then
  # overwrite the existing $FC_CORE_REPO and fedora-updates.repo
  #
  echo "Creating the fedora core packages repository list..."
  cat <<-YUM_END > /etc/yum.repos.d/$FC_CORE_REPO
[core]
name=Fedora Core \$releasever - \$basearch - Base
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/\$releasever/\$basearch/os/
#mirrorlist=http://fedora.redhat.com/download/mirrors/fedora-core-\$releasever
baseurl=$url_os_coreroot/\$releasever/\$basearch/os
enabled=1
gpgcheck=1
YUM_END

  #
  echo "Creating the fedora updates packages repository list..."
  cat <<-YUM_END > /etc/yum.repos.d/fedora-updates.repo
[updates-released]
name=Fedora Core \$releasever - \$basearch - Released Updates
#baseurl=http://download.fedora.redhat.com/pub/fedora/linux/core/updates/\$releasever/\$basearch/
#mirrorlist=http://fedora.redhat.com/download/mirrors/updates-released-fc\$releasever
baseurl=$url_os_coreroot/updates/\$releasever/\$basearch/
enabled=1
gpgcheck=1
YUM_END

fi
} # end of generate_fedora_yum_conf

#
generate_drbl_yum_conf() {
# DRBL stable
echo "$msg_delimiter_star_line"
echo "Creating the DRBL stable packages repository list..."
cat <<-YUM_END > /etc/yum.repos.d/drbl.repo
[drbl]
name=DRBL stable packages
baseurl=$url_drbl/$drbl_ayo_path/RPMS.drbl
enabled=1
gpgcheck=0
YUM_END

# DRBL testing
if [ -n "$DRBL_TEST" ]; then
  echo "Creating the DRBL testing packages repository list..."
  cat <<-YUM_END > /etc/yum.repos.d/drbl-testing.repo
[drbl-testing]
name=DRBL testing packages
baseurl=$url_drbl/$drbl_ayo_path/RPMS.drbl-testing
enabled=1
gpgcheck=0
YUM_END
fi

# DRBL testing
if [ -n "$DRBL_UNSTABLE" ]; then
  echo "Creating the DRBL unstable packages repository list..."
  cat <<-YUM_END > /etc/yum.repos.d/drbl-unstable.repo
[drbl-unstable]
name=DRBL unstable packages
baseurl=$url_drbl/$drbl_ayo_path/RPMS.drbl-unstable
enabled=1
gpgcheck=0
YUM_END
fi

# DRBL extra
# if [ -n "$DRBL_EXTRA" ]; then
#   echo "Creating the DRBL extra packages repository list..."
#   cat <<-YUM_END > /etc/yum.repos.d/drbl-extra.repo
# [drbl-extra]
# name=DRBL extra packages
# baseurl=$url_drbl/$drbl_ayo_path/RPMS.drbl-extra
# enabled=1
# gpgcheck=0
# YUM_END
# fi

echo "done!"
} # end of generate_drbl_yum_conf

#
do_upgrade_system_for_RH() {
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_glibc_openssl_upgrade_question"
echo "$msg_glibc_openssl_keep"
echo "$msg_keep_glibc_upgrade_openssl"
echo "$msg_upgrade_glibc_keep_openssl"
echo "$msg_upgrade_glibc_openssl"
echo "$msg_warning_glibc_upgrade"
echo "$msg_upgrade_glibc_make_you_cry"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[3] "
read RH_upgrade_option

echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_upgrade_whole_system"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"

# we must keep drbl 
APT_KEEP_OPT='-o RPM::Hold::=drbl'
YUM_KEEP_OPT='--exclude=drbl'
case "$RH_upgrade_option" in
  0)
     # keep glibc and openssl
     APT_KEEP_OPT="$APT_KEEP_OPT -o RPM::Hold::=openssl -o RPM::Hold::=glibc"
     YUM_KEEP_OPT="$YUM_KEEP_OPT --exclude=openssl --exclude=glibc --exclude=glibc-common --exclude=glibc-devel --exclude=glibc-headers"
     ;;
  1)
     # upgrade openssl, keep glibc
     APT_KEEP_OPT="$APT_KEEP_OPT -o RPM::Hold::=glibc"
     YUM_KEEP_OPT="$YUM_KEEP_OPT --exclude=glibc --exclude=glibc-common --exclude=glibc-devel --exclude=glibc-headers"
     ;;
  2) 
     # upgrade glibc, keep openssl
     APT_KEEP_OPT="$APT_KEEP_OPT -o RPM::Hold::=openssl"
     YUM_KEEP_OPT="$YUM_KEEP_OPT --exclude=openssl"
     ;;
  *)
     # upgrade glibc and openssl
     echo "We will upgrade glibc and openssl."
     ;;
esac

# do it
[ "$installer" = "apt" ] && apt-get dist-upgrade $APT_KEEP_OPT 
[ "$installer" = "yum" ] && yum $YUM_KEEP_OPT update
} # end of do_upgrade_system_for_RH

#
import_rpm_key() {
echo "$msg_delimiter_star_line"
case "$OS_Version" in
   FC*)
    # import GPG key if not imported.
    if ! rpm -qi --quiet gpg-pubkey-db42a60e-37ea5438; then
      echo -n "Importing the RPM-GPG-KEY from /usr/share/doc/fedora-release-${OS_Version:2}/RPM-GPG-KEY ..."
      rpm --import /usr/share/doc/fedora-release-${OS_Version:2}/RPM-GPG-KEY
      echo "done!"
    fi
    if ! rpm -qi --quiet gpg-pubkey-4f2a6fd2-3f9d9d3b; then
      echo -n "Importing the RPM-GPG-KEY-fedora from /usr/share/doc/fedora-release-${OS_Version:2}/RPM-GPG-KEY-fedora ..."
      rpm --import /usr/share/doc/fedora-release-${OS_Version:2}/RPM-GPG-KEY-fedora
      echo "done!"
    fi
    ;;
  CO*)
    if ! rpm -qi --quiet gpg-pubkey-443e1821-421f218f; then
      # /usr/share/doc/centos-release-4/RPM-GPG-KEY is all the same with
      # /usr/share/doc/centos-release-4/RPM-GPG-KEY-centos4
      # So we just use the one RPM-GPG-KEY-centos4
      CO_GPG_KEY="$(rpm -ql centos-release | grep -E "RPM-GPG-KEY-centos4$")"
      echo -n "Importing the RPM GPG KEY from $CO_GPG_KEY..."
      rpm --import $CO_GPG_KEY
      echo "done!"
    fi
    ;;
esac
} # end of import_rpm_key

#
do_upgrade_system_for_MDK() {
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_delimiter_star_line"
echo "$msg_upgrade_whole_system"
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

APT_KEEP_OPT='-o RPM::Hold::=drbl'
case "$installer" in
  "apt")
    apt-get upgrade $APT_KEEP_OPT
    ;;
  "urpmi")
    # backup the original skip.list
    [ -f /etc/urpmi/skip.list ] && mv -f /etc/urpmi/skip.list /etc/urpmi/skip.list.drblsave
    # generate the skip list, we want to keep drbl
    cat <<EOF > /etc/urpmi/skip.list
drbl
EOF
    urpmi --auto-select

    [ -f /etc/urpmi/skip.list.drblsave ] && mv -f /etc/urpmi/skip.list.drblsave /etc/urpmi/skip.list
    ;;
esac
} # end of do_upgrade_system_for_MDK

#
do_upgrade_system_for_DBN() {
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_upgrade_whole_system"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo "$msg_delimiter_star_line"
# do it
[ "$installer" = "apt" ] && apt-get -y dist-upgrade
} # end of do_upgrade_system_for_RH

# For Debian
install_drbl_DBN() {
# function to install Debian DRBL
# /etc/-release is like:
# DISTRIB_ID=Ubuntu
# DISTRIB_RELEASE=6.06
# DISTRIB_CODENAME=dapper
# DISTRIB_DESCRIPTION="Ubuntu 6.06 LTS"
[ -e /etc/lsb-release ] && . /etc/lsb-release
case "$OS_Version" in
  DBN-TU)
    # Ubuntu breezy, Debian Etch, Sid or...
    # Force to set LC_ALL=C, otherwise in Ubuntu, it will show a lot warning messages in debconf like
    # perl: warning: Setting locale failed.
    # perl: warning: Please check that your locale settings:
    # LANGUAGE = "zh_TW:zh:en_US:en",
    # LC_ALL = "zh_TW.Big5",
    # LANG = "zh_TW.UTF-8"
    # are supported and installed on your system.
    # perl: warning: Falling back to the standard locale ("C").
    export LC_ALL=C
    ;;
esac

# append the --force-yes since there is no key in drbl related package.
# Only Ubuntu/Etch will check key, not sarga or woody, but we just give it.
apt_extra_opt="--force-yes"

# Some checkings:
# It seems sarge running kernel 2.4 will have NFS problem...
case "$OS_Version" in
  DBN3.1)
    if [ -n "$(uname -r | grep -E "^2\.4\.*")" ]; then
      echo "Kernel 2.4 is too old in DRBL environment for Debian Sarge, you have to run kernel 2.6. Please install kernel 2.6 and use it. Program terminated!"
      exit 1
    fi
    ;;
esac
# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
[ ! -d "/tftpboot/nbi_img" ] && mkdir -p /tftpboot/nbi_img

# 1.
# User has to setup apt/yum first by them self, so skip this.

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# 
parse_client_archi_setting

# In Debian, the installer is apt.
inst_prog="apt-get"
inst_opt="-y $apt_extra_opt"
inst_action="install"

# 2.
# It's important to remove the cache of apt, otherwise maybe something like
# the RPM::Architecture will not take effect. check this:
# http://distro2.conectiva.com.br/pipermail/apt-rpm/2003-November/002015.html
echo "$msg_delimiter_star_line"
echo "$msg_clean_apt_cache"
for ifile in /var/cache/apt/*.bin; do
   rm -f /var/cache/apt/$ifile
done

# Update the apt data with repository.
$inst_prog update

# UGLY
obsolete_pkgs="mkinitrd-net partimage-server knoppix-terminalserver"
# mkinitrd-net is replaced by mkpxeinitrd-net in this version. Although 
# mkinitrd-net is set as one of the obsoletes in mkpxeinitrd-net.spec.
# However, we use alien to convert drbl rpm to drbl deb, the obsolete is not 
# converted. 
# Same for partimage-server (The partimage released by DRBL containing partimaged), knoppix-terminalserver
for i in $obsolete_pkgs; do
  if [ -n "$(dpkg -s $i 2>/dev/null | grep -Ei "^Status: install ok installed")" ]; then
    dpkg --purge $i
  fi
done

# Shall We try to upgrade system first.?
# Since there are many debian based distributions, it's might not be a good
# idea to upgrade the system. So let user decided.
echo "$msg_delimiter_star_line"
if [ -z "$upgrade_system_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_upgrade_system_question"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read upgrade_system_answer
fi
case "$upgrade_system_answer" in 
   y|Y|[yY][eE][sS])
      do_upgrade_system_for_DBN
      ;;
esac

# 3.
# Install the necessary packages for DRBL 
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_delimiter_star_line"
echo "$msg_install_DRBL_necesary_files"
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
# package from Debian
# mkinitrd is in initrd-tools
# dos2unix is in sysutils
# Supposed "udpcast" is put in the rpm spec's requirement of clonezilla. But
# Alien fails to make that deps, so we add here.
# We need gawk instead of mawk (the original awk)
packages_from_debian="initrd-tools dhcp3-server tftpd-hpa nfs-kernel-server nis ntp wget dialog rsync parted pciutils tcpdump bc grub curl lftp iptables sysutils gawk hdparm udpcast ssh libdigest-sha1-perl netcat file ethtool etherwake"
pkgs_to_query="lvm2"
# Check if lvm2 is available, if so, add it, we need this for clonezilla to
# save or restore LVM2 PV/VG/LV.
for ipkg in $pkgs_to_query; do
  if [ -n "$(apt-cache pkgnames $ipkg)" ]; then
    packages_from_debian="$packages_from_debian $ipkg"
  fi
done
# Detect if discover/discover1 is installed or not, if installed, 
# do not install it. If not, we need discover (not discover1) for clients. 
# The package name "discover" exists both in woody and sarge.
# The reason we have to check is that we prefer to keep the installed package, maybe discover or discover1.
if ! chk_deb_installed discover && ! chk_deb_installed discover1; then
   packages_from_debian="$packages_from_debian discover"
fi
# For Breezy, Etch, udev is necessary ? not hotplug ? udev and hotplug conflict.# For Breezy, Etch, we just won't touch the server, if hotplug is unstalled (breezy), ust that, if udev is installed (ethc), use that.
case "$OS_Version" in
  DBN3.1|DBN3.0)
    # Genuine Debian sarge/woody
    packages_from_debian="$packages_from_debian hotplug"
    ;;
esac

# package from DRBL,
# $packages_from_drbl will be loaded from conf/drbl.conf

# We need to preconfig some packages, such as dhcp3-server, tftpd-hpa, nis so
# it will not ask user to config. We will configure later in drblpush.
$DRBL_SCRIPT_PATH/sbin/deb-preconf-drbl

# Install them
$inst_prog $inst_opt $inst_action $packages_from_debian $packages_from_drbl

# Patch the /etc/init.d/tftp-hpa, the "set -e" should not be put otherwise
# if tftpd-hpa is not started, "/etc/init.d/tftpd-hpa restart" will just stop
# and fail, will not start it.
if [ -e /etc/init.d/tftpd-hpa ]; then
  perl -p -i -e "s/^[[:space:]]*set -e.*/#set -e/g" /etc/init.d/tftpd-hpa
fi

#
echo "$msg_delimiter_star_line"
# 4. dev for client machines
# For Debian woody & B2D pureKDE20050603..It's necessary to make one before run
# this... (Just create the tarball from some Debian machine)
# It's ugly here...
# The solution: Make udev as the 1st priority
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir

# try to install udev...
if ! chk_deb_installed udev; then
  echo "$msg_delimiter_star_line"
  echo "Try to install udev..."
  $inst_prog $inst_opt $inst_action udev &>/dev/null
fi
if ! chk_deb_installed udev; then
  # udev is not available, use tarball
  if [ -f $drbl_setup_path/files/$OS_Version/dev.$OS_Version.tgz ]; then
    echo "Udev is not available, use pre-packaged device files."
    cp -f $drbl_setup_path/files/$OS_Version/dev.$OS_Version.tgz $drbl_pkgdir/dev.tgz
  else
    [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
    echo "No udev, and no pre-packaged device files!!! Program terminated!"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    exit 1
  fi
fi

# 5.
echo "$msg_delimiter_star_line"
# Force to install all the kernel selected.
echo "$msg_search_kernel_in_ayo ${SMP_OPTION:1} kernel ..."

# Install the kernel
# Note! This must be done after gawk is installed, since we need gawk to parse
# the latest kernel.
if [ "$client_archi_set" = "best_optimization" ]; then
  # same CPU archi with server,  maybe for i686, or i386 (for AMD K7 ?)
  KARCH_LIST="$($DRBL_SCRIPT_PATH/bin/check_kernel_cpu_arch `uname -r`)"
  # KARCH_LIST will be nothing if we can not find the kernel config file, so we choose to use the same arch of server for client.
  [ -z "$KARCH_LIST" ] && KARCH_LIST="$(uname -m)"
  # convert the arch name from server to client, 
  # This is the lists for Debian Sarge:
  # -----------------------------------------------
  # kernel-image-2.6.8-3-686-smp 2.6.8-16sarge2
  # kernel-image-2.6.8-3-686 2.6.8-16sarge2
  # kernel-image-2.6.8-3-386 2.6.8-16sarge2
  # kernel-image-2.6.8-2-k7-smp 2.6.8-16sarge1
  # kernel-image-2.6.8-2-k7 2.6.8-16sarge1
  # kernel-image-2.6.8-12-em64t-p4-smp 2.6.8-16sarge2
  # kernel-image-2.6.8-12-em64t-p4 2.6.8-16sarge2
  # kernel-image-2.6.8-12-amd64-k8-smp 2.6.8-16sarge2
  # kernel-image-2.6.8-12-amd64-k8 2.6.8-16sarge2
  # kernel-image-2.6.8-12-amd64-generic 2.6.8-16sarge2
  # kernel-image-2.6.8-11-em64t-p4-smp 2.6.8-16sarge1
  # kernel-image-2.6.8-11-em64t-p4 2.6.8-16sarge1
  # kernel-image-2.6.8-11-amd64-k8-smp 2.6.8-16sarge1
  # kernel-image-2.6.8-11-amd64-k8 2.6.8-16sarge1
  # kernel-image-2.6.8-11-amd64-generic 2.6.8-16sarge1
  # -----------------------------------------------
  # i386/i486/i586 -> i686 since only i686-smp and k7-smp exists in Debian
  # For k7, keep what it is.
  # If found nothing, let KARCH_LIST=i686
  # TODO: How about em64t ? check_kernel_cpu_arch can show correct messages ?
  case "$KARCH_LIST" in
  i[0-9]86)
     KARCH_LIST="i686"
     # skip the leading "i"
     client_archi=${KARCH_LIST:1}
     ;;
  x86_64)
     KARCH_LIST="amd64-k8"
     client_archi=${KARCH_LIST}
     ;;
  *)
     KARCH_LIST="i686"
     # skip the leading "i"
     client_archi=${KARCH_LIST:1}
     ;;
  esac
else
  # For Debian, only i386 is available we decided. Maybe in the future, we 
  # will add K7 ?
  # For Debian Etch/Ubuntu Dapper... the kernel is like linux-image-2.6.15-1-486, only 486
  KARCH_LIST="i[34]86"
  # skip the leading "i"
  client_archi=${KARCH_LIST:1}
fi
cpu_filter="grep -iE $client_archi"

if [ -n "$SMP_OPTION" ]; then
  # enable SMP filter
  smp_filter="grep -i smp"
else
  # "NOT SMP" filter
  smp_filter="grep -iv smp"
fi

# Ugly!!!
# There is no SMP tag kernel image in Ubuntu Dapper, so we force to set not smp
if [ -n "$SMP_OPTION" -a "$DISTRIB_ID" = "Ubuntu" -a "$DISTRIB_RELEASE" = "6.06" ]; then
  echo "Since all the kernel image in Ubuntu Dapper supports SMP, we do not have to specify the kernel with SMP tag for clients."
  smp_filter="grep -iv smp"
fi

# The kernel image name: linux-iamge (such as Etch) or kernel-image (such as Sarge)
for ikmg in linux-image kernel-image; do
  latest_drbl_kernel_in_rep="$(apt-cache pkgnames $ikmg | grep -E "$ikmg-[0-9]+\.[0-9]+\.[0-9]+-.*" | $cpu_filter | $smp_filter | $DRBL_SCRIPT_PATH/bin/pkg-ver-latest)"
  if [ -n "$latest_drbl_kernel_in_rep" ]; then
     kernel_img_name="$ikmg"
     break
  fi
done
echo "$msg_latest_kernel_in_ayo $latest_drbl_kernel_in_rep"

# Get the selected kernel arch (i686/i386/i486)
# Ex:
# linux-image-2.6.12-10-686 -> 686
# linux-image-2.6.12-10-686-smp -> 686
# linux-image-2.6.12-10-amd64-xeon -> x86_64
# linux-image-2.6.12-10-amd64-k8-smp -> x86_64
# linux-image-2.6.12-10-amd64-k8 -> x86_64
# linux-image-2.6.12-10-amd64-generic -> x86_64
# For Etch or Dapper, there is an issue in Dapper, check
# https://lists.ubuntu.com/archives/ubuntu-devel/2006-May/018185.html
# for more details.
# linux-image-2.6.16-1-486_2.6.16-12_i386.deb (Etch) -> 486, there is a "486", not "386", to show this is for i486 arch.
# linux-image-2.6.15-23-386_2.6.15-23.39_i386.deb (Dapper beta) -> the name implies 386, but actually it's 486... Ha...
case "$latest_drbl_kernel_in_rep" in
  *-[0-9]86*)
    KARCH="$(echo $latest_drbl_kernel_in_rep | sed -e "s/.*-\([0-9]86\).*$/i\1/" | sed -e "s/-smp//g")"
    ;;
  *-amd64-*|*-em64t-*)
    KARCH=x86_64
    ;;
esac

# UGLY!!! For Ubuntu Dapper, about the issue mentioned above, we have to use the ugly rule! Since altthough the name is 386, but actually it's 486.
if [ "$KARCH" = "i386" -a \
     "$DISTRIB_ID" = "Ubuntu" -a \
     "$DISTRIB_RELEASE" = "6.06" ]
then
  KARCH="i486"
fi

# Another available kernel maybe is from server, check it:
rununig_karch="$($DRBL_SCRIPT_PATH/bin/check_kernel_cpu_arch `uname -r`)"
running_kernel_in_server="$(echo $(uname -r) $rununig_karch | $cpu_filter | $smp_filter)"

use_kernel_from=
if [ -n "$running_kernel_in_server" ]; then
  # Server running kernel matches client's requirement
  if [ -z "$chosen_client_kernel_from" ]; then
    echo "$msg_2_kernel_availables"
    echo "[1]: kernel $running_kernel_in_server ($msg_from_this_drbl_server)"
    echo "[2]: $latest_drbl_kernel_in_rep ($msg_from_apt_repository)"
    echo -n "[1] "
    read chosen_client_kernel_from
  fi
  case "$chosen_client_kernel_from" in
    2)
      echo "Clients will use the kernel $latest_drbl_kernel_in_rep from apt repository."
      use_kernel_from="apt-repository"
      ;;
    *)
      echo "Clients will use the kernel $running_kernel_in_server from server."
      use_kernel_from="local"
      ;;
  esac
else
  # Use the kernel in the repository
  echo "Use the kernel $latest_drbl_kernel_in_rep from apt repository!"
  use_kernel_from="apt-repository"
fi

rc_install_kernel=1
case "$use_kernel_from" in
  "apt-repository")
    # get the version and release of kernel, strip the kernel[#-] or kernel-smp[#-]
    # $drbl_kernel_ver will be like "2.6.8-2-386", "2.6.8-2-686", or "2.6.8-2-686-smp"
    drbl_kernel_ver="$(echo $latest_drbl_kernel_in_rep | sed -e s/$kernel_img_name-//g)"
    if [ -z "$drbl_kernel_ver" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "NO kernel $kernel_img_name is available!!! Something went wrong!"
      echo "You can run this program $0 again, and try to choose different CPU architecture (such as i386, i686, no SMP...)!"
      echo "Program terminated!!!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      exit 1
    fi
    
    # Before download it, clean the kernel-image in the cache dir so that we can fin
    # the download one by ls.
    rm -f /var/cache/apt/archives/${latest_drbl_kernel_in_rep}*.deb 2>/dev/null
    
    # download it
    apt-get $inst_opt --reinstall --download-only install $latest_drbl_kernel_in_rep
    rc_get_kernel=$?
    if [ $rc_get_kernel -gt 0 ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "$latest_drbl_kernel_in_rep can NOT be downloaded!!! Something went wrong!"
      echo "Maybe you should check /etc/sources.list or remove the unnecessary kernel block in /var/lib/dpkg/status so that "apt-cache pkgnames $kernel_img_name" can get the downloadable kernel! Then try to run this program again."
      echo "Program terminated!!!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      exit 1
    fi
    
    # The "_" is important, since the file names in the cache dir are like
    # kernel-image-2.6.8-2-386_2.6.8-16_i386.deb, 
    # kernel-image-2.6.8-2-686_2.6.8-16_i386.deb, 
    # kernel-image-2.6.8-2-686-smp_2.6.8-16_i386.deb
    if [ -n "$(ls /var/cache/apt/archives/${latest_drbl_kernel_in_rep}_*.deb 2>/dev/null)" ]; then
      latest_drbl_kernel_in_rep_deb="$(ls /var/cache/apt/archives/$latest_drbl_kernel_in_rep*.deb 2>/dev/null)"
      echo -n "Extracting the kernel $latest_drbl_kernel_in_rep_deb into $drbl_common_root..."
      dpkg-deb --extract $latest_drbl_kernel_in_rep_deb $drbl_common_root
      rc_install_kernel=$?
      echo "done!"
    fi
    ;;
  "local")
    echo "$msg_install_kernel_might_take_several_minutes"
    drbl_kernel_ver="$(uname -r)"
    mkdir -p $drbl_common_root/boot/ $drbl_common_root/lib/modules/
    rsync -a /boot/*$drbl_kernel_ver $drbl_common_root/boot/
    rsync -a /lib/modules/$drbl_kernel_ver $drbl_common_root/lib/modules/
    echo "$msg_done!"
    rc_install_kernel=0
    ;;
esac

# once it's done, skip the next search
if [ "$rc_install_kernel" -eq 0 ]; then
  # make a tag in the system for client's kernel arch.
  echo "$KARCH" > $pxecfg_pd/client_kernel_arch.txt
else
  [ -n "$verbose" ] && echo "$latest_drbl_kernel_in_rep_deb is not installed successfully!!!"
fi

echo "$msg_delimiter_star_line"
# 6.
# ugly...might be not my fault...
# for some distribution some version, we have to force upgrading some packages.
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_try_to_update_some_pkgs"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
case "$OS_Version" in
  DBN3.0)
    # We need "grep -o", and the grep in woody does not have that option.
    # so use the one from backports.
    echo "$msg_OS_version: $OS_Version."
    echo "$msg_force_to_upgrade_some_pkgs"
    apt-get $inst_opt $inst_action grep
    ;;
esac

# 7.
# netinstall
if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_install_net_install_images"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  $inst_prog $inst_opt install $net_install_pkgs
  # TODO
  RETVAL=$?
  check_apt_install $RETVAL
fi

# 8.
# make the nbi/pxe files
create_pxe_nbi_files

# 9.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_create_files_for_PXELINUX"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
create_files_for_PXELINUX

echo "$msg_delimiter_star_line"
echo "$msg_Done!"

} # end of install_drbl_DBN

#
install_drbl_SUSE() {
# function to install SuSE Linux
# Now the installer is only apt, maybe someday we will find a way to use yast.
# Note: Here we use apt instead of "apt-get" since apt can accept "--no-checksig"
case "$installer" in
  "yum")
        inst_prog="yum"
        inst_opt="-y"
        inst_action="install"
              ;;
  *)
        # Default is apt
        inst_prog="apt"
        inst_opt="-y --no-checksig $apt_extra_opt"
        inst_action="install"
        ;;
esac

# Check the running kernel
echo "$msg_delimiter_star_line"
# ugly...might be not my fault...
# for some distribution some version, we have to force upgrading some packages.

# SuSE/OpenSuSE 10.0 with kernel 2.6.13-15-default is BUGGY 
# in DRBL server!!! Since it's a kernel upgrade, we wont' do that for
# user, just prompt them
if [ "$(uname -r)" = "2.6.13-15-default" ]; then
  echo "$msg_OS_version: $OS_Version."
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "$msg_kernel_2_6_13_15_defaul_is_buggy."
  echo "$msg_u_have_to_upgrade_kernel_in_this_machine"
  case "$ARCH" in
    i386)
        echo "rpm -ivh http://free.nchc.org.tw/SuSE/SuSE/i386/update/10.0/rpm/i586/kernel-default-2.6.13-15.7.i586.rpm"
        ;;
    x86_64)
        echo "rpm -ivh http://free.nchc.org.tw/SuSE/SuSE/i386/update/10.0/rpm/x86_64/kernel-default-2.6.13-15.7.x86_64.rpm"
        ;;
  esac
  echo "$msg_then_reboot_this_server"
  echo "Ref: https://bugzilla.novell.com/show_bug.cgi?id=128784"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_program_stop"
  exit 1
fi

# check if installer exists
if [ "$installer" = "apt" ] && ! rpm -q apt &>/dev/null; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo "You must install apt4rpm first in this SuSE server!"
  echo "Check http://linux01.gwdg.de/apt4rpm for more details!"
  echo "Or just download http://linux01.gwdg.de/~scorot/install_apt4suse, then run it."
  echo "Remeber to add the following line in /etc/apt/sources.list"
  echo "rpm http://free.nchc.org.tw/drbl-core i386 drbl-unstable drbl-testing drbl-stable"
  echo "Note: drbl-unstable or drbl-testing is optional, use at your own risk!"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  exit 1
fi
if [ "$installer" = "yum" ] && ! rpm -q yum &>/dev/null; then
  # From SuSE 10.0, yum is available
  if [ "$OS_Version" != "SUSE9.3" ]; then 
    echo "$msg_delimiter_star_line"
    echo "Try to install yum first..."
    /sbin/yast2 -i yum
    echo "$msg_delimiter_star_line"
    if ! rpm -q yum &>/dev/null; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "You must manually install yum first in this SuSE server!"
      echo "Use yast2 to install that, and remember to set the yum repository in /etc/yum.repos.d/"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      exit 1
    fi
  fi
fi

# clean the stale files and make necessary directories
[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
# create the directory for diskless image
for it in $drbl_common_root $pxecfg_pd; do
  [ ! -d "$it" ] && mkdir -p $it
done

# setup yum respository
if [ "$installer" = "yum" ]; then
  # remove the old repo file, since 1.6.4-7, we will use opensuse-${OS_VER_NO}.repo instead of opensuse.repo
  [ -f /etc/yum.repos.d/opensuse.repo ] && rm -f /etc/yum.repos.d/opensuse.repo
  if [ ! -f /etc/yum.repos.d/drbl.repo -o ! -f /etc/yum.repos.d/opensuse-${OS_VER_NO}.repo ]; then
    echo "Try to backup the original yum repository settings if they exist..."
    # we won't touch the existing one, just copied by drbl.
    repo_lists="drbl.repo drbl-testing.repo drbl-unstable.repo drbl-extra.repo opensuse-${OS_VER_NO}.repo opensuse-updates-${OS_VER_NO}.repo"
    for ifile in $repo_lists; do
      [ -f "/etc/yum.repos.d/$ifile" ] && mv -v /etc/yum.repos.d/$ifile /etc/yum.repos.d/$ifile.drblsave
    done
    cp -f $DRBL_SCRIPT_PATH/setup/yum-repos/{drbl*.repo,opensuse*-${OS_VER_NO}.repo} /etc/yum.repos.d/
  fi
  # ask some RPM repository options, such as drbl-testing, drbl-unstable
  ask_and_parse_rpm_ayo_repo_options
  # turn on some branches if set
  [ -n "$DRBL_TEST" ] && perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-testing.repo
  [ -n "$DRBL_UNSTABLE" ] && perl -pi -e "s/^[[:space:]]*enabled=.*/enabled=1/gi" /etc/yum.repos.d/drbl-unstable.repo
fi

# ask some setting for client, such as SMP, serial console...
ask_and_parse_client_extra_setting

# parse the setting of client archi.
parse_client_archi_setting

# It's important to remove the cache of apt, otherwise 
# the RPM::Architecture will not take effect. check this:
# http://distro2.conectiva.com.br/pipermail/apt-rpm/2003-November/002015.html
# ... RPM::Architecture, is what he's looking for. His problem was that the cache was built before he set the architecture. Doing "rm -f /var/cache/apt/*.bin; apt-get update" would make it work.
if [ "$installer" = "apt" ]; then
  echo "$msg_clean_apt_cache"
  $inst_prog clean
  for ifile in /var/cache/apt/*.bin; do
     rm -f /var/cache/apt/$ifile
  done
fi

# If user specifies the client CPU arch is i586, we must put the setting in
# apt so that different arch program will NOT be upgraded.
if [ "$installer" = "apt" ]; then
  if [ "$client_archi_set" = "i586" ]; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "Setting the \"Architecture "i586"\" in apt config file /etc/apt/apt.conf.d/drbl-arch.conf."
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    cat <<-APTCFG_END > /etc/apt/apt.conf.d/drbl-arch.conf
// necessary for DRBL client
RPM
{
   Architecture "i586";
}
APTCFG_END
  fi
fi

# Update the apt data with repository.
# if it's yum, skip this, later yum will do it. Because yum update will also
# do upgrade...
[ "$installer" = "apt" ] && $inst_prog update

# 2.
# check if apt is integrity... Ex: original MDK 9.2 is buggy...
if [ "$installer" = "apt" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_check_apt_integrity"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  check_apt_integrity
fi

echo "$msg_delimiter_star_line"
# 3.
# We will upgrade system first.
# Try to find if the i386 glibc and openssl are installed or not
# If they are installed, hold them not to be upgraded to i686
if [ -z "$upgrade_system_answer" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_upgrade_system_question"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "[y/N] "
  read upgrade_system_answer
fi

case "$upgrade_system_answer" in 
   y|Y|[yY][eE][sS])
      [ "$installer" = "apt" ] && $inst_prog $inst_opt dist-upgrade
      [ "$installer" = "yum" ] && yum upgrade
      ;;
esac

# 4.
# lftp & curl are necessary here, we install it first
if ! rpm -q --quiet lftp || ! rpm -q --quiet curl || ! rpm -q --quiet wget ; then
  echo "lftp and curl are necessary so that we can find some program in apt or yum repository. Install them now if not installed..."
  # we need lftp to parse the repo setting if it's in ftp://
  if [ "$ARCH" = "x86_64" -a "$installer" = "yum" ]; then
    lftp_curl_etc="lftp.x86_64 curl.x86_64 wget.x86_64"
  else
    lftp_curl_etc="lftp curl wget"
  fi
  $inst_prog $inst_opt install $lftp_curl_etc
fi
# After the dist upgrade, check the ARCH for glibc, db... which are different 
# from i586 and are found in ftp://ftp.twaren.net/Linux/SuSE/i386/9.3/suse/i686
for ipkg in $i686_pkg_check_list_SUSE; do
  # i686_pkg_check_list_SUSE is: glibc glibc-devel db db-devel...
  # since "-" can not ba a variable name, replace it as "_"
  ipkg_arch_var="$(echo $ipkg | sed -e "s/-/_/g")"
  rpm -q --qf '%{ARCH}' $ipkg > /dev/null && eval ${ipkg_arch_var}_ARCH="$(rpm -q --qf '%{ARCH}' $ipkg)"
done
# We need to get url_os os_ayo_path url_os_coreroot if it's yum.
if [ "$installer" = "yum" ]; then
  url_tmp="$($DRBL_SCRIPT_PATH/bin/find-url-in-yum-set /etc/yum.repos.d/opensuse-${OS_VER_NO}.repo $OS_VER_NO $ARCH 2>/dev/null)"
  if [ -n "$url_tmp" ]; then
    url_os="$(echo $url_tmp | awk -F' ' '{print $1}')"
    os_ayo_path="$(echo $url_tmp | awk -F' ' '{print $2}')"
    url_os_coreroot=$url_os/$os_ayo_path
    echo "$msg_delimiter_star_line"
    echo "Use yum repository in $url_os_coreroot"
    echo "$msg_delimiter_star_line"
  fi
  url_update_tmp="$($DRBL_SCRIPT_PATH/bin/find-url-in-yum-set /etc/yum.repos.d/opensuse-updates-${OS_VER_NO}.repo $OS_VER_NO $ARCH 2>/dev/null)"
  if [ -n "$url_update_tmp" ]; then
    # Ex:
    # url_updates_root will be like:
    # http://opensource.nchc.org.tw/suse/SuSE/update
    # Then the updates RPM will be in http://opensource.nchc.org.tw/suse/SuSE/update/10.0/rpm/i586/
    url_updates="$(echo $url_update_tmp | awk -F' ' '{print $1}')"
    updates_ayo_path="$(echo $url_update_tmp | awk -F' ' '{print $2}')"
    url_updates_root=$url_updates/$updates_ayo_path
    echo "$msg_delimiter_star_line"
    echo "Use yum updates repository in $url_updates_root"
    echo "$msg_delimiter_star_line"
  fi
fi

install_client_archi_glibc_etc_SUSE

# 5.
echo "$msg_delimiter_star_line"
# Force to install all the kernel selected.
echo "$msg_search_kernel_in_ayo kernel${SMP_OPTION} ..."

install_client_kernel_in_SUSE

# 6.
# wget maybe not installed, we will check it is installed only when apt is not installed, so if apt/yum is installed while wget is not installed, get_inst_rpm() will failed! Add wget here.
# Note, for SUSE, nfs-utils is kernel nfs server, nfs-server is user space.
PKG_FROM_SUSE="dhcp-server tftp nfs-utils ypserv ypbind yp-tools mkinitrd xntp iptables wget dialog rsync parted tcpdump reiserfs bc grub dos2unix ddrescue curl lftp openssh insserv coreutils gzip bzip2 netcat file ethtool netdiag"
pkgs_to_query="lvm2"
echo -n "Searching if $pkgs_to_query available... "
case "$installer" in
  yum)
     for ipkg in $pkgs_to_query; do
       if [ -n "$(yum list $ipkg |grep -E "^$ipkg")" ]; then
         echo -n "Found $ipkg package in repository..."
         PKG_FROM_SUSE="$PKG_FROM_SUSE $ipkg"
       fi
     done
     ;;
  apt)
     for ipkg in $pkgs_to_query; do
       if [ -n "$(apt-cache pkgnames $ipkg)" ]; then
         echo -n "Found $ipkg package in repository..."
         PKG_FROM_SUSE="$PKG_FROM_SUSE $ipkg"
       fi
     done
     ;;
esac
echo " done!"

PKG_FROM_DRBL="mkpxeinitrd-net etherboot partimage freedos clonezilla drbl-ntfsprogs lzop"
# Since the repodata in yum repository of opensuse include all the arch, they do not separate them, so we have to put the specific arch, especially for x86_64. Otherwise the i586 will be always the first priority, that will case the problem:
# http://sourceforge.net/mailarchive/forum.php?thread_id=9549930&forum_id=41969
if [ "$ARCH" = "x86_64" -a "$installer" = "yum" ]; then
    PKG_TO_INSTALL=""
    for ipkgs in $PKG_FROM_SUSE; do
      PKG_TO_INSTALL="$PKG_TO_INSTALL ${ipkgs}.x86_64"
    done
else
    PKG_TO_INSTALL="$PKG_FROM_SUSE"
fi
$inst_prog $inst_opt install $PKG_TO_INSTALL $PKG_FROM_DRBL

# TODO
RETVAL=$?
check_apt_install $RETVAL

# 7.
# Creat dev tarball in $drbl_pkgdir,
# which will be copied to /tftpboot/node_root/etc/diskless-image 
# in deploy script
[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir

echo "$msg_delimiter_star_line"
# get dev
echo "The DRBL client will use udev..."
# we must install udev...
if ! rpm -q --quiet udev; then
  echo -n "Try to install udev..."
  $inst_prog $inst_opt install udev
  echo "done!"
fi
if ! rpm -q --quiet udev; then
  echo "No udev, and no pre-packaged devices file!!! Program terminated!"
  exit 1
fi

# 8.
# netinstall
if [ "$DRBL_NETINSTALL" = "yes" ]; then
  echo "$msg_delimiter_star_line"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_install_net_install_images"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo "$msg_delimiter_star_line"
  $inst_prog $inst_opt install $net_install_pkgs
  # TODO
  RETVAL=$?
  check_apt_install $RETVAL
fi

# 9.
# make the nbi/pxe files
create_pxe_nbi_files

# 10.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_create_files_for_PXELINUX"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
create_files_for_PXELINUX

echo "$msg_delimiter_star_line"
echo "$msg_Done!"
} # end of install_drbl_SUSE

install_curl_etc_via_yum_if_necessary() {
  # we need $url_os, "$os_ayo_path"and "$url_os_coreroot for kernel and dev
  # try to parse the yum config in the system
  # We will search CentOS-base.repo, $FC_CORE_REPO in /etc/yum.repos.d/
  # TODO: maybe search all the *.repo in /etc/yum.repos.d/ ?
  echo "$msg_delimiter_star_line"
  echo "Try to find yum repository url..."
  for i in CentOS-Base fedora-core fedora; do
    if [ -f "/etc/yum.repos.d/${i}.repo" ]; then
      if ! rpm -q --quiet curl || ! rpm -q --quiet lftp || ! rpm -q --quiet wget; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	echo "$msg_need_curl_etc_to_parse_config"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	# we need lftp to parse the yum repo config if the repo is in ftp://
	$installer -y install lftp curl wget
      fi
      # Once we have curl, we can get url_os, os_ayo_path...
      if [ "$i" = "CentOS-Base" ]; then
        # CentOS 4.3 use the new method to get the mirrorlist system.
	# Ref: http://lists.centos.org/pipermail/centos/2006-April/063312.html
	# We can only query releasever=4 for 4.3
	# Since earlier version 4.2/4.1 is no more, forget about them.
	# Actually, for CentOS, the latest version in 4.x is linked to 4, so use 4 is the latest release.
        QUERY_OS_VER="$(echo ${OS_Version:2} | sed -e "s/\.[[:digit:]]*//g")"
      else
        QUERY_OS_VER="${OS_Version:2}"
      fi
      url_tmp="$($DRBL_SCRIPT_PATH/bin/find-url-in-yum-set /etc/yum.repos.d/${i}.repo $QUERY_OS_VER $ARCH 2>/dev/null)"
      if [ -n "$url_tmp" ]; then
        url_os="$(echo $url_tmp | awk -F' ' '{print $1}')"
        os_ayo_path="$(echo $url_tmp | awk -F' ' '{print $2}')"
        url_os_coreroot=$url_os/$os_ayo_path
        echo "$msg_delimiter_star_line"
        echo "Use yum repository in $url_os_coreroot"
        check_if_repository_in_blacklist $url_os
	break
      fi
    fi
  done
} # end of install_curl_etc_via_yum_if_necessary
#
install_curl_etc_via_apt_if_necessary() {
  # we need $url_os, "$os_ayo_path"and "$url_os_coreroot for kernel and dev
  echo "$msg_delimiter_star_line"
  if ! rpm -q --quiet curl || ! rpm -q --quiet lftp || ! rpm -q --quiet wget; then
    [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
    echo "$msg_need_curl_etc_to_parse_config"
    [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
    apt-get update
    # we need lftp to parse the repo config if the repo is in ftp://
    # wget is already installed when setup apt, but who cares, add it in case
    apt-get -y install lftp curl wget
  fi
} # end of install_curl_etc_via_apt_if_necessary

#
install_client_kernel_in_SUSE() {
# Install the kernel of clients in SuSE
#
if [ "$client_archi_set" = "best_optimization" ]; then
  # same CPU archi with server,  maybe for i686, or i386 (for AMD K7 ?)
  [ -z "$KARCH" ] && KARCH="$(uname -m)"
  KARCH="$($DRBL_SCRIPT_PATH/bin/check_kernel_cpu_arch `uname -r`)"
  # KARCH will be nothing if we can not find the kernel config file, so we choose to use the same arch of server for client.
  [ -z "$KARCH" ] && KARCH="$(uname -m)"
  # convert the arch name from server to client, 
  # Note:
  # "KARCH_RPM_URL_PATH" is for RPM repository PATH use only, like 
  # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/i586/
  # of
  # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/x86_64/
  case "$KARCH" in
  i[0-9]86)
     KARCH="i686"
     # skip the leading "i"
     client_archi=${KARCH:1}
     KARCH_RPM_URL_PATH="i586"
     ;;
  x86_64)
     KARCH="x86_64"
     client_archi=${KARCH}
     KARCH_RPM_URL_PATH="x86_64"
     ;;
  *)
     KARCH="i686"
     # skip the leading "i"
     client_archi=${KARCH:1}
     KARCH_RPM_URL_PATH="i586"
     ;;
  esac
else
  # For SuSE, only i586 is available we decided. Maybe in the future, we 
  # will add K7 ?
  KARCH="i586"
  # skip the leading "i"
  client_archi=${KARCH:1}
  KARCH_RPM_URL_PATH="i586"
fi
cpu_filter="grep -i $client_archi"
if [ -n "$SMP_OPTION" ]; then
  # enable SMP filter
  smp_filter="grep -i smp"
  kernel_img_name="kernel-smp"
else
  # "NOT SMP" filter
  smp_filter="grep -iv smp"
  kernel_img_name="kernel-default"
fi
# Since there is only i586 kernel for SuSE, we do not have to filter KARCH here
if [ "$installer" = "apt" ]; then
  latest_drbl_kernel_in_rep="$(apt-cache pkgnames $kernel_img_name | grep -E "\-default$" | $DRBL_SCRIPT_PATH/bin/pkg-ver-latest)"
elif [ "$installer" = "yum" ]; then
  # Find the latest kernel in updates first (like http://opensource.nchc.org.tw/suse/SuSE/update/10.0/rpm/i586/)
  latest_drbl_kernel_in_rep="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_updates_root/$OS_VER_NO/rpm/$KARCH_RPM_URL_PATH/ | grep -E "${kernel_img_name}-[0-9]+" | $DRBL_SCRIPT_PATH/bin/pkg-ver-latest)"
  if [ -n "$latest_drbl_kernel_in_rep" ]; then
    # Keep the url, we will use it to download
    suse_latest_kernel_url="$url_updates_root/$OS_VER_NO/rpm/$KARCH_RPM_URL_PATH/$latest_drbl_kernel_in_rep"
  elif [ -z "$latest_drbl_kernel_in_rep" ]; then
    # If we can not find it in updates, use the one in release (like http://opensource.nchc.org.tw/opensuse/distribution/SL-10.0-OSS/inst-source/suse/i586/)
    latest_drbl_kernel_in_rep="$($DRBL_SCRIPT_PATH/bin/list_available_rpm $url_os_coreroot/$OS_PATH_IN_YUM_REPO/inst-source/suse/$KARCH_RPM_URL_PATH/ | grep -E "${kernel_img_name}-[0-9]+" | $DRBL_SCRIPT_PATH/bin/pkg-ver-latest)"
    suse_latest_kernel_url="$url_os_coreroot/$OS_PATH_IN_YUM_REPO/inst-source/suse/$KARCH_RPM_URL_PATH/$latest_drbl_kernel_in_rep"
  fi
fi
echo "$msg_latest_kernel_in_ayo $latest_drbl_kernel_in_rep"

# Another available kernel maybe is from server, check it:
rununig_karch="$($DRBL_SCRIPT_PATH/bin/check_kernel_cpu_arch `uname -r`)"
running_kernel_in_server="$(echo $(uname -r) $rununig_karch | $cpu_filter | $smp_filter)"

use_kernel_from=
if [ -n "$running_kernel_in_server" ]; then
  # Server running kernel matches client's requirement
  if [ -z "$chosen_client_kernel_from" ]; then
    echo "$msg_2_kernel_availables"
    echo "[1]: kernel $running_kernel_in_server ($msg_from_this_drbl_server)"
    echo "[2]: $latest_drbl_kernel_in_rep ($msg_from_ayo_repository)"
    echo -n "[1] "
    read chosen_client_kernel_from
  fi
  case "$chosen_client_kernel_from" in
    2)
      echo "Clients will use the kernel $latest_drbl_kernel_in_rep from ayo repository."
      use_kernel_from="ayo-repository"
      ;;
    *)
      echo "Clients will use the kernel $running_kernel_in_server from server."
      use_kernel_from="local"
      ;;
  esac
else
  # Use the kernel in the repository
  echo "Use the kernel $latest_drbl_kernel_in_rep from ayo repository!"
  use_kernel_from="ayo-repository"
fi

case "$use_kernel_from" in
  "ayo-repository")
    drbl_kernel_ver=
    case "$installer" in
     apt)
      # Before download it, clean the kernel-image in the cache dir so that we can fin
      # the download one by ls.
      rm -f /var/cache/apt/archives/${latest_drbl_kernel_in_rep}*.rpm 2>/dev/null
      
      # download it
      $inst_prog $inst_opt --reinstall --download-only install $latest_drbl_kernel_in_rep
      RC=$?
      if [ $RC -gt 0 ]; then
        [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
        echo "$latest_drbl_kernel_in_rep can NOT be downloaded!!! Something went wrong!"
        echo "Maybe you should check /etc/sources.list so that "apt-cache pkgnames $kernel_img_name" can get the downloadable kernel! Then try to run this program again."
        echo "Program terminated!!!"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
        exit 1
      fi
      
      # The "_" is important, since the file names in the cache dir are like
      # kernel-default_2.6.11.4-21.7_i586.rpm
      if [ -n "$(ls /var/cache/apt/archives/${latest_drbl_kernel_in_rep}_*.rpm 2>/dev/null)" ]; then
        latest_drbl_kernel_in_rep_rpm="$(ls /var/cache/apt/archives/$latest_drbl_kernel_in_rep*.rpm 2>/dev/null)"
        rc=0
        for ik in $latest_drbl_kernel_in_rep_rpm; do
          echo -n "Extracting the kernel $ik into $drbl_common_root..."
          cd $drbl_common_root && rpm2cpio $ik | cpio -idmu
          rc=$((rc + $?))
          # get the version and release of kernel,
          # $drbl_kernel_ver will be like "2.6.8-2-386", "2.6.8-2-686", or "2.6.8-2-686-smp"
          # maybe there are more than 1 kernel image, in SuSE, they are:
          # kernel-default_2.6.11.4-21.7_i586.rpm
          # kernel-default-nongpl_2.6.11.4-21.7_i586.rpm
          if [ -z "$drbl_kernel_ver" ]; then
            drbl_kernel_ver="$(rpm -qpl $ik | grep -E "^\/boot\/config-[0-9]*.*" | sed -e "s|/boot/config-||g")"
          fi
        done
        echo "done!"
      fi
      ;;
     yum)
      krn_dwn_tmp=`mktemp -d /tmp/SuSE_RPM.XXXXXXX`
      echo "Downloading the kernel from $suse_latest_kernel_url..."
      wget --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$krn_dwn_tmp" $suse_latest_kernel_url
      if [ -f "$krn_dwn_tmp/$latest_drbl_kernel_in_rep" ]; then
        latest_drbl_kernel_in_rep_rpm="$krn_dwn_tmp/$latest_drbl_kernel_in_rep"
        rc=0
        for ik in $latest_drbl_kernel_in_rep_rpm; do
          echo -n "Extracting the kernel $ik into $drbl_common_root..."
          cd $drbl_common_root && rpm2cpio $ik | cpio -idmu
          rc=$((rc + $?))
          # get the version and release of kernel,
          # $drbl_kernel_ver will be like "2.6.8-2-386", "2.6.8-2-686", or "2.6.8-2-686-smp"
          # maybe there are more than 1 kernel image, in SuSE, they are:
          # kernel-default_2.6.11.4-21.7_i586.rpm
          # kernel-default-nongpl_2.6.11.4-21.7_i586.rpm
          if [ -z "$drbl_kernel_ver" ]; then
            drbl_kernel_ver="$(rpm -qpl $ik | grep -E "^\/boot\/config-[0-9]*.*" | sed -e "s|/boot/config-||g")"
          fi
        done
        echo "done!"
      fi
      [ -d "$krn_dwn_tmp" ] && rm -rf $krn_dwn_tmp
      ;;
    esac  
    # Check if the $drbl_kernel_ver exists
    if [ -z "$drbl_kernel_ver" ]; then
      [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
      echo "NO $kernel_img_name is available!!! Something went wrong! Program terminated!!!"
      [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
      exit 1
    fi
    # create modules.dep...
    if [ -e "/tftpboot/node_root/boot/System.map-$drbl_kernel_ver" ]; then
       create_depmod_env /tftpboot/node_root
       /usr/bin/chroot /tftpboot/node_root/ depmod -ae -F /boot/System.map-$drbl_kernel_ver $drbl_kernel_ver
       #clean_depmod_env /tftpboot/node_root
    else
       echo "Can not find /tftpboot/node_root/boot/System.map-$drbl_kernel_ver $drbl_kernel_ver! Program terminated!!!"
       exit 1 
    fi
    ;;
  "local")
    echo "$msg_install_kernel_might_take_several_minutes"
    drbl_kernel_ver="$(uname -r)"
    mkdir -p $drbl_common_root/boot/ $drbl_common_root/lib/modules/
    rsync -a /boot/*$drbl_kernel_ver $drbl_common_root/boot/
    rsync -a /lib/modules/$drbl_kernel_ver $drbl_common_root/lib/modules/
    echo "$msg_done!"
    rc=0
    ;;
esac

# once it's done, skip the next search
if [ "$rc" -eq 0 ]; then
  # make a tag in the system for client's kernel arch.
  echo "$KARCH" > $pxecfg_pd/client_kernel_arch.txt
else
  [ -n "$verbose" ] && echo "$latest_drbl_kernel_in_rep_rpm is not installed successfully!!!"
fi
} # end of install_client_kernel_in_SUSE

#
Usage() {
  echo "Usage:"
  echo "$0 [Option]"
  echo "Option:"
  echo "-f, --force-yes:  force yes, only for Debian-like distribution. It  should  not  be used except in very special situations. Using force-yes can potentially destroy your system!" 
  echo "-i, --install:    install DRBL." 
  echo "-u, --uninstal:   uninstall DRBL." 
  echo "-v, --verbose:    verbose mode."
  echo "-t, --testing [y/n]:    use packages in testing branch or not."
  echo "-a, --unstable [y/n]:   use packages in unstable branch or not."
  echo "-n, --netinstall [y/n]: install the network installation program or not."
  echo "-m, --smp-client [y/n]: use SMP kernel for DRBL clients or not."
  echo "-x, --set-proxy [y/n]:  set proxy or not."
  echo "-c, --console-output [y/n]: set console output for client or not."
  echo "-g, --upgrade_system [y/n]: upgrade system or not."
  echo "-k, --client_archi [0/1/2]: set the client's CPU arch, 0 for i386, 1 for i586, 2 for same level with DRBL server (for x86_64, use 2)."
  echo "-o, --client_kernel_from [1/2]: choose client's kernel image from, 1 from DRBL server running kernel (if available), 2 from ayo repository."
  echo "-s, --skip-select-repository: skip the question for selecting repository."
  echo "-l, --language N  Set the language to be shown, where N:"
  echo "                  0: English,"
  echo "                  1: Chinese Traditional (Big5) - Taiwan,"
  echo "                  2: Chinese Traditional (UTF-8, Unicode) - Taiwan."
}

#############
### main ####
#############
check_if_root

# main
while [ $# -gt 0 ]; do
  case "$1" in
   -f|--force-yes)
        shift; apt_extra_opt="--force-yes"
	;;
   -l|--language)
	shift; specified_lang="$1"
	shift;;
   -i|--install)
        shift; mode="install"
	;;
   -u|--uninstall)
        shift; mode="uninstall"
	;;
   -t|--testing)
        shift; drbl_test_answer="$1"
	shift;;
   -a|--unstable)
        shift; drbl_unstable_answer="$1"
	shift;;
   -n|--netinstall)
        shift; drbl_netinstall_answer="$1"
	shift;;
   -m|--smp-client)
        shift; drbl_SMP_answer="$1"
	shift;;
   -s|--skip-select-repository)
        shift; select_repository="no"
	;;
   -x|--set-proxy)
        shift; set_proxy_answer="$1"
	shift;;
   -c|--console-output)
        shift; console_output_answer="$1"
	shift;;
   -g|--upgrade_system)
        shift; upgrade_system_answer="$1"
	shift;;
   -k|--client_archi)
        shift; client_archi_ans="$1"
	shift;;
   -o|--client_kernel_from)
        shift; chosen_client_kernel_from="$1"
	shift;;
   -y|--yum)
        shift; preferred_installer="yum"
	;;
   -d|--apt)
        shift; preferred_installer="apt"
	;;
   -p|--urpmi)
        shift; preferred_installer="urpmi"
	;;
   -v|--verbose)
        shift; verbose="on"
        ;;
   *)
	Usage
	exit 1
  esac
done
     
# mode is essential
[ -z "$mode" ] && Usage && exit 1

#
ask_and_load_lang_set $specified_lang

#
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_hint_for_answer"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

echo "$msg_delimiter_star_line"
# get the distribution name and type: OS_Version and OS_type
check_distribution_name
echo "$msg_delimiter_star_line"

#
case "$OS_Version" in
   RH*)
      # For RH/FC, $client_archi_set will be decided by user or system.
      installer="apt"
      ARCH="i386"
      ;;
   FC*|CO*)
      # For RH/FC, $client_archi_set will be decided by user or system.
      # Yum is better is FC3 then that in FC2/FC1, we can use it as one of our
      # installer.
      case "$OS_Version" in
        FC[3-9]|CO*)
         # TODO x86_64: CPU arch -> rpm package, which one is real ?
         # "uname -m" or "rpm -q --qf '%{arch\n}' kernel..."
         if [ "$(uname -m)" = "x86_64" ]; then
             ARCH="x86_64"
         else
             ARCH="i386"
         fi
	 #
	 if [ "$preferred_installer" = "apt" ]; then
           installer="apt"
         else
           installer="yum"
         fi
	 ;;
	*)
	 # we only support x86 version and use apt for FC2/FC1
         ARCH="i386"
         installer="apt"
	 ;;
      esac
      ;;
   MDK*|MDV*)
      # default installer is urpmi now, no more apt
      installer="urpmi"
      # For MDK, the arch is only i586 or x86_64
      if [ "$(uname -m)" = "x86_64" ]; then
          ARCH="x86_64"
      else
          ARCH="i586"
      fi
      ;;
   DBN*)
      # For Debian, $client_archi_set will be decided by user or system.
      installer="apt"
      if [ "$(uname -m)" = "x86_64" ]; then
          ARCH="amd64"
      else
          ARCH="i386"
      fi
      ;;
   SUSE*)
      #
      case "$OS_Version" in
        SUSE9.3)
          installer="apt"
	  ;;
	*)
	  # For SuSE 10.0 or later, let yum be the default installer
          if [ "$preferred_installer" = "apt" ]; then
            installer="apt"
          else
            installer="yum"
          fi
	  ;;
      esac

      if [ "$(uname -m)" = "x86_64" ]; then
          ARCH="x86_64"
      else
          ARCH="i386"
      fi
      # Get the version no, OS_Version is like "SUSE10.0", then OS_VER_NO=10.0
      OS_VER_NO=${OS_Version:4}
      # For OpenSuSE 10.0, the path in yum repo is like
      # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.0-OSS/inst-source/suse/
      # For OpenSuSE 10.1, it's like (no -OSS):
      # http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/inst-source/suse/ 
      # Hope SuSE will keep this rule.
      # For SuSE 9,3 this variable $OS_PATH_IN_YUM_REPO is useless.
      case "$OS_VER_NO" in
        10.0)
            OS_PATH_IN_YUM_REPO="SL-${OS_VER_NO}-OSS"
	    ;;
        *)
            OS_PATH_IN_YUM_REPO="SL-${OS_VER_NO}"
	    ;;
      esac
      ;;
   *)
      echo "This version in this distribution is NOT supported by DRBL, maybe you can try to use the drbl in testing or unstable repository. Program terminated!"
      exit 1 
esac

# for yum and apt, the regular expression for pkg is different.
# netinstall_for_yum="rh-*-netinstall rh-FC*-netinstall mdk-*-netinstall mdv-*-netinstall debian-*-netinstall CentOS-*-netinstall ubuntu-*-netinstall suse-*-netinstall opensuse-*-netinstall"
# netinstall_for_apt="rh-.*-netinstall rh-FC.*-netinstall mdk-.*-netinstall mdv-.*-netinstall debian-.*-netinstall CentOS-.*-netinstall ubuntu-.*-netinstall suse-.*-netinstall opensuse-.*-netinstall"
netinstall_for_yum="netinstall-*"
netinstall_for_apt="netinstall-.*"
# urpmi does not accept the package names with regular expression,
# so $netinstall_for_urpmi and $net_install_pkgs will be decided when urpmi source media is set.

eval net_install_pkgs=\$netinstall_for_${installer}

#
case "$installer" in
  "apt")
     inst_prog="apt-get"
     ;;
  "yum")
     inst_prog="yum"
     ;;
  "urpmi")
     inst_prog="urpmi"
     ;;
  *)
     echo "Unknown installer!!! Program terminated!"
     exit 1
     ;;
esac

# for CentOS, we force to make select_repository="no", we will not put centos 
# yum config in this file
# for some distributions, we have to force user select the repository
# for FC3 and newer version, the question is open
# in the future, maybe we will set all the distribution select_repository="no"
case "$OS_Version" in
     CO*)
       select_repository="no"
       ;;
     RH*|FC[1-2]|MD[KV]*)
       select_repository="yes"
       ;;
esac

# do it
case "$mode" in
   install)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	eval msg=\$msg_install_${OS_type}
	echo $msg
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	install_drbl_${OS_type}
	;;
   uninstall)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	eval msg=\$msg_uninstalling_DRBL_${OS_type}
	echo $msg
	echo "$msg_analyzing_rpm_info"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	uninstall_drbl_${OS_type}
	;;
   *)
	Usage
	exit 1
esac
