#!/bin/sh
#
# Copyright (C) 2002-2003 by NCHC, Steven Shiau, K. L. Huang
# (steven@nchc.org.tw, c00hkl00@nchc.org.tw)
#
# 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.
#
# script to prepare some files for DRBL server
# Written by Steven Shiau < steven@nchc.org.tw > 2003/4/13
#
#
url_prefix="http://opensource.nchc.org.tw/"
DRBL_SCRIPT_PATH="/opt/drbl"
drbl_setup_path="$DRBL_SCRIPT_PATH/setup"
drblroot="/tftpboot/nodes"
cache_dir="/var/cache/apt/archives"
drbl_pkgdir="/opt/drbl/share/"
pxelinux_file="/usr/lib/syslinux/pxelinux.0"
memdisk_file="/usr/lib/syslinux/memdisk"
fdos_img_src="/usr/lib/freedos/boot1440.img"
img_output="boot1440_drbl.img"
nbi_output="boot1440_drbl.nbi"
#
cache_dir="/var/cache/apt/archives"
#
MDK92_wget_rpm="wget-1.8.2-12mdk.i586.rpm"
MDK10_0_wget_rpm="wget-1.9.1-4mdk.i586.rpm"
# for RH and MDK, the arch is different
ARCH="i586"

# Source function library.
. /etc/rc.d/init.d/functions

# Language
echo "Language ?"
echo "[0]: English (default)"
echo "[1]: Chinese Traditional (Big5) - Taiwan"
echo "[2]: Chinese Traditional (UTF-8, Unicode) - Taiwan";
read lang_answer
case "$lang_answer" in
   1)
      lang="tw.BIG5"
      ;;
   2)
      lang="tw.UTF-8"
      ;;
   *)
      lang="en"
esac
     
# get the l10n message
. $drbl_setup_path/lang/bash/$lang

#
echo "$msg_delimiter_star_line"
#
check_apt_install() {
  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_if_root() {
# Check if root or not
   if [ ! "$UID" = "0" ]; then
     echo
     echo "[$LOGNAME] You need to run this script \"`basename $0`\" as root."
     echo
     exit 1
   fi
}

#
check_apt_integrity() {
   apt-get -f install
}

#
setup_apt_mdk() {
# define a function get_inst_rpm to get and install rpm package which 
# apt can not process now. ie. only $ARCH rpms
get_inst_rpm() {
  # example: get_inst_rpm http://os.nchc.org.tw/... glibc --force
  url_tmp="$1"
  pkg="$2"
  opt="$3"
  pkg_tmp_dir=`mktemp -d -q /tmp/$pkg.XXXXXX || exit 1`
  # note! the last / in $url must be kept!!!
  wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd -P "$pkg_tmp_dir" -A "$pkg-[0-9].*.$ARCH.rpm" $url_tmp/
  rc="$?"
  if [ $rc -gt 0 ]; then
     [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
     echo "$msg_Failed_to_get $pkg...$msg_program_stop"
     [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
     exit 1
  fi
  pkg_file=$(ls $pkg_tmp_dir/$pkg-[0-9].*.$ARCH.rpm)
  if [ "$pkg_file" != "" ]; then
    rpm -ivh $opt $pkg_file
    RETVAL=$?
    if [ "$RETVAL" -gt 0 ]; then 
       echo "$msg_Failed_to_install $pkg_file..."
       return $RETVAL
    fi
  else
    echo "XXXXXXX        XXXXXXX       XXXXXXX"
    echo "$msg_Failed_to_install \"$pkg\"!!!"
    echo "$msg_check_network_and_url $url"
    exit 1
  fi
  if [ -d $pkg_tmp_dir ]; then
    rm -rf $pkg_tmp_dir
  fi
}

# Start
check_if_root


# Setting
# 8.0:/etc/issue or /etc/redhat-release: Red Hat Linux release 8.0 (Psyche)
# 9.0:/etc/issue or /etc/redhat-release: Red Hat Linux release 9 (Shrike)
# FC1:/etc/fedora-release: Fedora Core release 1 (Yarrow)
# $OS_Version will be the directory index for apt repository
echo "$msg_delimiter_star_line"
grep "Mandrake Linux release 9.2 (FiveStar) for i586" /etc/mandrake-release 2>/dev/null && OS_Version="MDK9.2"
grep "Mandrake Linux release 10.0 (Community) for i586" /etc/mandrake-release 2> /dev/null && OS_Version="MDK10.0"
echo "$msg_delimiter_star_line"
if [ -z "$OS_Version" ]; then
  [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
  echo "$msg_not_determine_OS"
  echo "$msg_enter_OS_ver"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  read OS_Version
  case "$OS_Version" in
  MDK9.2|MDK10.0)
              [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
              echo "The version number you entered is $OS_Version."
              [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
              ;;
  *)
              [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
              echo "$OS_Version is not supported version, program stop!."
	      echo "$msg_press_ctrl_c_stop!"
              [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	      read
	      exit 1 
  esac
fi

#
case "$OS_Version" in
   MDK9.2|MDK10.0)
            [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
            echo "$msg_OS_version: $OS_Version"
            [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
            #http://opensource.nchc.org.tw/redhat/apt/ redhat/9/en/$ARCH/ os updates drbl... -> $url_prefix/$apt_dir $apt_list $core_dir
	    # core_dir is for the core program, for RH8 and 9, RPMS.os, set it as "os"
            #
            VER="`echo $OS_Version | sed -e 's/MDK//g'`"
            apt_dir="/mandrake/apt/"
	    # note!!! Do NOT append "/" in the end of $apt_list
	    apt_list="mandrake/i586"
	    core_dir="$VER"
            ;;
   *)
            [ "$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

# 0.
echo "$msg_delimiter_star_line"
DOMAINNAME=`/bin/dnsdomainname`
if [ -n "$DOMAINNAME" ]; then
    DEFAULT_PROXY="proxy.$DOMAINNAME"
fi
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_set_proxy"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[y/N] "
read proxy_answer
case "$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

# 2. 

[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_drbl_test_option"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[y/N] "
read drbl_test_answer
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_firefly_option"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[y/N] "
read cjk_answer
[ "$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
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
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_smp_clients_MDK"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
echo -n "[y/N] "
read SMP_answer

# url is where the RPMS.os, RPMS.update, RPMS.drbl... exist
url="$url_prefix/$apt_dir/$apt_list/"

# check if apt is installed or not
if ! rpm -q --quiet apt; then
   echo "$msg_install_apt"
   rpm -q wget &>/dev/null
   if [ "$?" -gt 0 ]; then 
      echo "$msg_no_wget_get_it"
      case "$VER" in
      9.2)
        echo "The MDK version is $VER"
        # This one only works for RH8
        rpm -ivh $url/RPMS.$VER/$MDK92_wget_rpm
        ;;
      10.0)
        echo "The MDK version is $VER"
        # This one only works for RH8
        rpm -ivh $url/RPMS.$VER/${MDK10_0_wget_rpm}
        ;;
      esac
   fi
   get_inst_rpm $url/RPMS.$VER-drbl libapt-pkg0
   get_inst_rpm $url/RPMS.$VER-drbl apt   
else
   echo "$msg_delimiter_star_line"
   echo "$msg_apt_already_installed"
   echo "$msg_delimiter_star_line"
fi

#
echo "$msg_delimiter_star_line"
if [ -f /etc/apt/sources.list ]; then
  echo "$msg_backup_apt_sources"
  mv -v /etc/apt/sources.list /etc/apt/sources.list.orig
fi

#
case "$drbl_test_answer" in
    [yY]|[yY][eE][Ss])
      DRBL_TEST="$VER-drbl-test"
      ;;
    *)
      DRBL_TEST=""
esac
case "$cjk_answer" in
    [yY]|[yY][eE][Ss])
      CJK_REPOSITORY="$VER-firefly"
      ;;
    *)
      CJK_REPOSITORY=""
esac
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 "$SMP_answer" in
    [yY]|[yY][eE][Ss])
      SMP_OPTION="-smp"
      ;;
    *)
      SMP_OPTION=""
esac

echo "$msg_setup_apt_sources_MDK $VER..."
cat <<-APT_END > /etc/apt/sources.list
rpm $url_prefix/$apt_dir $apt_list $core_dir $VER-contrib $VER-updates $VER-drbl $DRBL_TEST $CJK_REPOSITORY
APT_END

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

} # setup_apt_mdk

install_mdk_drbl() {

# function to install mdk drbl
# setup apt first
setup_apt_mdk

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

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.
apt-get update

# check if apt is integrity... original MDK 9.2 is buggy...
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_check_apt_integrity"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
check_apt_integrity

#
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_delimiter_star_line"
echo "$msg_upgrade_whole_system"
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
apt-get upgrade


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

#
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_delimiter_star_line"
echo "$msg_install_DRBL_necesary_files"
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
apt-get -y install dhcp-server tftp-server nfs-utils ypserv ypbind yp-tools mkinitrd ntp iptables
#apt-get -y install -o $latest_drbl_kernel_in_rep
apt-get -y install mkinitrd-net etherboot syslinux partimage drbl-script socket memtest86+ freedos clonezilla rh-.*-netinstall mdk-.*-netinstall dialog initscripts rsync drakxtools pciutils autologin reiserfs-utils tcpdump bc

# if no gdm, we do not install drbl-gdm
if rpm -q --quiet gdm; then
  apt-get install drbl-gdm
fi
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL

# Steven todo, for MDK 9.2... no dev rpm...I have to make one...
# It's ugly here...
# from DRBL server system ?
# 8.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 || exit 1`
#[ ! -d $drbl_pkgdir ] && mkdir -p $drbl_pkgdir
#
#apt-get -y --download-only --reinstall install dev86 makedev
#RETVAL=$?
#check_apt_install $RETVAL
#(cd $tmp_dev_dir; rpm2cpio $cache_dir/dev86_*_i586.rpm |cpio -idm)
#(cd $tmp_dev_dir; rpm2cpio $cache_dir/makedev_*_noarch.rpm |cpio -idm)
mkdir -p $drbl_pkgdir/
cp -f $drbl_setup_path/files/dev.$OS_Version.tgz $drbl_pkgdir/dev.tgz
#(cd $tmp_dev_dir/dev; tar czf $drbl_pkgdir/dev.tgz *)
#echo "Cleaning the tmp device directory..."
#rm -rf $tmp_dev_dir

# Check if the kernel matches the user specified ?
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo "$msg_check_installed_kernel"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
latest_drbl_kernel=`rpm -qa |grep ^kernel${SMP_OPTION}-2.[46].* | sort -g | tail -n 1`
if [ -n "$latest_drbl_kernel" ]; then
   echo "$msg_Got_it, $latest_drbl_kernel $msg_is_already_installed"
else
   echo "$msg_need_to_install_kernel"
   latest_drbl_kernel=`apt-cache pkgnames kernel${SMP_OPTION}-2.* | sort -g | tail -n 1`
   apt-get -y install $latest_drbl_kernel
fi

rpm -q --qf '%{filenames}\n' $latest_drbl_kernel > /dev/null && boot_kernel=`rpm -q --qf '%{filenames}\n' $latest_drbl_kernel`
drbl_kernel=`echo $boot_kernel |cut -d"-" -f2-`

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"

# check if drbl-script is installed or not.
if ! rpm -q --quiet drbl-script; 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
$DRBL_SCRIPT_PATH/sbin/mknic-nbi --kernel $drbl_kernel --all
echo "$msg_Done!"

# 11.
echo "$msg_delimiter_star_line"
[ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
echo -n "$msg_create_files_for_PXE_clients"
[ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
if [ ! -d /tftpboot/nbi_img/pxelinux.cfg ]; then
   mkdir -p /tftpboot/nbi_img/pxelinux.cfg
fi
if [ -f $pxelinux_file ]; then
   cp -f $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
if [ -f $memdisk_file ]; then
   cp -f $memdisk_file /tftpboot/nbi_img
else
   [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
   echo "$msg_Warning! $memdisk_file $msg_NOT_found!!! $msg_PXE_NOT_work"
   echo "$msg_press_enter_to_continue"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   read
fi

cat <<-PXE_END > /tftpboot/nbi_img/pxelinux.cfg/default
# note!!! if "serial" directive exists, it must be the first directive
$PXE_SERIAL_OUTPUT
default drbl
timeout 0
label drbl
  kernel vmlinuz-pxe
  append initrd=initrd-pxe.img ramdisk_size=9600 $CONSOLE_OUTPUT

label memtest
  # note! *.bin is specially purpose for syslinux, 
  # do NOT use memtest86.bin, use memtest86 instead of memtest86.bin
  kernel memtest86

label local
  localboot 0

label fdos
  # ref: http://syslinux.zytor.com/memdisk.php
  kernel memdisk
  append initrd=boot1440_drbl.img

label rh-8.0-netinstall
  kernel vmlinuz.rh8.0_netinstall
  append initrd=initrd_rh8.0_netinstall.img

label rh-9-netinstall
  kernel vmlinuz.rh9_netinstall
  append initrd=initrd_rh9_netinstall.img

label rh-FC1-netinstall
  kernel vmlinuz.FC1_netinstall
  append initrd=initrd_FC1_netinstall.img

label mdk-9.2-netinstall
  kernel vmlinuz.mdk9.2_netinstall
  append initrd=initrd_mdk9.2_netinstall.img

label mdk-10.0-netinstall
  kernel vmlinuz.mdk10.0_netinstall
  append initrd=initrd_mdk10.0_netinstall.img

PXE_END

# create the freedos image, which a clean one.
# if user wants to insert files, use 
# the script /opt/drbl/sbin/insert-file-fdos.sh
[ -d kernel.sys ] && rm -f kernel.sys
if [ -f $fdos_img_src ]; then
   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/$nbi_output kernel.sys $fdos_img_src
   cp -f $fdos_img_src /tftpboot/nbi_img/$img_output
   [ -d $img_mt ] && rm -rf $img_mt
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

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

uninstall_drbl_mdk() {

#apt-get remove dhcp-server tftp-server nfs-utils ypserv ypbind yp-tools mkinitrd ntp iptables
#apt-get remove mkinitrd-net etherboot syslinux partimage drbl-script socket memtest86+ freedos clonezilla  mdk-.*-netinstall dialog initscripts rsync drakxtools pciutils autologin drbl-gdm

# check if apt is installed or not
if ! rpm -q --quiet apt; then
   [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
   echo "$msg_no_apt_already_uninstall"
   echo "$msg_program_stop"
   [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
   exit 1
fi
apt-get remove mkinitrd-net etherboot partimage drbl-script socket memtest86+ freedos clonezilla rh-.*-netinstall mdk-.*-netinstall drbl-gdm drbl-setup

[ -f $drbl_pkgdir/dev.tgz ] && rm -f $drbl_pkgdir/dev.tgz
echo "$msg_remove_apt_question"
echo -n "[y/N] "
read apt_answer
if [ "$apt_answer" = "y" ] || [ "$apt_answer" = "Y" ]; then
  rpm -e libapt-pkg0 apt
  if [ "$(ls /var/cache/apt/ 2> /dev/null)" != "" ]; then
     echo "$msg_cleaning_apt_cache"
     rm -rf /var/cache/apt/
  fi
fi
echo "$msg_delimiter_star_line"
echo "$msg_cleaning_tftpboot"
echo "$msg_delimiter_star_line"
rm -rf /tftpboot/* 

echo "$msg_Done!!!"

}

#
Usage() {
  echo "Usage:"
  echo "$0 [-i|--install] [-u|--uninstall]"
  echo "-i, --instal:     install DRBL for Mandrake." 
  echo "-u, --uninstal:   uninstall DRBL for Mandrake." 
}

mode="$1"

check_if_root

case "$mode" in
   -i|--install)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	echo $msg_install_MDK
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	install_mdk_drbl
	;;
   -u|--uninstall)
        [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING
	echo "$msg_uninstalling_DRBL_MDK"
	echo "$msg_analyzing_rpm_info"
        [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
	uninstall_drbl_mdk
	;;
   *)
	Usage
	exit 1
esac
