#!/bin/bash

function set_installpath {
    case $CHOICE in
	A) INSTALL_PATH=/;;
	B) INSTALL_PATH=/boot;;
	C) INSTALL_PATH=/etc;;
    esac
}

function make_mrproper {
    CRETVAL=

    if [ -f /usr/src/linux/.config ]; then
	echo -e "\n\033[1;33mExecuting \"make mrproper\" ...\033[0;31m"
	sleep 2
	cp -f /usr/src/linux/.config /tmp/.config.tmp
	make mrproper
	mv /tmp/.config.tmp /usr/src/linux/.config
    fi
}

function compile_commands {
    CRETVAL=

    echo -e "\n\033[1;33mExecuting \"make dep\" ...\033[0;31m"
    sleep 2
    if ! make dep ; then
	CRETVAL=1
	return
    fi

    #    echo -e "\n\033[1;33mExecuting \"make clean\" ...\033[0;31m"
    #    sleep 2
    #    make clean

    echo -e "\n\033[1;33mExecuting \"make bzimage\" ...\033[0;31m"
    sleep 2
    if ! make bzImage ; then
	CRETVAL=1
	return
    fi

    echo -e "\n\033[1;33mExecuting \"make modules\" ...\033[0;31m"
    sleep 2
    if ! make modules ; then
	CRETVAL=1
	return
    fi

    echo -e "\n\033[1;33mExecuting \"make modules_install\" ...\033[0;31m"
    sleep 2
    if ! make modules_install ; then
	#CRETVAL=1
	return
    fi

    setterm -foreground white -background black -bold off
}

function set_bootloader {
    case $CHOICE in
	A) LOADER=LILO;;
	B) LOADER=GRUB;;
    esac
}

function compile_kernel {
    local BOOTIMAGE=/usr/src/linux/arch/i386/boot/bzImage
    local MAPFILE=/usr/src/linux/System.map

    RAD="Where do you keep your kernels ?"
    INPUT=set_installpath
    STRING=("A" "/" off \
	    "B" "/boot" on \
	    "C" "/etc" off)
    radiolist "${STRING[@]}"
    [ "$RETVAL" != "0" ] && return

    inputbox "Specify source directory (e.g. linux-2.2.14) or hit enter for [${SOURCEDIRNAME}]"
    SOURCEDIRNAME=${INPUT:=$SOURCEDIRNAME}
    [ "$RETVAL" != "0" ] && return

    inputbox "Specify kernel version (e.g. 2.2.14-15) or hit enter for [${KERNEL_VERSION}]"
    KERNEL_VERSION=${INPUT:=$KERNEL_VERSION}
    [ "$RETVAL" != "0" ] && return

    saverc_user

    [ -d "/usr/src/$SOURCEDIRNAME" ] && \
    ln -sfn /usr/src/$SOURCEDIRNAME /usr/src/linux

    cd /usr/src/linux

    exec_command make_mrproper

    if [ -z "$DISPLAY" ]; then
	CNF=menuconfig
	exec_command "make menuconfig" || NOCONFIG=1
    else
	CNF=xconfig
	make xconfig || NOCONFIG=1
    fi

    if [ "$NOCONFIG" = "1" ]; then
	msgbox "Failed to run \"make $CNF\""
	exit 1
    else
	exec_command compile_commands
    fi

    if [ ! -f $BOOTIMAGE ]; then
	CRETVAL=1
	killall make
	msgbox "Error: Kernel image \"$BOOTIMAGE\" failed to compile."
    fi

    if  [ "$CRETVAL" != "1" ]; then

	CRETVAL=

	if [ -f $INSTALL_PATH/kernel-$KERNEL_VERSION ]; then
	    cp -b $INSTALL_PATH/kernel-$KERNEL_VERSION \
    	    $INSTALL_PATH/kernel-$KERNEL_VERSION.cmorig;
	fi

	if cp -f $BOOTIMAGE $INSTALL_PATH/kernel-$KERNEL_VERSION; then
	    msgbox "Copied kernel to [$INSTALL_PATH/kernel-$KERNEL_VERSION]
If necessary, edit your /etc/lilo.conf file"	    
	fi

	if [ -f $INSTALL_PATH/System.map-$KERNEL_VERSION ]; then
            cp -b $INSTALL_PATH/System.map-$KERNEL_VERSION \
            $INSTALL_PATH/System.map-$KERNEL_VERSION.cmorig;
	fi

	if cp -f $MAPFILE $INSTALL_PATH/System.map-$KERNEL_VERSION; then
	    msgbox "Copied System.map to [$INSTALL_PATH/System.map-$KERNEL_VERSION]"
	fi

	cd $INSTALL_PATH
	ln -sf System.map-$KERNEL_VERSION $INSTALL_PATH/System.map
	cp -b ${HOME}/.conmen/.config-$KERNEL_VERSION ${HOME}/.conmen/cmorig/.config-$KERNEL_VERSION.cmorig
	cp -b /usr/src/linux/.config ${HOME}/.conmen/.config-$KERNEL_VERSION

	# /sbin/depmod -a

	ROOT=`mount | grep "/dev/hd.* on / " | cut -d " " -f 1 | cut -d \  -f 1`
	LABEL=cm`uname`
	RAD="Which bootloader do you use ?"
	INPUT=set_bootloader
	STRING=("A" "LILO" off \
		"B" "GRUB" on)
	radiolist "${STRING[@]}"

	if [ "${LOADER}" = "LILO" ]; then
	    if ! grep -q ":${LABEL}:" /etc/lilo.conf; then
		yesnobox "Add the following lines to /etc/lilo.conf ?\n
image=${INSTALL_PATH}/kernel-${KERNEL_VERSION}
      label=${LABEL}
      root=${ROOT}
      read-only"

		if [ "$?" = "0" ]; then
		    echo -e "image=${INSTALL_PATH}/kernel-${KERNEL_VERSION}
      label=${LABEL}
      root=${ROOT}
      read-only" >> /etc/lilo.conf
		fi
	    fi

	    yesnobox "Run LILO ?"
	    if [ "$?" = "0" ]; then
		if [ -x /sbin/lilo ]; then
		    /sbin/lilo -v
		else
		    /etc/lilo/install
		fi
		sleep 5
	    fi
	fi
	
	if [ "${LOADER}" = "GRUB" ]; then
	    NUM=`echo ${ROOT} | sed "s|/dev/hd.|| ; \
				     s|/dev/sd.||"`
	    PARTITION=`echo ${ROOT} | sed "s|/dev/hd|| ; \
					   s|/dev/sd|| ; \
					   s|a.*|0,$(expr $NUM - 1)|; \
					   s|b.*|1,$(expr $NUM - 1)|; \
					   s|c.*|2,$(expr $NUM - 1)|; \
					   s|d.*|3,$(expr $NUM - 1)|"`

	    if ! grep -q ":${LABEL}:" /boot/grub/menu.lst; then
		yesnobox "Add the following lines to /boot/grub/menu.lst ?\n
title  ${LABEL}
root (hd${PARTITION})
kernel ${INSTALL_PATH}/kernel-${KERNEL_VERSION} \
root=${ROOT}"

		if [ "$?" = "0" ]; then
		    echo -e "title  ${LABEL}
root (hd${PARTITION})
kernel ${INSTALL_PATH}/kernel-${KERNEL_VERSION} \
root=${ROOT}" >> /boot/grub/menu.lst
		fi
	    fi
	fi
    fi
}

function test_for_file {
    if [ -e "$TSTFILE" ]; then
	$DO $TSTFILE
    else
	echo -e "[\033[1;33m"$TSTFILE"\033[0m] does not exist"
    fi
}

function proc_info {
    echo "*******************************************"
    echo "c :  CPU               p :  PCI devices    "
    echo "m :  Memory            f :  File systems   "
    echo "d :  Detected devices  i :  IRQ/IO/DMA     "
    echo "s :  Sound             x :  Exit"
    echo "*******************************************"
    read GO
    DO=cat
    case $GO in
	c) TSTFILE=/proc/cpuinfo; test_for_file;;
	m) TSTFILE=/proc/meminfo; test_for_file;;
	d) TSTFILE=/proc/devices; test_for_file
	   TSTFILE=/proc/interrupts; test_for_file;;
	s) TSTFILE=/proc/sound; test_for_file
	   TSTFILE=/dev/sndstat; test_for_file
           TSTFILE=/proc/asound/cards; test_for_file
           TSTFILE=/proc/asound/devices; test_for_file
           TSTFILE=/proc/asound/oss-devices; test_for_file
           TSTFILE=/proc/asound/pcm; test_for_file
           TSTFILE=/proc/asound/sndstat; test_for_file
           TSTFILE=/proc/asound/timers; test_for_file
           TSTFILE=/proc/asound/version; test_for_file
           TSTFILE=/proc/asound/0/mixerD0; test_for_file;;
	p) TSTFILE=/proc/pci; test_for_file;;
	f) TSTFILE=/proc/filesystems; test_for_file
	   TSTFILE=/proc/partitions; test_for_file;;
	i) TSTFILE=/proc/interrupts; test_for_file
	   TSTFILE=/proc/ioports; test_for_file
	   TSTFILE=/proc/dma; test_for_file;;
	x) return;;
    esac
    echo
    echo -e "\033[1;33mUse Shift-PGUP, Shift-PGDOWN to scroll\033[0m"
    hit_enter; clear; proc_info
}

function remove_backupfiles {
    cd /
    # note: it must be $CHOICES, not "$CHOICES"
    for WORD in $CHOICES ; do

	case $WORD in
	    A) nice -20 find / -regex '.*~' -type f -exec rm -i {} \; ;;
	    B) nice -20 find / -regex '~*'  -type f -exec rm -i {} \; ;;
	    C) nice -20 find / -regex '.*-' -type f -exec rm -i {} \; ;;
	    D) nice -20 find / -regex '#.'  -type f -exec rm -i {} \; ;;
	    E) nice -20 find / -iregex '.*[.]bak' -type f -exec rm -i {} \; ;;
	    F) nice -20 find / -iregex '.*[.]bck' -type f -exec rm -i {} \; ;;
	    G) nice -20 find / -iregex '.*[.]old' -type f -exec rm -i {} \; ;;
	    H) nice -20 find / -regex '.*[.][r][p][m][son].*' -type f -exec rm -i {} \; ;;
	    I) nice -20 find / -name 'core' -type f -exec rm -i {} \; ;;
	    J) nice -20 find /var/log -name '*.[1-9]' -type f -exec rm -i {} \; ;;
	    K) nice -20 find / -name '*TRANS.TBL'     -type f -mount -exec rm -i {} \; ;;
	esac

    done

    df -h

    hit_enter
}

function set_string {

    STRING=(atd "At daemon" off \
    	    crond "Cron scheduler" off \
	    fcron "Fcron scheduler" off \
	    autofs "Automounter" off \
    	    syslog "System logger" off \
	    lpd "Line Printer Daemon" off \
	    cups "Common Unix Printing System" off \
    	    xwSvr "XW-Print server" off \
    	    xwPrint "XW-Print spooler" off \
	    inet "Inet daemon" off \
    	    junkbuster "JunkBuster" off \
	    postfix "Mail Transport Agent" off \
    	    smb "Samba smbd" off \
	    scanlogd "Portscan detector" off)

}

function kill_daemons {
    for WORD in $CHOICES ; do
	$INITDIR/$WORD stop ; sleep 1
    done
    hit_enter
}

function start_daemons {
    for WORD in $CHOICES ; do
	if [ "${WORD}" = "xwSvr" -o "${WORD}" = "xwPrint" ]; then
	    $INITDIR/$WORD stop ; sleep 1
	    $INITDIR/$WORD start ; sleep 1
	else
	    $INITDIR/$WORD restart ; sleep 1
	fi
    done
    hit_enter
}

function menuchoices {
    if [ "$choice" = "A" ]; then

	CHK="Select the files you want to delete"
	INPUT=remove_backupfiles

	STRING=("A" "Remove *~" on \
		"B" "Remove ~*" on \
		"C" "Remove *-" on \
		"D" "Remove #*" on \
		"E" "Remove *.BAK and *.bak" off \
		"F" "Remove *.BCK and *.bck" off \
		"G" "Remove *.OLD and *.old" off \
		"H" "Remove *.rpmsave *.rpmorig *.rpmnew" off \
		"I" "Remove coredumps" on \
		"J" "Remove *.1-9 from /var/log/" on \
		"K" "Remove *TRANS.TBL" on)

	checkbox "${STRING[@]}"

    fi

    if [ "$choice" = "B" ]; then
	CHK="Name The Daemons Thou Wants To Kill"
	INPUT=kill_daemons
	set_string
	checkbox "${STRING[@]}"
    fi

    if [ "$choice" = "C" ]; then
	CHK="Select the services you want to (re)start"
	INPUT=start_daemons
	set_string
	checkbox "${STRING[@]}"
    fi

    [ "$choice" = "D" ] && compile_kernel

    [ "$choice" = "E" ] && clear && proc_info

    if [ "$choice" = "F" ]; then
	inputbox "Specify Directory
A file /tmp/biggestfile will be written"
	cd $INPUT; ls -lR | sort +4n > /tmp/biggestfile
    fi

system
}
