#! /bin/sh

# ----------------------------------------------------------
# Diva build (c) Copyright by Eicon Networks 1993 - 2005
# Please read legal notice at begin of reference manual:
# /usr/lib/eicon/divas/httpd/reference_manual.html
# ----------------------------------------------------------

#
# Set up variables
#
options=""
do_not_wait=0
do_not_install=0
help_only=0
force_mrproper=0
INSTALL_DIR=/usr/lib/eicon/divas
build_rpm=0
no_depend=0
kernel_major=0
kernel_minor=0
modules_build_subdir="drivers/isdn"
modules_subdir="drivers/isdn/eicon"
kernelcapi_subdir="drivers/isdn/kernelcapi"
capi_subdir="drivers/isdn/capi20"
object_suffix="o"
version_h_was_updated=0
binary_build=""
src_only=0
backtitle="Diva Build Wizard (c) Copyright by Eicon Networks 1993 - 2005"
optimize_capi_token="       Build optimized CAPI driver without CAPI manager?\n\n      (Suggested for systems with high amount of channels,\n       supports only Eicon Diva adapters)"

# ---------------------------------------------------------
#  Features for misc kernels
# ---------------------------------------------------------
detect_kernel_features=0
kernel_2_4_feature_notifier=0

while [ $(($#)) -gt 0 ]
do
	case "$1" in
		'-mrproper') force_mrproper=1
								 options="${options} $1"
							;;
		'-nowait') do_not_wait=1
							 options="${options} $1"
							;;

		'-noinstall') do_not_install=1
							 		options="${options} $1"
							;;
    '-rpm') build_rpm=1
					  options="${options} $1"
							;;
    '-no-depend') no_depend=1
                  options="${options} $1"
							;;
    '-detect-kernel-features') detect_kernel_features=1
                  options="${options} $1"
							;;
		'-target') if [ $(($#)) -gt 1 ]
							 then
								 INSTALL_DIR=$2
			 					 options="${options} $1"
								 shift
							 else
								 help_only=1
							 fi
			 				 options="${options} $1"
							;;
		'-binary-build') if [ $(($#)) -gt 1 ]
										 then
											  binary_build="$2"
												shift
										 fi
							;;
    '-src-only') src_only=1
                  options="${options} $1"
							;;

		*) help_only=1
			 options="${options} $1"
			;;
	esac

	shift
done

if [ $((build_rpm)) -ne 0 ]
then
	if [ $((do_not_install)) -ne 0 -o "${INSTALL_DIR}" != "/usr/lib/eicon/divas" ]
	then
		echo "ERROR: combination '${options}' not supported"
		exit 1
	fi
fi

cur_build="105-541"

if [ ! -z "${binary_build}" ]
then
	echo "${binary_build}" > binary_build
fi

to=15
HERE=$(pwd)
KSRC="/usr/src/linux"
LOG="${HERE}/divas.log"
TMP="${HERE}/divas.tmp"
filelist="${HERE}/filelist"
KERNEL_VERSION=$(cat /proc/version)
SYSTEM_VERSION=$(cat /etc/*-release 2>/dev/null)
ARCHITECTURE=$(uname -m)
missing_version_h=0

case "${ARCHITECTURE}" in
  "ia64")
         ;;

  "x86_64")
         ;;


  *) ARCHITECTURE="i386"
    ;;
esac

# --------------------------------------------------
#  Status Messages
# --------------------------------------------------
cur_s_msg=1
cur=1
SMsg[$cur]="Please press CTRL-C to abort (waiting ${to} sec)"
cur=$(($cur+1))

SMsg[$cur]="Cleanup"
cur=$(($cur+1))

SMsg[$cur]="Check system environment"
cur=$(($cur+1))

SMsg[$cur]="Read kernel version"
cur=$(($cur+1))

SMsg[$cur]="Fix system headers"
cur=$(($cur+1))

SMsg[$cur]="Update kernel source tree"
cur=$(($cur+1))

SMsg[$cur]="Update kernel configuration"
cur=$(($cur+1))

SMsg[$cur]="Call 'make menuconfig'\n\n"
cur=$(($cur+1))

SMsg[$cur]="Call 'make dep', please be patient"
cur=$(($cur+1))

SMsg[$cur]="Call 'make modules', please be patient"
cur=$(($cur+1))

SMsg[$cur]="Build MTPX adapter driver"
cur=$(($cur+1))

SMsg[$cur]="Install Diva modules"
cur=$(($cur+1))

SMsg[$cur]="Cleanup"
cur=$(($cur+1))

SMsg[$cur]="Build divactrl"
cur=$(($cur+1))

SMsg[$cur]="Build CAPI tools"
cur=$(($cur+1))

SMsg[$cur]="Cleanup"
cur=$(($cur+1))

UsageString[0]="Usage: Build [ parameters ]"
UsageString[1]=" Parameters:"
UsageString[2]="   -mrproper  : to start build with 'make mrproper'"
UsageString[3]="   -detect-kernel-features"
UsageString[4]="              : to detect kernel features useful for debugging"
UsageString[5]="   -rpm       : to build binary RPM package"
UsageString[6]="   -nowait    : do not wait for responses"
UsageString[7]="   -noinstall : do not install drivers to target directory"
UsageString[8]="   -target    : to specify alternative target directory"
UsageString[9]=""

SMsg[$cur]=""


# --------------------------------------------------
#  Table with used components that are to be verified
#  before build process can start
# --------------------------------------------------
cur=1
CEntryName[$cur]="sed"
CEntryType[$cur]=5
CEntryErr[$cur]="Can not locate 'sed' utility"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="grep"
CEntryType[$cur]=4
CEntryErr[$cur]="Can not locate 'grep' utility"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="make"
CEntryType[$cur]=3
CEntryErr[$cur]="Can not locate 'make' utility"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="${KSRC}"
CEntryType[$cur]=10
CEntryErr[$cur]="Can not locate kernel source directory"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="${KSRC}/include/linux"
CEntryType[$cur]=1
CEntryErr[$cur]="Can not locate kernel include directory"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="${INSTALL_DIR}"
CEntryType[$cur]=1
CEntryErr[$cur]="Can not locate installation directory"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="${KSRC}/Makefile"
CEntryType[$cur]=2
CEntryErr[$cur]="Can not locate kernel Makefile"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="Kernel version"
CEntryType[$cur]=12
CEntryErr[$cur]="Can not determine kernel version"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="src/kernel.tgz"
CEntryType[$cur]=2
CEntryErr[$cur]="Can not locate kernel source tree update"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="src/kernel_2.tgz"
CEntryType[$cur]=2
CEntryErr[$cur]="Can not locate kernel source tree update"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="gcc"
CEntryType[$cur]=7
CEntryErr[$cur]="Can not locate 'gcc' C compiler"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="gcc"
CEntryType[$cur]=11
CEntryErr[$cur]="Can not locate 'gcc' C++ compiler"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="tar"
CEntryType[$cur]=8
CEntryErr[$cur]="Can not locate 'tar' utility"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="${KSRC}/.config"
CEntryType[$cur]=9
CEntryErr[$cur]="Can not locate kernel configuration file.\n\t\tPlease install appropriate '.config' file\n\t\tor change to ${KSRC} directory\n\t\tand execute 'make menuconfig' or 'make xconfig'\n\t\tand create kernel configuration file"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="${KSRC}/include/linux/sched.h"
CEntryType[$cur]=2
CEntryErr[$cur]="Can not locate kernel inclide file 'sched.h'"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="${KSRC}/drivers/Makefile"
CEntryType[$cur]=2
CEntryErr[$cur]="Can not locate kernel Makefile file 'drivers/Makefile'"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="/bin/bash"
CEntryType[$cur]=14
CEntryErr[$cur]="Please ensure echo -e test1 -r test2 returns test1 -r test2"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]="Kernel features"
CEntryType[$cur]=15
CEntryErr[$cur]="none"
cur=$(($cur+1))
# --------------------------------------------------
CEntryName[$cur]=""
CEntryType[$cur]=0
CEntryErr[$cur]=""

# --------------------------------------------------
#  Error messages
# --------------------------------------------------
cur=1
ErrMsg[$cur]="Can not locate compiler"
err_msg_compiler=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not fix ISDN_CONFIG_BOOL, 'sed' failed"
err_msg_isdn_config_bool_sed=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not fix ISDN_CONFIG_BOOL, 'cp' failed"
err_msg_isdn_config_bool_cp=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not fix malloc.h, 'sed' failed"
err_msg_malloc_h_sed=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not fix malloc.h, 'cp' failed"
err_msg_malloc_h_cp=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not fix sched.h failed"
err_msg_sched_h=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not fix tty.h failed"
err_msg_tty_h=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not update kernel configuration, 'sed' failed"
err_msg_fix_kernel_cfg_sed=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not update kernel configuration, 'cp' failed"
err_msg_fix_kernel_cfg_cp=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not write new kernel configuration"
err_msg_fix_kernel_cfg_write=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Failed to execute 'make menuconfig'"
err_msg_make_menuconfig=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Failed to execute 'make dep'"
err_msg_make_dep=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Execution aborted by user"
err_msg_aborted=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Failed to call 'std2kern'"
err_msg_std2kern=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Failed to call 'make modules'"
err_msg_make_modules=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Failed to copy file"
err_msg_cp_file=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Failed to fix up kernel include file"
err_msg_fix_bitops_h=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="ERROR: Can't compile tty driver. Please ensure 'mount /dev/shm /tmp -t tmpfs' is working"
err_msg_tmpfs=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="ERROR: Failed to execute command"
err_msg_exec_cmd=$((cur))
cur=$(($cur+1))
# --------------------------------------------------
ErrMsg[$cur]="Can not find kompiler"
err_msg_compiler=$((cur))

# --------------------------------------------------
#  Fatal exit. Print error message and exit
# --------------------------------------------------
fatal_exit()
{
	clear
	echo ""
	echo " ---------------------------------------------------------"
	echo -e "  ERROR\t:\t$1"
	echo "#! LOG ABORT EXECUTION DUE TO ERROR  : $1" >> ${LOG} 2>&1
	if [ $(($#)) -gt 1 ]
	then
	echo -e "  MISSING:\t$2"
	echo "#! LOG ERROR INFO: $2" >> ${LOG} 2>&1
	fi
	echo ""
	echo "  Execution aborted, plese see '${LOG}' file"
	echo "  for details"
	echo " ---------------------------------------------------------"
	echo ""
	exit 1
}

#
# First name  - found configuration
# Second name - configuration created by "make menuconfig"
#
fix_up_kernel_config()
{
	original="${TMP}.1"
	config="${TMP}.2"

	sed -e "/^$/d;/^ *$/d;" $1         | sed -e "s/=.*$//" - | sort > $original 2>>${LOG}
	sed -e "/^ *#/d;/^ *$/d;/^$/d;" $2 | sed -e "s/=.*$//" - | sort > $config 2>>${LOG}

	diff ${original} ${config} | grep ">" | sed -e "s/>/#/" -e "s/$/ is not set/" - >> $1
}

# --------------------------------------------------
# Compare kernel versions that results in 'uname -r'
# and is located in the kernel Makefile
# --------------------------------------------------
compare_versions()
{
	k_version_name=$(uname -r)

	k_version=$(sed -ne "/^ *VERSION *=/p" ${KSRC}/Makefile | sed -e "s/^.*= *//" -)
	k_patchlevel=$(sed -ne "/^ *PATCHLEVEL *=/p" ${KSRC}/Makefile | sed -e "s/^.*= *//" -)
	k_sublevel=$(sed -ne "/^ *SUBLEVEL *=/p" ${KSRC}/Makefile | sed -e "s/^.*= *//" -)

	k_r_version=$(echo $k_version_name | sed -e "s/\..*//" -)
	k_r_patchlevel=$(echo $k_version_name | sed -e "s/^${k_r_version}\.//" -e "s/\..*//" -)
	k_r_sublevel=$(echo $k_version_name | sed -e "s/^${k_r_version}\.${k_r_patchlevel}\.//" -e "s/-.*//" -)

	echo "#+++ LOG INFO: 'uname -r': '$k_version_name'"      >>${LOG} 2>&1
	echo "#+++ LOG INFO: 'Version' : '${k_r_version}'.'${k_r_patchlevel}'.'${k_r_sublevel}'"  >>${LOG} 2>&1
	echo "#+++ LOG INFO: 'Makefile : '${k_version}'.'${k_patchlevel}'.'${k_sublevel}'"  >>${LOG} 2>&1

	if [ "$k_version" != "$k_r_version" ]
	then
		return 1
	fi
	if [ "$k_patchlevel" != "$k_r_patchlevel" ]
	then
		return 1
	fi
	if [ "$k_sublevel" != "$k_r_sublevel" ]
	then
		return 1
	fi

	return 0
}

#
# Check features for 2.4.X kernels
#
# The features are checked only in case running kernel version
# has the same version as source code
#
check_2_4_kernel_features()
{
	if [ $((detect_kernel_features)) -eq 0 ]
	then
		return
	fi

	echo "# LOG ---- START SECTION check 2.4 kernel features ------------------ " >> ${LOG} 2>&1

	compare_versions
	if [ $(($?)) -ne 0 ]
	then
		echo "#+++ LOG INFO: can not check for features: different versions"      >>${LOG} 2>&1
		return
	fi

# ---

	cat /proc/ksyms 2>/dev/null | grep notifier_chain_register > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: no notifier in /proc/ksyms"      >>${LOG} 2>&1
		return
	fi

	grep notifier_chain_register ${KSRC}/include/linux/notifier.h > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: no notifier in linux/notifier.h"      >>${LOG} 2>&1
		return
	fi

	cat /proc/ksyms 2>/dev/null | grep panic_notifier_list > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: no notifier list in /proc/ksyms"      >>${LOG} 2>&1
		return
	fi

	grep panic_notifier_list ${KSRC}/include/linux/kernel.h > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: no notifier list in linux/kernel.h"      >>${LOG} 2>&1
		return
	fi

	grep panic_notifier_list ${KSRC}/kernel/panic.c > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: no notifier list in kernel/panic.c"      >>${LOG} 2>&1
		return
	fi

	cat /proc/ksyms 2>/dev/null | fgrep register_reboot_notifier > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: no reboot notifier in /proc/ksyms"      >>${LOG} 2>&1
		return
	fi

	grep register_reboot_notifier ${KSRC}/include/linux/reboot.h > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: no reboot notifier in linux/reboot.h"      >>${LOG} 2>&1
		return
	fi

	echo -e "detected kernel notifier ... \c"

	echo "#++ LOG INFO: detected feature notifier"      >>${LOG} 2>&1

	kernel_2_4_feature_notifier=1

# ---

	echo "# LOG ---- START SECTION check 2.4 kernel features ------------------ " >> ${LOG} 2>&1
}

#
# Parameter name  - found configuration
#
fix_up_kernel_config_if_necessary()
{
	echo "#+++ LOG INFO: check if '$1' fix up is necessary" >> ${LOG} 2>&1
	grep "#.*is not set" $1 > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		echo "#+++ LOG INFO: fix up is necessary, invoke fix up procedure" >> ${LOG} 2>&1
		echo -e "'$1' is inconsistent, fixing up ... \c"
		echo ""
		echo ""

		call_make_menuconfig

		echo ""
		echo ""

		cd ${KSRC} > /dev/null 2>>${LOG}
		echo "#+++ LOG INFO: kernel dir:$(pwd)" >> ${LOG} 2>&1
		cp -f $1 ${TMP}.config

		fix_up_kernel_config ${TMP}.config ${KSRC}/.config

		echo "#+++ LOG INFO: call 'make mrproper' after kernel config fix-up" >> ${LOG} 2>&1
		echo -e "call 'make mrproper' ... \c"
		make mrproper > /dev/null 2>>${LOG}
		if [ $((kernel_major)) -gt $((4)) ]
		then
			echo "#+++ LOG INFO: call 'make mrproper' second time" >> ${LOG} 2>&1
			make mrproper > /dev/null 2>>${LOG}
		fi
		cd ${HERE} > /dev/null 2>>${LOG}
		echo "#+++ LOG INFO: finale dir:$(pwd)" >> ${LOG} 2>&1

		echo -e "update '${KSRC}/.config' ... \c"

		cp -f ${TMP}.config ${KSRC}/.config > /dev/null 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_sed]}" "${KSRC}/.config"
		fi
	fi
}

# --------------------------------------------------
#  Missing '.config' kernel configuration file
#  now try to locate appropriate file
# --------------------------------------------------
locate_kernel_config()
{

	echo "#+++ LOG INFO: call 'make mrproper'" >> ${LOG} 2>&1
	cd ${KSRC} >>${LOG} 2>&1
	echo "#+++ LOG INFO: kernel pwd:$(pwd)" >> ${LOG} 2>&1
	echo -e "call 'make mrproper' ... \c"
	make mrproper > /dev/null 2>>${LOG}
	cd ${HERE} >>${LOG} 2>&1
	echo "#+++ LOG INFO: final pwd:$(pwd)" >> ${LOG} 2>&1
	echo "#+++ LOG INFO: call 'make mrproper' ok" >> ${LOG} 2>&1

	echo "#+++ LOG INFO: check /boot/vmlinuz.config" >> ${LOG} 2>&1
	if [ -s /boot/vmlinuz.config ]
	then
		echo "#+++ LOG INFO: foung" >> ${LOG} 2>&1
		cp /boot/vmlinuz.config ${KSRC}/.config 2>>${LOG}
		echo -e "found /boot/vmlinuz.config ... \c"
		fix_up_kernel_config_if_necessary /boot/vmlinuz.config

		if [ ! -s ${KSRC}/include/linux/version.h ]
		then
			if [ -s /boot/vmlinuz.version.h ]
			then
				echo "#+++ LOG INFO: use /boot/vmlinuz.version.h" >> ${LOG} 2>&1
				cp -f /boot/vmlinuz.version.h ${KSRC}/include/linux/version.h > /dev/null 2>>${LOG}
				echo -e "found /boot/vmlinuz.version.h ... \c"
			else
				missing_version_h=1
			fi
		fi

		return
	fi

	cfg_fname="/boot/config-$(uname -r)"
	ver_fname="/boot/version-$(uname -r)"
	echo "#+++ LOG INFO: check ${cfg_fname}" >> ${LOG} 2>&1
	if [ -s ${cfg_fname} ]
	then
		echo "#+++ LOG INFO: foung" >> ${LOG} 2>&1
		cp ${cfg_fname} ${KSRC}/.config 2>>${LOG}
		echo -e "found ${cfg_fname} ... \c"
		fix_up_kernel_config_if_necessary ${cfg_fname}

		if [ ! -s ${KSRC}/include/linux/version.h ]
		then
			if [ -s ${ver_fname} ]
			then
				echo "#+++ LOG INFO: foung ${ver_fname}" >> ${LOG} 2>&1
				cp -f ${ver_fname} ${KSRC}/include/linux/version.h >> ${LOG} 2>&1
				echo -e "found ${ver_fname} ... \c"
			else
				missing_version_h=1
			fi
		fi

		return
	fi

	echo "#+++ LOG INFO: check /proc/config.gz" >> ${LOG} 2>&1
	if [ -f /proc/config.gz ]
	then
		echo "#+++ LOG INFO: foung" >> ${LOG} 2>&1
		zcat /proc/config.gz > ${KSRC}/.config 2>>${LOG}
		echo -e "found /proc/config.gz ... \c"
		if [ ! -s ${KSRC}/include/linux/version.h ]
		then
			missing_version_h=1
		fi
		return
	fi
}

# --------------------------------------------------
#  Check location of the kernel source tree
# --------------------------------------------------
check_files()
{
	cur=1

	echo "# LOG ---- START SECTION check_files ------------------ " >> ${LOG} 2>&1

	while [ $((${CEntryType[$cur]})) -gt 0 ]
	do
	  echo "#+ LOG START PROCESS STEP $((cur)): ${CEntryName[$cur]}" >> ${LOG} 2>&1
		case $((${CEntryType[$cur]})) in
			1) if [ ! -d ${CEntryName[$cur]} ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi ;;
			2) if [ ! -s ${CEntryName[$cur]} ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi ;;
			3) echo "ALL:" > test.mak 2>>${LOG}
         echo -e "\t@echo OK" >> test.mak 2>>${LOG}
				 make -f test.mak > /dev/null 2>>${LOG}
				 if [ $(($?)) -ne 0 ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi ;;
			4) echo "OK" > test.grep 2>>${LOG}
				 grep OK test.grep > /dev/null 2>>${LOG}
				 if [ $(($?)) -ne 0 ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi ;;
			5) echo "TEST" > test.sed 2>>${LOG}
				 sed -e "s/TEST/OK/" test.sed > /dev/null 2>>${LOG}
				 if [ $(($?)) -ne 0 ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi ;;
			7) echo 'int main (int argc, char** argv) { return (0); }' > test.c 2>>${LOG}
				 gcc test.c -o test.gcc > /dev/null 2>>${LOG}
				 if [ $(($?)) -ne 0 ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi
				 ./test.gcc > /dev/null 2>>${LOG}
				 if [ $(($?)) -ne 0 ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi ;;
	 '11') echo 'int main (int argc, char** argv) { return (0); }' > test.cpp 2>>${LOG}
				 gcc -c test.cpp -o test.o > /dev/null 2>>${LOG}
				 if [ $(($?)) -ne 0 ]
				 then
           fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi ;;
	 '12') cp -f $KSRC/Makefile conftest.make               2>>${LOG}
				 echo -e "conftest.PATCHLEVEL:" >>conftest.make   2>>${LOG}
				 echo -e "\t@echo \$(PATCHLEVEL)" >>conftest.make 2>>${LOG}
				 echo "" >>conftest.make 2>>${LOG}
				 echo -e "conftest.SUBLEVEL:" >>conftest.make   2>>${LOG}
				 echo -e "\t@echo \$(SUBLEVEL)" >>conftest.make 2>>${LOG}
				 here=`pwd`
				 kernel_major=`cd $KSRC; make --no-print-directory -f $here/conftest.make conftest.PATCHLEVEL`
				 kernel_major=$((kernel_major))
         if [ $((kernel_major)) -le $((0)) ]
				 then
					 fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				 fi
				 kernel_minor=`cd $KSRC; make --no-print-directory -f $here/conftest.make conftest.SUBLEVEL`
				 kernel_minor=$((kernel_minor))
				 if [ $((kernel_major)) -gt $((4)) ]
				 then
					 modules_subdir="drivers/isdn/hardware/eicon"
					 kernelcapi_subdir="drivers/isdn/capi"
					 capi_subdir="${kernelcapi_subdir}"
					 object_suffix="ko"
				 fi
	     	 echo "#++ LOG INFO kernel major $((kernel_major))" >> ${LOG} 2>&1
				 ;;
   '14') echo 'test -r test'  > test.1
				 echo -e test -r test > test.2
         diff test.1 test.2 >> ${LOG} 2>&1
         if [ $(($?)) -ne $((0)) ]
         then
           fatal_exit "${CEntryErr[$cur]}" "Please check /bin/bash version"
         fi
         ;;
		'15') if [ $((kernel_major)) -eq $((4)) ]
					then
						check_2_4_kernel_features
					fi
				 ;;
			8) tar -ztvf src/kernel.tgz > /dev/null 2>>${LOG}
				 if [ $(($?)) -ne 0 ]
				 then
				   gunzip -c src/kernel.tgz > /dev/null 2>>${LOG}
				   if [ $(($?)) -ne 1 ]
				   then
					 gunzip -c src/kernel.tgz | tar -tv > /dev/null 2>>${LOG}
					 if [ $(($?)) -eq 1 ]
					 then
					   fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
					 fi
				   else
					 fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
				   fi
				 fi ;;

			9) if [ ! -s ${CEntryName[$cur]} -o $((force_mrproper)) -ne $((0)) ]
				 then
					 if [ $((force_mrproper)) -ne 0 ]
					 then
	        	 echo -e "force 'make mrproper' ... \c"
	       		 echo "#++ LOG forcing 'make mrproper'" >> ${LOG} 2>&1
					 else
	        	 echo -e "missing '.config', try to detect ... \c"
	       		 echo "#++ LOG missing '.config', try to detect" >> ${LOG} 2>&1
					 fi
           locate_kernel_config
					 if [ ! -s ${CEntryName[$cur]} ]
					 then
	         	 echo "#+++ LOG failed to locate '.config', abort" >> ${LOG} 2>&1
						 fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
					 fi
         else
           echo "#++ LOG found '.config', preserved" >> ${LOG} 2>&1
				 fi ;;
			'10') if [ ! -d ${CEntryName[$cur]} ]
					then
						echo "#+++ LOG failed to locate '${KSRC}', try to detect" >> ${LOG} 2>&1
						if [ -d "/usr/src/linux-2.4" ]
						then
							ln -sf /usr/src/linux-2.4 ${KSRC}
							echo "#+++ LOG created symbolic link from /usr/src/linux-2.4 to ${KSRC}" >> ${LOG} 2>&1
						fi
					fi
					if [ ! -d ${CEntryName[$cur]} ]
					then
						tmp_name="kernel-source-$(uname -r | sed -e "s/-.*$//" -)"
						if [ -d /usr/src/${tmp_name} ]
						then
							ln -sf /usr/src/${tmp_name} ${KSRC}
							echo "#+++ LOG created symbolic link from /usr/src/${tmp_name} to ${KSRC}" >> ${LOG} 2>&1
						fi
					fi
					if [ ! -d ${CEntryName[$cur]} ]
					then
						echo "#+++ LOG failed to locate '${KSRC}', abort" >> ${LOG} 2>&1
						fatal_exit "${CEntryErr[$cur]}" "${CEntryName[$cur]}"
					fi ;;

			*) fatal_exit "check_files"
				;;
		esac
	  echo "#+ LOG END PROCESS STEP: $((cur))" >> ${LOG} 2>&1

		cur=$(($cur+1))
	done

	echo "# LOG ---- END SECTION check_files ------------------ " >> ${LOG} 2>&1
}

# -------------------------------------------------
# Show status messages
# -------------------------------------------------
show_status_start()
{
	echo -e "PROCESSING: ${SMsg[$cur_s_msg]} ... \c"
}

show_status_end()
{
	echo -e "OK"
	cur_s_msg=$(($cur_s_msg+1))
}

skip_status()
{
	echo -e "SKIP (due to command line option)"
	cur_s_msg=$(($cur_s_msg+1))
}

skip_status_silently()
{
	cur_s_msg=$(($cur_s_msg+1))
}

# -------------------------------------------------
# Fix kernel headers
# -------------------------------------------------
fix_kernel_headers()
{
	echo "# LOG ---- START SECTION fix_kernel_headers ------------------ " >> ${LOG} 2>&1

# ---
	echo "#+ LOG START PROCESS STEP: CONFIG_ISDN_BOOL" >> ${LOG} 2>&1

	grep "CONFIG_ISDN_BOOL" ${KSRC}/drivers/Makefile > /dev/null 2>&1
	if [ $(($?)) -eq 0 ]
	then
	 	echo "#++ LOG INFO: fix drivers/Makefile now" >> ${LOG} 2>&1

		sed -e "s/CONFIG_ISDN_BOOL/CONFIG_ISDN/" ${KSRC}/drivers/Makefile > ${TMP} 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_isdn_config_bool_sed]}" "${KSRC}/drivers/Makefile"
		fi
		cp -f ${TMP} "${KSRC}/drivers/Makefile" > /dev/null 2>&1
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_isdn_config_bool_cp]}" "${KSRC}/drivers/Makefile"
		fi
	 	echo "#++ LOG INFO: fix of drivers/Makefile completed" >> ${LOG} 2>&1
	else
	 echo "#++ LOG INFO: not necessary to fix" >> ${LOG} 2>&1
	fi

	echo "#+ LOG END PROCESS STEP: CONFIG_ISDN_BOOL" >> ${LOG} 2>&1
#---
	echo "#+ LOG START PROCESS STEP: inclide/linux/malloc.h #error" >> ${LOG} 2>&1

	if [ -s ${KSRC}/include/linux/malloc.h ]
	then
		grep "#error" ${KSRC}/include/linux/malloc.h > /dev/null 2>&1
		if [ $(($?)) -eq 0 ]
		then
	 		echo "#++ LOG INFO: fix #error" >> ${LOG} 2>&1

			sed -e "s/#error/#warning/" ${KSRC}/include/linux/malloc.h > ${TMP} 2>>${LOG}
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_malloc_h_sed]}" "${KSRC}/include/linux/malloc.h"
			fi
			cp -f ${TMP} "${KSRC}/include/linux/malloc.h" > /dev/null 2>&1
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_malloc_h_cp]}" "${KSRC}/include/linux/malloc.h"
			fi
		 	echo "#++ LOG INFO: fix of include/linux/malloc.h completed" >> ${LOG} 2>&1
		else
			echo "#++ LOG INFO: not necessary to fix" >> ${LOG} 2>&1
		fi
	fi

	echo "#+ LOG END PROCESS STEP: inclide/linux/malloc.h #error" >> ${LOG} 2>&1
#---
	echo "#+ LOG START PROCESS STEP: inclide/linux/sched.h, set_user_nice" >> ${LOG} 2>&1

	grep "DIVA_OS_SET_USER_NICE_SUPPORTED" ${KSRC}/include/linux/sched.h > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		grep "set_user_nice" ${KSRC}/include/linux/sched.h > /dev/null 2>&1
		if [ $(($?)) -eq 0 ]
		then
			echo "#++ LOG INFO: found set_user_nice" >> ${LOG} 2>&1
			echo "#ifndef DIVA_OS_SET_USER_NICE_SUPPORTED"   >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			echo "#define DIVA_OS_SET_USER_NICE_SUPPORTED 1" >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			echo "#endif"                                    >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_sched_h]}" "${KSRC}/include/linux/sched.h"
			fi
		fi
	else
		echo "#++ LOG INFO: DIVA_OS_SET_USER_NICE_SUPPORTED already found" >> ${LOG} 2>&1
	fi

	echo "#+ LOG END PROCESS STEP: include/linux/sched.h, set_user_nice" >> ${LOG} 2>&1
#---
	echo "#+ LOG START PROCESS STEP: inclide/linux/sched.h, SCHED_OTHER" >> ${LOG} 2>&1

	grep "SCHED_OTHER" ${KSRC}/include/linux/sched.h > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		grep "SCHED_NORMAL" ${KSRC}/include/linux/sched.h > /dev/null 2>&1
		if [ $(($?)) -eq 0 ]
		then
			echo "#++ LOG INFO: found SCHED_NORMAL" >> ${LOG} 2>&1
			echo "#ifndef SCHED_OTHER"              >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			echo "#define SCHED_OTHER SCHED_NORMAL" >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			echo "#endif"                           >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_sched_h]}" "${KSRC}/include/linux/sched.h"
			fi
    else
		  echo "#++ LOG WARNING: SCHED_OTHER not found and SCHED_NORMAL is MISSING, use SCHED_RR" >> ${LOG} 2>&1
			echo "#++ LOG INFO: found SCHED_NORMAL" >> ${LOG} 2>&1
			echo "#ifndef SCHED_OTHER"              >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			echo "#define SCHED_OTHER SCHED_RR"     >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			echo "#endif"                           >> ${KSRC}/include/linux/sched.h 2>>${LOG}
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_sched_h]}" "${KSRC}/include/linux/sched.h"
			fi
		fi
	else
		echo "#++ LOG INFO: SCHED_OTHER already found" >> ${LOG} 2>&1
	fi

	echo "#+ LOG END PROCESS STEP: inclide/linux/sched.h, SCHED_OTHER" >> ${LOG} 2>&1
#---
	echo "#+ LOG START PROCESS STEP: inclide/linux/sched.h, sigmask_lock" >> ${LOG} 2>&1

	grep "sigmask_lock" ${KSRC}/include/linux/sched.h > /dev/null 2>&1
	if [ $(($?)) -ne 0 ]
	then
		echo "#+++ LOG INFO: start fix sigmask_lock" >> ${LOG} 2>&1
		echo "#ifndef SIGMASK_LOCK_DIVA_DEFINE"      >> ${KSRC}/include/linux/sched.h 2>>${LOG}
		echo "#define SIGMASK_LOCK_DIVA_DEFINE 1"    >> ${KSRC}/include/linux/sched.h 2>>${LOG}
		echo "#define sigmask_lock sighand->siglock" >> ${KSRC}/include/linux/sched.h 2>>${LOG}
		echo "#endif"                                >> ${KSRC}/include/linux/sched.h 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			echo "#+++ LOG INFO: sigmask_lock fix failed" >> ${LOG} 2>&1
			fatal_exit "${ErrMsg[$err_msg_sched_h]}" "${KSRC}/include/linux/sched.h"
		fi
		echo "#+++ LOG INFO: end fix sigmask_lock" >> ${LOG} 2>&1
	fi

	echo "#+ LOG END PROCESS STEP: inclide/linux/sched.h, sigmask_lock" >> ${LOG} 2>&1
#---

	echo "# LOG ---- END SECTION fix_kernel_headers ------------------ " >> ${LOG} 2>&1
}

# -------------------------------------------------
# Fix tty headers
# -------------------------------------------------
fix_tty_headers()
{
	echo "# LOG ---- START SECTION fix_tty_headers ------------------ " >> ${LOG} 2>&1

#---

	echo "#+ LOG START PROCESS STEP: inclide/linux/tty.h, count" >> ${LOG} 2>&1

	cp -f $KSRC/Makefile conftest.make                                2>>${LOG}
	echo -e "conftest.CC:" >>conftest.make                            2>>${LOG}
	echo -e "\t@echo \$(CC) \$(CFLAGS) \$(MODFLAGS)" >>conftest.make  2>>${LOG}
	tty_test_compiler=$(cd $KSRC 2>>${LOG}; make --no-print-directory -f $HERE/conftest.make conftest.CC 2>>${LOG})
	cd ${HERE} >>${LOG} 2>&1
	echo "#+++ LOG INFO: final pwd:$(pwd)" >> ${LOG} 2>&1
	echo "#+++ LOG INFO: tty_test_compiler: '${tty_test_compiler}'" >> ${LOG} 2>&1
	rm -f conftest.make

	echo "#++ STEP: check tty->count" >> ${LOG} 2>&1

	echo "#include <linux/config.h>" >  test.c 2>>${LOG}
	echo "#include <linux/module.h>" >> test.c 2>>${LOG}
	echo "#include <linux/types.h>"  >> test.c 2>>${LOG}
	echo "#include <linux/kernel.h>" >> test.c 2>>${LOG}
	echo "#include <linux/tty.h>"    >> test.c 2>>${LOG}
	echo 'int diva_tty_test (struct tty_struct *tty) {' >> test.c 2>>${LOG}
	echo '  if (tty->count == 1) return 1;'             >> test.c 2>>${LOG}
	echo '  return 0;'                                  >> test.c 2>>${LOG}
	echo '}'                                            >> test.c 2>>${LOG}
	$tty_test_compiler -c test.c -o test.o > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		echo "#+++ INFO: tty->count does not work"      >> ${LOG} 2>&1
		echo "#+++ STEP: check atomic_read(&(tty->count))" >> ${LOG} 2>&1

		echo "#include <linux/config.h>" >  test.c 2>>${LOG}
		echo "#include <linux/module.h>" >> test.c 2>>${LOG}
		echo "#include <linux/types.h>"  >> test.c 2>>${LOG}
		echo "#include <linux/kernel.h>" >> test.c 2>>${LOG}
		echo "#include <linux/tty.h>"    >> test.c 2>>${LOG}
		echo 'int diva_tty_test (struct tty_struct *tty) {' >> test.c 2>>${LOG}
		echo '  if (atomic_read(&(tty->count)) == 1) return 1;'             >> test.c 2>>${LOG}
		echo '  return 0;'                                  >> test.c 2>>${LOG}
		echo '}'                                            >> test.c 2>>${LOG}
		$tty_test_compiler -c test.c -o test.o > /dev/null 2>>${LOG}
		if [ $(($?)) -eq 0 ]
		then
			echo "#+++ INFO: OK, atomic_read(&(tty->count)) works"          >> ${LOG} 2>&1
			grep "DIVA_TTY_COUNT_ATOMIC" ${KSRC}/include/linux/tty.h > /dev/null 2>&1
			if [ $(($?)) -ne 0 ]
			then
				echo "#+++ INFO: add DIVA_TTY_COUNT_ATOMIC to tty.h" >> ${LOG} 2>&1
				echo "#ifndef DIVA_TTY_COUNT_ATOMIC"   >> ${KSRC}/include/linux/tty.h 2>>${LOG}
				echo "#define DIVA_TTY_COUNT_ATOMIC 1" >> ${KSRC}/include/linux/tty.h 2>>${LOG}
				echo "#endif"                          >> ${KSRC}/include/linux/tty.h 2>>${LOG}
				if [ $(($?)) -ne 0 ]
				then
					fatal_exit "${ErrMsg[$err_msg_tty_h]}" "${KSRC}/include/linux/tty.h"
				fi
			else
				echo "#+++ INFO: tty.h already contains DIVA_TTY_COUNT_ATOMIC, leave unchanged" >> ${LOG} 2>&1
			fi
		else
			echo "#+++ WARNING: atomic_read(tty->count) failed. Leave tty.h unchanged"   >> ${LOG} 2>&1
		fi
	else
		echo "#+++ INFO: OK, tty->count works"          >> ${LOG} 2>&1
	fi

	echo "#+ LOG END PROCESS STEP: inclide/linux/tty.h, count" >> ${LOG} 2>&1

#---

	echo "# LOG ---- END SECTION fix_tty_headers ------------------ " >> ${LOG} 2>&1
}

# -------------------------------------------------
# Update kernel configuration file
# -------------------------------------------------
update_kernel_configuration()
{
	echo "# LOG ---- START SECTION update_kernel_configuration ------------------ " >> ${LOG} 2>&1

	grep "ISDN" ${KSRC}/.config >> ${LOG} 2>&1
	if [ $(($?)) -eq 0 ]
	then
		echo "#++ LOG INFO: remove existing ISDN configuration" >> ${LOG} 2>&1
		sed -e "/ISDN/d" ${KSRC}/.config > ${TMP} 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_sed]}" "${KSRC}/.config"
		fi
		cp -f ${TMP} "${KSRC}/.config" > /dev/null 2>&1
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_cp]}" "${KSRC}/.config"
		fi
	fi

	grep "HYSDN" ${KSRC}/.config >> ${LOG} 2>&1
	if [ $(($?)) -eq 0 ]
	then
		echo "#++ LOG INFO: remove existing ISDN configuration" >> ${LOG} 2>&1
		sed -e "/HYSDN/d" ${KSRC}/.config > ${TMP} 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_sed]}" "${KSRC}/.config"
		fi
		cp -f ${TMP} "${KSRC}/.config" > /dev/null 2>&1
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_cp]}" "${KSRC}/.config"
		fi
	fi

	echo "#++ LOG INFO: write new ISDN configuration" >> ${LOG} 2>&1

	echo "CONFIG_ISDN=m"                             >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_KCAPI=m"                       >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_KCAPI_NO_INFOSTR=y"            >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_KCAPI_CAPI20=m"                >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_DIVAS=m"                       >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_DIVAS_BRIPCI=y"                >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_DIVAS_4BRIPCI=y"               >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_DIVAS_PRIPCI=y"                >> ${KSRC}/.config 2>>${LOG}
  echo "CONFIG_ISDN_DIVAS_DIVACAPI=m"              >> ${KSRC}/.config 2>>${LOG}

  echo '# CONFIG_ISDN_MOD_BOOL is not set'         >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_MOD is not set'              >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_KCAPI_VERBOSE is not set'    >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_KCAPI_MIDDLEWARE is not set' >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_KCAPI_CAPIDRV is not set'    >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_DRV_ICN is not set'          >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_DRV_PCBIT is not set'        >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_DRV_SC is not set'           >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_DRV_ACT2000 is not set'      >> ${KSRC}/.config 2>>${LOG}
  echo '# CONFIG_ISDN_DIVAS_DIVA2I4L is not set'   >> ${KSRC}/.config 2>>${LOG}
	echo '# CONFIG_ISDN_CAPI is not set'             >> ${KSRC}/.config 2>>${LOG}
	echo '# CONFIG_HYSDN is not set'                 >> ${KSRC}/.config 2>>${LOG}
	echo '# CONFIG_HYSDN_CAPI is not set'            >> ${KSRC}/.config 2>>${LOG}
	echo '# CONFIG_ISDN_BLA is not set'              >> ${KSRC}/.config 2>>${LOG}

	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_write]}" "${KSRC}/.config"
	fi

	echo "#++ LOG INFO: remove 'autoconf.h'" >> ${LOG} 2>&1
	rm -f ${KSRC}/include/linux/autoconf.h

	echo "# LOG ---- END SECTION update_kernel_configuration ------------------ " >> ${LOG} 2>&1
}

# -------------------------------------------------
# Update kernel configuration file
# -------------------------------------------------
update_kernel_2_configuration()
{
	echo "# LOG ---- START SECTION update_kernel_2_configuration ------------------ " >> ${LOG} 2>&1

	echo "#++ LOG INFO: remove existing ISDN configuration" >> ${LOG} 2>&1

	sed -e "/ISDN/d;" -e "/CAPI/d;" -e "/HISAX/d;" -e "/HYSDN/d;" -e "/CONFIG_DE_AOC/d;" ${KSRC}/.config > ${TMP} 2>>${LOG}
	if [ $(($?)) -ne $((0)) ]
	then
		fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_sed]}" "${KSRC}/.config"
	fi

	echo "#++ LOG INFO: update ISDN configuration" >> ${LOG} 2>&1

	echo "" >> ${TMP} 2>>${LOG}

	echo 'CONFIG_ISDN=m' >> ${TMP} 2>>${LOG}
	echo '# CONFIG_ISDN_I4L is not set' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_CAPI=m' >> ${TMP} 2>>${LOG}
	echo '# CONFIG_ISDN_DRV_AVMB1_VERBOSE_REASON is not set' >> ${TMP} 2>>${LOG}
	echo '# CONFIG_ISDN_CAPI_MIDDLEWARE is not set' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_CAPI_CAPI20=m' >> ${TMP} 2>>${LOG}
	echo '# CONFIG_CAPI_AVM is not set' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_CAPI_EICON=y' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_DIVAS=m' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_DIVAS_BRIPCI=y' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_DIVAS_PRIPCI=y' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_DIVAS_ANALOG=y' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_DIVAS_DIVACAPI=m' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_DIVAS_USERIDI=m' >> ${TMP} 2>>${LOG}
	echo 'CONFIG_ISDN_DIVAS_MAINT=m' >> ${TMP} 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_write]}" "${KSRC}/.config"
	fi

	echo "#++ LOG INFO: write new ISDN configuration" >> ${LOG} 2>&1

	cp -f ${TMP} ${KSRC}/.config >> ${LOG} 2>&1

	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_fix_kernel_cfg_cp]}" "${KSRC}/.config"
	fi

	echo "# LOG ---- END SECTION update_kernel_2_configuration ------------------ " >> ${LOG} 2>&1
}

# --------------------------------------------------
# Calls 'make menuconfig'
# --------------------------------------------------
call_make_menuconfig()
{
	echo "# LOG ---- START SECTION 'make menuconfig' ------------------ " >> ${LOG} 2>&1

	echo "#+ LOG INFO: pwd:$(pwd)"            >>${LOG} 2>&1
	cd ${KSRC}                                >>${LOG} 2>&1
	echo "#+ LOG INFO: kernel dir pwd:$(pwd)" >>${LOG} 2>&1
	echo -e "\t" >> ${KSRC}/diva.menu.ctrl    2>>${LOG}
	echo    ""   >> ${KSRC}/diva.menu.ctrl    2>>${LOG}
	echo    ""   >> ${KSRC}/diva.menu.ctrl    2>>${LOG}
	echo    ""   >> ${KSRC}/diva.menu.ctrl    2>>${LOG}
	echo    ""   >> ${KSRC}/diva.menu.ctrl    2>>${LOG}
	echo    ""   >> ${KSRC}/diva.menu.ctrl    2>>${LOG}
	make menuconfig < ${KSRC}/diva.menu.ctrl  2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		cd $HERE >> ${LOG} 2>&1
		echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
		fatal_exit "${ErrMsg[$err_msg_make_menuconfig]}" "make menuconfig"
	fi
	cd $HERE >> ${LOG} 2>&1
	echo "#+ LOG INFO: final pwd:$(pwd)"      >>${LOG} 2>&1

	echo "# LOG ---- END SECTION 'make menuconfig' ------------------ " >> ${LOG} 2>&1


#---
# Should be fixed here due to fact that it is possible that 'asm' link not exists
# in the original kernel
#---
	echo "#+ LOG START PROCESS STEP: inclide/asm/bitops.h 'word'" >> ${LOG} 2>&1

	bitops_name="${KSRC}/include/asm/bitops.h"
	if [ ! -f "${bitops_name}" ]
	then
		bitops_name="${KSRC}/include/asm-${ARCHITECTURE}/bitops.h"
	fi


	if [ -f "${bitops_name}" ]
	then

		echo "#+ LOG INFO: found ${bitops_name}"      >>${LOG} 2>&1

		if [ ! -f "${bitops_name}.diva" ]
		then
			cp -f ${bitops_name} ${bitops_name}.diva 2>>${LOG}
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
			fi
		fi
		sed -e "s/word/p1/g" ${bitops_name} > ${TMP} 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
		fi
		cp -f ${TMP} ${bitops_name} 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			cp -f ${bitops_name}.diva ${bitops_name} 2>>${LOG}
			fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
		fi
	else
		echo "#+ LOG INFO: bitops.h not found"      >>${LOG} 2>&1
	fi

	echo "#+ LOG END PROCESS STEP: inclide/asm/bitops.h 'word'" >> ${LOG} 2>&1

#---

  echo "#+ LOG START PROCESS STEP: inclide/linux/bitops.h 'word'" >> ${LOG} 2>&1

  bitops_name="${KSRC}/include/linux/bitops.h"

  if [ -s "${bitops_name}" ]
  then

    echo "#+ LOG INFO: found ${bitops_name}"      >>${LOG} 2>&1

    if [ ! -f "${bitops_name}.diva" ]
    then
      cp -f ${bitops_name} ${bitops_name}.diva 2>>${LOG}
      if [ $(($?)) -ne 0 ]
      then
        fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
      fi
    fi
    sed -e "s/word/p1/g" ${bitops_name} > ${TMP} 2>>${LOG}
    if [ $(($?)) -ne 0 ]
    then
      fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
    fi
    cp -f ${TMP} ${bitops_name} 2>>${LOG}
    if [ $(($?)) -ne 0 ]
    then
      cp -f ${bitops_name}.diva ${bitops_name} 2>>${LOG}
      fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
    fi
  else
    echo "#+ LOG INFO: ${bitops_name} not found"      >>${LOG} 2>&1
  fi

  echo "#+ LOG END PROCESS STEP: inclide/include/bitops.h 'word'" >> ${LOG} 2>&1

#---
	echo "#+ LOG START PROCESS STEP: inclide/asm-x86_64/bitops.h 'word'" >> ${LOG} 2>&1


	bitops_name="${KSRC}/include/asm-x86_64/bitops.h"

	if [ -f "${bitops_name}" ]
	then

		echo "#+ LOG INFO: found ${bitops_name}"      >>${LOG} 2>&1

		if [ ! -f "${bitops_name}.diva" ]
		then
			cp -f ${bitops_name} ${bitops_name}.diva 2>>${LOG}
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
			fi
		fi
		sed -e "s/word/p1/g" ${bitops_name} > ${TMP} 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
		fi
		cp -f ${TMP} ${bitops_name} 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			cp -f ${bitops_name}.diva ${bitops_name} 2>>${LOG}
			fatal_exit "${ErrMsg[$err_msg_fix_bitops_h]}" "bitops.h"
		fi
	else
		echo "#+ LOG INFO: include/asm-x86_64/bitops.h not found"      >>${LOG} 2>&1
	fi

	echo "#+ LOG END PROCESS STEP: inclide/asm-x86_64/bitops.h 'word'" >> ${LOG} 2>&1
#---
}

# --------------------------------------------------
# Update kernel version file 'version.h'
# --------------------------------------------------
update_version_h()
{
	compare_versions
	if [ $(($?)) -ne 0 ]
	then
		echo "#+++ LOG INFO: can not update version.h, different versions"      >>${LOG} 2>&1
		return
	fi

	echo -e "update 'version.h' ... \c"

	if [ $((kernel_major)) -gt $((4)) ]
	then
		echo "#+ LOG INFO: pwd:$(pwd)"            >>${LOG} 2>&1
		cd ${KSRC}                                >>${LOG} 2>&1
		echo "#+ LOG INFO: kernel dir pwd:$(pwd)" >>${LOG} 2>&1
		make include/linux/version.h              >>${LOG} 2>&1
		if [ $(($?)) -ne 0 ]
		then
			cd $HERE >> ${LOG} 2>&1
			echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
			fatal_exit "${ErrMsg[$err_msg_make_dep]}" "make include/linux/version.h"
		fi
		cd $HERE >> ${LOG} 2>&1
		echo "#+ LOG INFO: final pwd:$(pwd)"      >>${LOG} 2>&1
	fi

	echo "#define UTS_RELEASE "\"$(uname -r)\" > ${TMP}                 2>>${LOG}
	sed -e "/UTS_RELEASE/d" ${KSRC}/include/linux/version.h >> ${TMP}  2>>${LOG}
	cp -f ${TMP} ${KSRC}/include/linux/version.h                        2>>${LOG}
	echo "#+++ LOG INFO START: use version.h"      >>${LOG} 2>&1
	cat ${KSRC}/include/linux/version.h            >>${LOG} 2>&1
	echo "#+++ LOG INFO END:   use version.h"      >>${LOG} 2>&1
	echo "#+++ LOG INFO START: update version.h time" >>${LOG} 2>&1
	touch -c -r ${KSRC}/Makefile ${KSRC}/include/linux/version.h 2>>${LOG}
	echo "#+++ LOG INFO END:   update version.h time" >>${LOG} 2>&1

	version_h_was_updated=1
}

# --------------------------------------------------
# Calls 'make menuconfig'
# --------------------------------------------------
call_make_dep()
{
	echo "# LOG ---- START SECTION 'make dep' ------------------ " >> ${LOG} 2>&1

	if [ $((kernel_major)) -le $((4)) ]
	then

		echo "#+ LOG INFO: cleanup obkect files in 'drivers/isdn/eicon'"            >>${LOG} 2>&1
		find ${KSRC}/drivers/isdn -name "*.${object_suffix}" -print | xargs rm -f > /dev/null 2>>${LOG}

		echo "#+ LOG INFO: pwd:$(pwd)"            >>${LOG} 2>&1
		cd ${KSRC}                                >>${LOG} 2>&1
		echo "#+ LOG INFO: kernel dir pwd:$(pwd)" >>${LOG} 2>&1
		make dep > /dev/null 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			cd $HERE >> ${LOG} 2>&1
			echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
			fatal_exit "${ErrMsg[$err_msg_make_dep]}" "make dep"
		fi
		cd $HERE >> ${LOG} 2>&1
		echo "#+ LOG INFO: final pwd:$(pwd)"      >>${LOG} 2>&1

		echo "#++ LOG INFO: check 'compile.h'"      >>${LOG} 2>&1
		if [ ! -s ${KSRC}/include/linux/compile.h ]
		then
			grep "compile.h" ${KSRC}/Makefile > /dev/null 2>>${LOG}
			echo -e "'compile.h' missing, create new file ... \c"
			echo "#+++ LOG INFO: create 'compile.h'"      >>${LOG} 2>&1
			cd ${KSRC} > /dev/null 2>>${LOG}
			echo "#+++ LOG INFO: pwd:$(pwd)"      >>${LOG} 2>&1
			make include/linux/compile.h > /dev/null 2>>${LOG}
			cd ${HERE} > /dev/null 2>>${LOG}
			echo "#+++ LOG INFO: final pwd:$(pwd)"      >>${LOG} 2>&1
		fi

	fi

	if [ $((kernel_major)) -gt $((4)) ]
	then
		find ${KSRC}/${modules_subdir} -name "*.${object_suffix}" -print | xargs rm -f > /dev/null 2>>${LOG}
		find ${KSRC}/${modules_subdir} -name "*.o" -print | xargs rm -f > /dev/null 2>>${LOG}
		find ${KSRC}/${kernelcapi_subdir} -name "*.${object_suffix}" -print | xargs rm -f > /dev/null 2>>${LOG}
		find ${KSRC}/${kernelcapi_subdir} -name "*.o" -print | xargs rm -f > /dev/null 2>>${LOG}
		find ${KSRC}/${capi_subdir} -name "*.${object_suffix}" -print | xargs rm -f > /dev/null 2>>${LOG}
		find ${KSRC}/${capi_subdir} -name "*.o" -print | xargs rm -f > /dev/null 2>>${LOG}
	fi

	if [ $((missing_version_h)) -ne 0 ]
	then
		echo "#++ LOG INFO: version.h was missing. Create new one"      >>${LOG} 2>&1
		update_version_h
	fi

	echo "# LOG ---- END SECTION 'make dep' ------------------ " >> ${LOG} 2>&1
}

# --------------------------------------------------
# Update kernel source tree
# --------------------------------------------------
update_kernel_source_tree()
{
	echo "# LOG ---- START SECTION 'update_kernel_source_tree' ------------------ " >> ${LOG} 2>&1

	rm -rf tmp > /dev/null 2>&1
	mkdir tmp > /dev/null 2>&1
	if [ ! -d tmp ]
	then
		echo "#+ LOG INFO: failed to create ${HERE}/tmp directory"            >>${LOG} 2>&1
		fatal_exit "${ErrMsg[$err_msg_std2kern]}" "tmp directory"
	fi
	cd tmp > /dev/null 2>>${LOG}
	echo "#+ LOG INFO: tmp pwd:$(pwd)"            >>${LOG} 2>&1

	if [ $((kernel_major)) -le $((4)) ]
	then
		echo "#++ LOG INFO: use kernel.tgz"           >>${LOG} 2>&1
		tar -zxf ../src/kernel.tgz >> ${LOG} 2>&1
	fi
	if [ $((kernel_major)) -gt $((4)) ]
	then
		echo "#++ LOG INFO: use kernel_2.tgz"         >>${LOG} 2>&1
		tar -zxf ../src/kernel_2.tgz >> ${LOG} 2>&1
	fi

	echo "#+ LOG INFO: call std2kern now"           >>${LOG} 2>&1

	if [ $((kernel_major)) -le $((4)) ]
	then
		./std2kern -d > /dev/null 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			cd $HERE >> ${LOG} 2>&1
			echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
			fatal_exit "${ErrMsg[$err_msg_std2kern]}" "std2kern -d"
		fi
	fi

	./std2kern    > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		cd $HERE >> ${LOG} 2>&1
		echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
		fatal_exit "${ErrMsg[$err_msg_std2kern]}" "std2kern"
	fi

	cd $HERE > /dev/null 2>>${LOG}
	rm -rf tmp

	echo "#+ LOG INFO: final pwd:$(pwd)"      >>${LOG} 2>&1


	if [ $((kernel_major)) -eq $((4)) ]
	then
		echo "#+ update maintidi.c features"               >>${LOG} 2>&1

		sed_cmd="s/# *define *FEATURE_NOTIFIER_SUPPORTED *.*/#define FEATURE_NOTIFIER_SUPPORTED $((kernel_2_4_feature_notifier))/"
		sed -e "${sed_cmd}" ${KSRC}/drivers/isdn/eicon/divamnt.c > ${KSRC}/drivers/isdn/eicon/divamnt.c.tmp 2>>${LOG}
		if [ $(($?)) -eq 0 ]
		then
			cp -f ${KSRC}/drivers/isdn/eicon/divamnt.c.tmp ${KSRC}/drivers/isdn/eicon/divamnt.c >> ${LOG} 2>&1
			cmp ${KSRC}/drivers/isdn/eicon/divamnt.c.tmp ${KSRC}/drivers/isdn/eicon/divamnt.c > /dev/null 2>&1
			if [ $(($?)) -ne 0 ]
			then
				echo "#+++ failed to copy updated maintidi.c" >>${LOG} 2>&1
			fi
		else
			echo "#+++ failed to update maintidi.c features" >>${LOG} 2>&1
		fi
		rm -f ${KSRC}/drivers/isdn/eicon/divamnt.c.tmp

		echo "#+ update maintidi.c features end"           >>${LOG} 2>&1
	fi


	echo "# LOG ---- END SECTION 'update_kernel_source_tree' ------------------ " >> ${LOG} 2>&1
}

# --------------------------------------------------
# Call make modules
# --------------------------------------------------
call_make_modules()
{
	echo "# LOG ---- START SECTION 'call_make_modules' ------------------ " >> ${LOG} 2>&1

	echo "#+ LOG INFO: pwd:$(pwd)"             >> ${LOG} 2>&1
	cd ${KSRC} > /dev/null 2>>${LOG}
	echo "#+ LOG INFO: kernel pwd:$(pwd)"      >> ${LOG} 2>&1

	if [ $((kernel_major)) -gt $((4)) ]
	then
		cp -f ${KSRC}/include/linux/version.h tmp.h         >> ${LOG} 2>&1
		echo "#+ LOG INFO: call modules_prepare"            >> ${LOG} 2>&1
		make modules_prepare                                >> ${LOG} 2>&1
		echo "#+ LOG INFO: end modules_prepare"             >> ${LOG} 2>&1
		cp -f tmp.h ${KSRC}/include/linux/version.h         >> ${LOG} 2>&1
		rm -f tmp.h >> ${LOG} 2>&1
	fi

	if [ $((src_only)) -ne $((0)) ]
	then
		cd $HERE > /dev/null 2>>${LOG}

		echo "#+ LOG INFO: final pwd:$(pwd)"       >>${LOG} 2>&1

		echo "# LOG ---- END SECTION 'call_make_modules' ------------------ " >> ${LOG} 2>&1

		echo "# INFO: build end due to source only option" >> ${LOG} 2>&1

		echo -e "SKIP (due to command line option)"

		echo ""
		echo "SUCCESS."
		echo ""

		exit 0
	fi

	make modules SUBDIRS=${modules_build_subdir}     >> ${LOG} 2>&1
	if [ $(($?)) -ne 0 ]
	then
		cd $HERE >> ${LOG} 2>&1
		echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
		fatal_exit "${ErrMsg[$err_msg_make_modules]}" "make modules"
	fi

	cd $HERE > /dev/null 2>>${LOG}

	echo "#+ LOG INFO: final pwd:$(pwd)"       >>${LOG} 2>&1

	echo "# LOG ---- END SECTION 'call_make_modules' ------------------ " >> ${LOG} 2>&1
}

# --------------------------------------------------
# Install diva modules
# --------------------------------------------------
install_diva_modules()
{
	echo "# LOG ---- START SECTION 'install_diva_modules' ------------------ " >> ${LOG} 2>&1

	echo -e "divadidd.${object_suffix}, \c"
	cp -f ${KSRC}/${modules_subdir}/divadidd.${object_suffix}         ${INSTALL_DIR} > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/divadidd.${object_suffix}"
	fi
	echo -e "divas.${object_suffix}, \c"
	cp -f ${KSRC}/${modules_subdir}/divas.${object_suffix}            ${INSTALL_DIR} > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/divas.${object_suffix}"
	fi
	echo -e "diva_mnt.${object_suffix}, \c"
	cp -f ${KSRC}/${modules_subdir}/diva_mnt.${object_suffix}         ${INSTALL_DIR} > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/diva_mnt.${object_suffix}"
	fi
	echo -e "diva_idi.${object_suffix}, \c"
	cp -f ${KSRC}/${modules_subdir}/diva_idi.${object_suffix}         ${INSTALL_DIR} > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/diva_idi.${object_suffix}"
	fi
	echo -e "divacapi.${object_suffix}, \c"
	cp -f ${KSRC}/${modules_subdir}/divacapi.${object_suffix}         ${INSTALL_DIR} > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/divacapi.${object_suffix}"
	fi
	echo -e "kernelcapi.${object_suffix}, \c"
	cp -f ${KSRC}/${kernelcapi_subdir}/kernelcapi.${object_suffix}  ${INSTALL_DIR} > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/kernelcapi.${object_suffix}"
	fi
	echo -e "capi.${object_suffix} \c"
	cp -f ${KSRC}/${capi_subdir}/capi.${object_suffix}            ${INSTALL_DIR} > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/capi.${object_suffix}"
	fi
	if [ -f src/mtpx.tgz ]
	then
		echo -e "diva_mtpx.${object_suffix} \c"
		cp -f diva_mtpx.${object_suffix} ${INSTALL_DIR} > /dev/null 2>>${LOG}
		if [ $(($?)) -ne 0 ]
		then
			fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/diva_mtpx.${object_suffix}"
		fi
	fi

	echo "# LOG ---- END SECTION 'install_diva_module' ------------------ " >> ${LOG} 2>&1
}

build_mtpx_adapter()
{
	mtpx_tmp_mount=1

	echo "# LOG ---- START SECTION 'build_mtpx_adapter' ------------------ " >> ${LOG} 2>&1
	rm -rf tmp > /dev/null 2>&1
	mkdir tmp > /dev/null 2>>${LOG}
	mount /dev/shm tmp -t tmpfs > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		mtpx_tmp_mount=0
		echo "#++ LOG INFO: failed to mount tmpfs ------------------ " >> ${LOG} 2>&1
	fi
	if [ ! -d tmp ]
	then
		echo "#+ LOG INFO: failed to create ${HERE}/tmp directory"            >>${LOG} 2>&1
		fatal_exit "${ErrMsg[$err_msg_std2kern]}" "tmp directory"
	fi
	cd tmp > /dev/null 2>>${LOG}
	echo "#+ LOG INFO: tmp pwd:$(pwd)"            >>${LOG} 2>&1
	tar -zxf ../src/mtpx.tgz >> ${LOG} 2>&1
	echo "#+ LOG INFO: pwd:$(pwd)"            >>${LOG} 2>&1
	make -C src/linux > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		cd $HERE >> ${LOG} 2>&1
		echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
		if [ $((mtpx_tmp_mount)) -ne $((0)) ]
		then
			umount tmp > /dev/null 2>>${LOG}
		fi
		rm -rf tmp
		fatal_exit "${ErrMsg[$err_msg_exec_cmd]}" "make mtpx"
	fi
	cp -f src/linux/diva_mtpx.${object_suffix} ../diva_mtpx.${object_suffix}
	if [ $(($?)) -ne 0 ]
	then
		cd $HERE >> ${LOG} 2>&1
		echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
		fatal_exit "${ErrMsg[$err_msg_exec_cmd]}" "cp diva_mtpx.${object_suffix}"
	fi
	cd $HERE > /dev/null 2>>${LOG}
	echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
	if [ $((mtpx_tmp_mount)) -ne $((0)) ]
	then
		umount tmp > /dev/null 2>>${LOG}
	fi
	rm -rf tmp

	echo "# LOG ---- END SECTION 'build_mtpx_adapter' ------------------ " >> ${LOG} 2>&1
}

# --------------------------------------------------
# Remove temporary files
# --------------------------------------------------
cleanup_tmp_files()
{
	echo "# LOG ---- START SECTION 'cleanup_tmp_files' ------------------ " >> ${LOG} 2>&1

	rm -f diva_mtpx.${object_suffix} Divatty.${object_suffix} test.* modules.log divas.tmp divas.tmp.* > /dev/null 2>&1
	rm -f conftest.make > /dev/null 2>&1
	umount tmp > /dev/null 2>&1
	rm -rf tmp > /dev/null 2>&1
	rm -f filelist > /dev/null 2>&1

	echo "# LOG ---- END SECTION 'cleanup_tmp_files' ------------------ " >> ${LOG} 2>&1
}

# --------------------------------------------------
# MAIN ENTRY
# --------------------------------------------------
clear
trap "cd $HERE; fatal_exit \"${ErrMsg[$err_msg_aborted]}\" ; exit 1" 1 2 15

echo "# --------------------------------------------------------------" >  ${LOG} 2>&1
echo "#"                                                                >> ${LOG} 2>&1
echo "# Diva build (c) Copyright by Eicon Networks 1993 - 2005"         >> ${LOG} 2>&1
echo "# Please read legal notice at begin of reference manual"          >> ${LOG} 2>&1
echo "# /usr/lib/eicon/divas/httpd/reference_manual.html"               >> ${LOG} 2>&1
echo "#"                                                                >> ${LOG} 2>&1
echo "# BUILD:             ${cur_build}"                                >> ${LOG} 2>&1
echo "# STARTED AT:        $(date)"                                     >> ${LOG} 2>&1
echo "# HOST:              $(hostname)"                                 >> ${LOG} 2>&1
echo "# WORKING DIRECTORY: ${HERE}"                                     >> ${LOG} 2>&1
echo "# LOG FILE:          ${LOG}"                                      >> ${LOG} 2>&1
echo "# OPTIONS:          ${options}"                                   >> ${LOG} 2>&1
echo "# TARGET:            ${INSTALL_DIR}"                              >> ${LOG} 2>&1
echo "#"                                                                >> ${LOG} 2>&1
usage_line=0
while [ ! -z "${UsageString[$usage_line]}" ]
do
echo "# ${UsageString[usage_line]}"                                     >> ${LOG} 2>&1
usage_line=$(($usage_line+1))
done

echo "#"                                                                >> ${LOG} 2>&1
echo "#   - COMPILE AND INSTALL DIVA DRIVERS -"                         >> ${LOG} 2>&1
echo "#"                                                                >> ${LOG} 2>&1
echo "#  INFO: Procedure includes update of kernel source"              >> ${LOG} 2>&1
echo "#        tree and of kernel configuration"                        >> ${LOG} 2>&1
echo "#"                                                                >> ${LOG} 2>&1
echo "# --------------------------------------------------------------" >> ${LOG} 2>&1
echo ""                                                                 >> ${LOG} 2>&1
cat ${LOG}

if [ $((help_only)) -eq 1 ]
then
	echo "${UsageString}"
	echo ""
	exit 1
fi

show_status_start
if [ $((do_not_wait)) -eq 0 ]
then
	cur=""
	read -t ${to} cur
	show_status_end
else
	skip_status
fi


show_status_start
cleanup_tmp_files
show_status_end

show_status_start
check_files
show_status_end

show_status_start
echo "# LOG ---- START SECTION read kernel version ------------------ " >> ${LOG} 2>&1
echo "#+ LOG INFO: /proc/version:  ${KERNEL_VERSION}" >> ${LOG} 2>&1
echo "#+ LOG INFO: /etc/*-release: ${SYSTEM_VERSION}" >> ${LOG} 2>&1
echo "#+ LOG INFO: Makefile/$(sed -ne "/^ *VERSION *=/p" ${KSRC}/Makefile)" >> ${LOG} 2>&1
echo "#+ LOG INFO: Makefile/$(sed -ne "/^ *PATCHLEVEL *=/p" ${KSRC}/Makefile)" >> ${LOG} 2>&1
echo "#+ LOG INFO: Makefile/$(sed -ne "/^ *SUBLEVEL *=/p" ${KSRC}/Makefile)" >> ${LOG} 2>&1
echo "#+ LOG INFO: Makefile/$(sed -ne "/^ *EXTRAVERSION *=/p" ${KSRC}/Makefile)" >> ${LOG} 2>&1
echo "# LOG ---- END SECTION read kernel version ------------------ " >> ${LOG} 2>&1
show_status_end

if [ $((kernel_major)) -le $((4)) ]
then
	show_status_start
	fix_kernel_headers
	show_status_end
else
	skip_status_silently
fi

show_status_start
update_kernel_source_tree
show_status_end

show_status_start
if [ $((kernel_major)) -le $((4)) ]
then
	update_kernel_configuration
else
	update_kernel_2_configuration
fi
show_status_end

#
# CAPI compile time options
#
optimize_capi=0
do_optimize_capi=0

if [ -z "${binary_build}" -a $((src_only)) -eq $((0)) ]
then #{
	if [ $((kernel_major)) -gt $((5)) -a $((kernel_minor)) -gt $((7)) ]
	then #{
		grep "define *.*DIVA_EICON_CAPI " ${KSRC}/drivers/isdn/hardware/eicon/platform.h > /dev/null 2>&1
		if [ $(($?)) -eq 0 ]
		then #{
			do_optimize_capi=1
			if [ -x "${INSTALL_DIR}/dialog" ]
			then #{
				${INSTALL_DIR}/dialog --clear --title "CAPI optimization" --backtitle "$backtitle" \
					--yesno "${optimize_capi_token}" 9 70 2>>${LOG}
				if [ $(($?)) -eq 0 ]
				then
					optimize_capi=1
				fi
			else # } {
				echo -e "${optimize_capi_token}(Y/N)[Y]: \c"
				optimize_capi_tmp=""
				read optimize_capi_tmp
				case ${optimize_capi_tmp} in
					'Y') optimize_capi=1
						;;
					'y') optimize_capi=1
						;;
				esac
			fi #}
		fi #}
	fi #}
fi #}

if [ $((do_optimize_capi)) -ne $((0)) ]
then #{

	if [ $((optimize_capi)) -ne $((0)) ]
	then #{
		echo "# LOG ---- START SECTION set DIVA_EICON_CAPI ON ------------------ " >> ${LOG} 2>&1

		sed -e "s/^.*define *DIVA_EICON_CAPI *.*$/#define DIVA_EICON_CAPI 1/" \
			${KSRC}/drivers/isdn/hardware/eicon/platform.h > divas.tmp 2>>${LOG}
		if [ $(($?)) -eq 0 ]
		then
			cp -f divas.tmp ${KSRC}/drivers/isdn/hardware/eicon/platform.h
			if [ $(($?)) -ne 0 ]
			then
				echo "#+ LOG WARNING: cp command failed" >> ${LOG} 2>&1
      	echo "WARNING: failed to turn CAPI optimization ON"
			fi
		else
			echo "#+ LOG WARNING: sed command failed" >> ${LOG} 2>&1
      echo "WARNING: failed to turn CAPI optimization ON"
		fi

		echo "# LOG ---- END SECTION set DIVA_EICON_CAPI ON ------------------ " >> ${LOG} 2>&1

	fi #}

fi #}

show_status_start
call_make_menuconfig
show_status_end

show_status_start
if [ $((no_depend)) -eq $((0)) ]
then
	call_make_dep
	show_status_end
else
	skip_status
fi

show_status_start
call_make_modules
show_status_end

show_status_start
if [ -f src/mtpx.tgz ]
then
	build_mtpx_adapter
	show_status_end
else
	skip_status
fi

show_status_start
if [ $((do_not_install)) -eq 0 ]
then
	install_diva_modules
	show_status_end
else
	skip_status
fi

show_status_start
cleanup_tmp_files
show_status_end

# ------------------------------------------------------------
#  Build Diva Server TTY driver
# ------------------------------------------------------------
if [ -s ./tty_compiler -a -x tty_compiler ]
then
	can_build_tty=1

	echo "# LOG ---- START SECTION tty_compiler ------------------ " >> ${LOG} 2>&1
	rm -rf tmp > /dev/null 2>>${LOG}
	mkdir tmp  > /dev/null 2>>${LOG}
	mount /dev/shm tmp -t tmpfs > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		echo "#++ LOG INFO: failed to mount tmpfs ------------------ " >> ${LOG} 2>&1
		can_build_tty=0
		echo ""
		echo "WARNING: ${ErrMsg[$err_msg_tmpfs]}"
		echo ""
	fi
	umount tmp > /dev/null 2>>${LOG}
	if [ $(($?)) -ne 0 ]
	then
		can_build_tty=0
		echo "#++ LOG INFO: failed to umount tmpfs ------------------ " >> ${LOG} 2>&1
		echo ""
		echo "WARNING: ${ErrMsg[$err_msg_tmpfs]}"
		echo ""
	fi
	rm -rf tmp > /dev/null 2>&1

	if [ $((can_build_tty)) -eq 1 ]
	then
		echo "#+++ LOG INFO: start TTY_COMPILER  ------------------ " >> ${LOG} 2>&1
		if [ $((kernel_major)) -le $((4)) ]
		then
			fix_tty_headers
		fi
		./tty_compiler
		err=$(($?))
		if [ $((err)) -eq 0 -a -s ./Divatty.${object_suffix} ]
		then
			cp -f Divatty.${object_suffix} ${INSTALL_DIR} > /dev/null 2>>${LOG}
			if [ $(($?)) -ne 0 ]
			then
				fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/Divatty.${object_suffix}"
			fi
			rm -f Divatty.${object_suffix} > /dev/null 2>&1
		else
			echo "#+++ LOG INFO: TTY_COMPILER failed ------------------ " >> ${LOG} 2>&1
		fi
		echo "#+++ LOG INFO: end   TTY_COMPILER  ------------------ " >> ${LOG} 2>&1
	fi

	echo "# LOG ---- END SECTION tty_compiler ------------------ " >> ${LOG} 2>&1
fi

cleanup_tmp_files
sync

# ------------------------------------------------------------
#  Check if divactrl is working. Compile divactrl if necessary
# ------------------------------------------------------------
echo "# LOG ---- START SECTION 'build divactrl' ------------------ " >> ${LOG} 2>&1

rm -rf tmp > /dev/null 2>&1
mkdir tmp > /dev/null 2>&1
if [ ! -d tmp ]
then
	echo "#+ LOG INFO: failed to create ${HERE}/tmp directory"            >>${LOG} 2>&1
	fatal_exit "${ErrMsg[$err_msg_std2kern]}" "tmp directory"
fi
mount /dev/shm tmp -t tmpfs > /dev/null 2>>${LOG}
cd tmp > /dev/null 2>>${LOG}
echo "#+ LOG INFO: tmp pwd:$(pwd)"            >>${LOG} 2>&1

${INSTALL_DIR}/divactrl load -h > tmp.txt 2>/dev/null
grep controller tmp.txt > /dev/null 2>&1
if [ $(($?)) -ne $((0)) ]
then
  show_status_start
  echo "#++ LOG INFO: build divactrl" >>${LOG} 2>&1
  tar -zxf ${HERE}/src/divactrl.tgz >>${LOG} 2>&1
  ./configure --with-firmware=/usr/lib/eicon/divas >>${LOG} 2>&1
  make >> ${LOG} 2>&1
  if [ $(($?)) -ne 0 ]
  then
    cd $HERE > /dev/null 2>&1
    echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
    cleanup_tmp_files
		fatal_exit "${ErrMsg[$err_msg_exec_cmd]}" "make divactrl"
  fi
  cp -f divactrl ${INSTALL_DIR}/ >> ${LOG} 2>&1
  if [ $(($?)) -ne 0 ]
  then
    cd $HERE > /dev/null 2>&1
    echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
    cleanup_tmp_files
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/divactrl"
  fi
  show_status_end
else
  skip_status_silently
fi
cd $HERE > /dev/null 2>&1
umount tmp >> ${LOG} 2>&1
rm -rf tmp
echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1

echo "# LOG ---- END SECTION 'build divactrl' ------------------ " >> ${LOG} 2>&1

# ------------------------------------------------------------
#  Check if acopy2 is working. Compile acopy2 if necessary
# ------------------------------------------------------------
echo "# LOG ---- START SECTION 'build capi tools' ------------------ " >> ${LOG} 2>&1

rm -rf tmp > /dev/null 2>&1
mkdir tmp > /dev/null 2>&1
if [ ! -d tmp ]
then
	echo "#+ LOG INFO: failed to create ${HERE}/tmp directory"            >>${LOG} 2>&1
	fatal_exit "${ErrMsg[$err_msg_std2kern]}" "tmp directory"
fi
mount /dev/shm tmp -t tmpfs > /dev/null 2>>${LOG}
cd tmp > /dev/null 2>>${LOG}
echo "#+ LOG INFO: tmp pwd:$(pwd)"            >>${LOG} 2>&1

echo "#+ LOG INFO: test acopy2"            >>${LOG} 2>&1
${INSTALL_DIR}/acopy2 -h /V0 > tmp.txt 2>/dev/null
grep ACOPY2 tmp.txt > /dev/null 2>&1
if [ $(($?)) -ne $((0)) ]
then
  show_status_start
  echo "#++ LOG INFO: build capi tools" >>${LOG} 2>&1
  tar -zxf ${HERE}/src/acopy2.tgz >>${LOG} 2>&1
	cd acopy2 >> ${LOG} 2>&1
  ./configure >>${LOG} 2>&1
  make >> ${LOG} 2>&1
  if [ $(($?)) -ne 0 ]
  then
    cd $HERE > /dev/null 2>&1
    echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
    cleanup_tmp_files
		fatal_exit "${ErrMsg[$err_msg_exec_cmd]}" "make acopy2"
  fi
  cp -f acopy2 ${INSTALL_DIR}/ >> ${LOG} 2>&1
  if [ $(($?)) -ne 0 ]
  then
    cd $HERE > /dev/null 2>&1
    echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1
    cleanup_tmp_files
		fatal_exit "${ErrMsg[$err_msg_cp_file]}" "${INSTALL_DIR}/acopy2"
  fi
  cp -f testfax ${INSTALL_DIR}/httpd/appl1.dat >> ${LOG} 2>&1
	if [ -x "${INSTALL_DIR}/testfax" ]
	then
  	cp -f testfax ${INSTALL_DIR}/ >> ${LOG} 2>&1
	fi
  show_status_end
else
  skip_status_silently
fi
cd $HERE > /dev/null 2>&1
umount tmp >> ${LOG} 2>&1
rm -rf tmp
echo "#+ LOG INFO: pwd:$(pwd)"          >>${LOG} 2>&1

echo "# LOG ---- END SECTION 'build capi tools' ------------------ " >> ${LOG} 2>&1

show_status_start
cleanup_tmp_files
sync
show_status_end

if [ $((build_rpm)) -eq $((0)) ]
then
  echo ""
  echo "SUCCESS. You can configure and start your Diva adapter now."
  echo ""
  exit 0
fi

rpm_kernel_name=""
grep UTS_RELEASE ${KSRC}/include/linux/version.h > /dev/null 2>&1
if [ $(($?)) -eq 0 ]
then
	rpm_kernel_name=$(grep UTS_RELEASE /usr/src/linux/include/linux/version.h | sed -e "s/\" *$//g;" -e "s/^.*\"//" -e "s/ *//g;" -e "s/\./_/g;" -e "s/-/_/g;" -)
fi
if [ -z "$rpm_kernel_name" ]
then
	rpm_kernel_name="unknown"
fi

hostname=$(hostname)

if [ ! -z "${binary_build}" ]
then
	build_major=$(echo $binary_build | sed -e "s/-.*$//" -)
	build_minor=$(echo $binary_build | sed -e "s/^.*-//" -)
	rpm_name="divas4linux_$rpm_kernel_name"
else
	build_major=$(echo $cur_build | sed -e "s/-.*$//" -)
	build_minor=$(echo $cur_build | sed -e "s/^.*-//" -)
	rpm_name="divas4linux_distributable_$rpm_kernel_name"
fi


echo "BUILD RPM $rpm_name now, please be patient"
rpm_base_dir="/usr/src/packages"
if [ ! -d /usr/src ]
then
	mkdir /usr/src > /dev/null 2>&1
fi
if [ ! -d $rpm_base_dir ]
then
	rpm_base_dir="/usr/src/redhat"
fi
if [ ! -d $rpm_base_dir ]
then
  rpm_base_dir="/usr/src/rpm"
  if [ ! -d $rpm_base_dir ]
  then
    fatal_exit "RPM base directory missing" "/usr/src/packages, /usr/src/redhat or /usr/src/rpm"
  fi
fi
if [ ! -d ${rpm_base_dir}/SOURCES ]
then
	mkdir ${rpm_base_dir}/SOURCES
fi
if [ ! -d ${rpm_base_dir}/BUILD ]
then
	mkdir ${rpm_base_dir}/BUILD
fi
if [ ! -d ${rpm_base_dir}/RPMS ]
then
	mkdir ${rpm_base_dir}/RPMS
fi
if [ ! -d ${rpm_base_dir}/RPMS/noarch ]
then
	mkdir ${rpm_base_dir}/RPMS/noarch
fi
if [ ! -d ${rpm_base_dir}/RPMS/i386 ]
then
	mkdir ${rpm_base_dir}/RPMS/i386
fi
if [ ! -d ${rpm_base_dir}/RPMS/${ARCHITECTURE} ]
then
	mkdir ${rpm_base_dir}/RPMS/${ARCHITECTURE}
fi
if [ ! -d ${rpm_base_dir}/SPECS ]
then
	mkdir ${rpm_base_dir}/SPECS
fi
if [ ! -d ${rpm_base_dir}/SRPMS ]
then
	mkdir ${rpm_base_dir}/SRPMS
fi


rpm_src_dir="${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}"
rm -rf $rpm_src_dir
rm -rf ${rpm_base_dir}/BUILD/${rpm_name}-${build_major}.${build_minor}
rm -rf ${rpm_base_dir}/SOURCES/divasb.tar
rm -rf /tmp/divas4linux*
rm -rf /var/tmp/divas4linux*
mkdir $rpm_src_dir > /dev/null 2>&1
if [ $(($?)) -ne $((0)) ]
then
	fatal_exit "${ErrMsg[$err_msg_exec_cmd]}" "mkdir $rpm_src_dir"
fi

#---------------------------------------------------
# Now WRITE RPM spec file
#---------------------------------------------------
rpm -q -l $(rpm -q -a | grep divas4linux) > $TMP 2>/dev/null
if [ $(($?)) -ne $((0)) ]
then
  fatal_exit "${ErrMsg[$err_msg_exec_cmd]}" 'rpm -q -l $(rpm -q -a | grep divas4linux)'
fi
find /usr/lib/eicon/divas -maxdepth 1 -name "*.${object_suffix}" -a -type f >> $TMP 2>/dev/null
sed -e "/\/src\//d;/\/doc\//d;/login$/d;/\/tools\//d;" $TMP > $filelist 2>/dev/null

echo 'Summary: Support for Eicon DIVA ISDN Adapters' > $TMP 2>/dev/null
echo "Name: $rpm_name" >> $TMP 2>/dev/null
echo "Version: ${build_major}.${build_minor}" >> $TMP 2>/dev/null
echo 'Release: 1' >> $TMP 2>/dev/null
echo 'Vendor: Eicon Networks' >> $TMP 2>/dev/null
echo 'Packager: Eicon Networks www.eicon.com' >> $TMP 2>/dev/null
echo 'Copyright: (C) Eicon Networks Corporation 1991-2005' >> $TMP 2>/dev/null
echo 'Source: divasb.tar' >> $TMP 2>/dev/null
echo 'BuildRoot: %{_tmppath}/%{name}-root' >> $TMP 2>/dev/null
echo 'Group: Networking' >> $TMP 2>/dev/null
echo '%description' >> $TMP 2>/dev/null
echo 'Drivers and utilities for PCI PRI/4BRI/BRI Eicon DIVA ISDN Adapters' >> $TMP 2>/dev/null
echo 'Build for (Kernel/Distribution): custom' >> $TMP 2>/dev/null
echo 'Provides CAPI2.0 interface, and TTY interface with AT command processing,' >> $TMP 2>/dev/null
echo 'ASYNC/SYNC PPP conversion and automatic B-Channel protocol detection' >> $TMP 2>/dev/null
echo 'Enables usage of V.90+V.42bis Central Office Modem, Fax+Voice+DTMF and' >> $TMP 2>/dev/null
echo 'miscellaneous wireless and digital protocols' >> $TMP 2>/dev/null
echo "" >> $TMP 2>/dev/null
echo '%prep' >> $TMP 2>/dev/null
echo "" >> $TMP 2>/dev/null
echo '%setup' >> $TMP 2>/dev/null
echo "" >> $TMP 2>/dev/null
echo '%build' >> $TMP 2>/dev/null
echo "" >> $TMP 2>/dev/null
echo '%pre' >> $TMP 2>/dev/null
echo 'if [ -d /usr/lib/eicon/divas ]' >> $TMP 2>/dev/null
echo 'then' >> $TMP 2>/dev/null
echo 'echo -en "DIVAS4LINUX INSTALLATION "' >> $TMP 2>/dev/null
echo 'echo -en "\\033[1;31m"' >> $TMP 2>/dev/null
echo 'echo -en "FAILED"' >> $TMP 2>/dev/null
echo 'echo -en "\\033[0;39m"' >> $TMP 2>/dev/null
echo 'echo " (previous installation found)"' >> $TMP 2>/dev/null
echo 'echo -en "\\033[1;33m"' >> $TMP 2>/dev/null
echo 'echo -en "Please remove previous installation first."' >> $TMP 2>/dev/null
echo 'echo -en "\\033[0;39m"' >> $TMP 2>/dev/null
echo 'echo ""' >> $TMP 2>/dev/null
echo 'exit 1' >> $TMP 2>/dev/null
echo 'fi' >> $TMP 2>/dev/null
echo 'mkdir -p /etc/saved.by.diva' >> $TMP
echo 'if [ -x /etc/init.d/isdn ]'                    >> $TMP
echo 'then'                                          >> $TMP
echo '  /etc/init.d/isdn stop > /dev/null 2>&1'      >> $TMP
echo '	cp -uf /etc/init.d/isdn /etc/saved.by.diva/etc.init.d.isdn' >> $TMP
echo "  echo '#! /bin/sh'  > /etc/init.d/isdn"       >> $TMP
echo "  echo '### BEGIN INIT INFO'                   >> /etc/init.d/isdn" >> $TMP
echo "  echo '# Provides:          isdn'             >> /etc/init.d/isdn" >> $TMP
echo "  echo '# Default-Start:     4'                >> /etc/init.d/isdn" >> $TMP
echo "  echo '# Short-Description: I4L inactive'     >> /etc/init.d/isdn" >> $TMP
echo "  echo '# Description:       inactive script'  >> /etc/init.d/isdn" >> $TMP
echo "  echo '#       moved to /etc/saved.by.diva/'  >> /etc/init.d/isdn" >> $TMP
echo "  echo '#       during install of Eicon Diva Server'  >> /etc/init.d/isdn" >> $TMP
echo "  echo '### END INIT INFO'  >> /etc/init.d/isdn"      >> $TMP
echo "  echo ''  >> /etc/init.d/isdn"                >> $TMP
echo "  echo exit 0 >> /etc/init.d/isdn"             >> $TMP
echo '	touch -c /etc/saved.by.diva/etc.init.d.isdn' >> $TMP
echo 'fi'                                            >> $TMP
echo 'if [ -f /etc/sysconfig/isdncard ]' >> $TMP
echo 'then' >> $TMP
echo '	cp -f /etc/sysconfig/isdncard /etc/saved.by.diva/etc.sysconfig.isdncard' >> $TMP
echo '	rm -f /etc/sysconfig/isdncard' >> $TMP
echo 'fi' >> $TMP
echo 'if [ -d /etc/hotplug ]'                        >> $TMP
echo 'then'                                          >> $TMP
echo '  grep -s diva /etc/hotplug/blacklist > /dev/null 2>&1' >> $TMP
echo '  if [ $(($?)) -ne 0 ]'                        >> $TMP
echo '  then'                                        >> $TMP
echo '    echo divas >> /etc/hotplug/blacklist'    >> $TMP
echo '    echo ""    >> /etc/hotplug/blacklist'    >> $TMP
echo '  fi'                                          >> $TMP
echo 'fi'                                            >> $TMP
echo 'lsmod | grep -s diva > /dev/null 2>&1'         >> $TMP
echo 'if [ $(($?)) -eq 0 ]'                          >> $TMP
echo 'then'                                          >> $TMP
echo '  rmmod divas > /dev/null 2>&1'                >> $TMP
echo '  rmmod divadidd > /dev/null 2>&1'             >> $TMP
echo 'fi'                                            >> $TMP
echo 'lsmod | grep -s diva > /dev/null 2>&1'         >> $TMP
echo 'if [ $(($?)) -eq 0 ]'                          >> $TMP
echo 'then'                                          >> $TMP
echo 'echo -en "DIVAS4LINUX INSTALLATION "'          >> $TMP
echo 'echo -en "\\033[1;31m"'                        >> $TMP
echo 'echo -en "FAILED"'                             >> $TMP
echo 'echo -en "\\033[0;39m"'                        >> $TMP
echo 'echo " (Failed to unload Diva drivers)"'       >> $TMP
echo 'echo -en "\\033[1;33m"'                        >> $TMP
echo 'echo -en "Please restart your system."'        >> $TMP
echo 'echo -en "\\033[0;39m"'                        >> $TMP
echo 'echo ""'                                       >> $TMP
echo 'exit 1'                                        >> $TMP
echo 'fi'                                            >> $TMP
echo "" >> $TMP 2>/dev/null
echo '%post' >> $TMP 2>/dev/null
echo 'chmod 600 /usr/lib/eicon/divas/httpd/login/login' >> $TMP 2>/dev/null
echo 'sh /usr/lib/eicon/divas/cfg_util.sh 3' >> $TMP 2>/dev/null
echo 'sh /usr/lib/eicon/divas/cfg_util.sh 7' >> $TMP 2>/dev/null
echo 'sh /usr/lib/eicon/divas/cfg_util.sh 8' >> $TMP 2>/dev/null
echo "" >> $TMP 2>/dev/null
echo '%preun' >> $TMP 2>/dev/null
echo 'if [ -f /usr/lib/eicon/divas/divas_stop.rc ]' >> $TMP 2>/dev/null
echo 'then' >> $TMP 2>/dev/null
echo '/usr/lib/eicon/divas/divas_stop.rc' >> $TMP 2>/dev/null
echo 'if [ $(($?)) -ne 0 ]' >> $TMP 2>/dev/null
echo 'then' >> $TMP 2>/dev/null
echo 'exit 1' >> $TMP 2>/dev/null
echo 'fi' >> $TMP 2>/dev/null
echo 'fi' >> $TMP 2>/dev/null
echo 'if [ -f /usr/lib/eicon/divas/cfg_util.sh ]' >> $TMP 2>/dev/null
echo 'then' >> $TMP 2>/dev/null
echo 'sh /usr/lib/eicon/divas/cfg_util.sh 4' >> $TMP 2>/dev/null
echo 'rm -f /usr/lib/eicon/divas/httpd/login/*.*' >> $TMP 2>/dev/null
echo 'sh /usr/lib/eicon/divas/cfg_util.sh 2' >> $TMP 2>/dev/null
echo 'sh /usr/lib/eicon/divas/cfg_util.sh 9' >> $TMP 2>/dev/null
echo 'fi' >> $TMP 2>/dev/null
echo "" >> $TMP 2>/dev/null
echo '%postun' >> $TMP 2>/dev/null
echo 'rm -rf /usr/lib/eicon/divas' >> $TMP 2>/dev/null
echo 'rm -f /dev/ttyds*' >> $TMP 2>/dev/null
echo "" >> $TMP 2>/dev/null
echo '%install' >> $TMP 2>/dev/null
echo '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT' >> $TMP 2>/dev/null
echo 'install -d -m 744 -o 0 -g 0 $RPM_BUILD_ROOT/usr/lib/eicon/divas' >> $TMP 2>/dev/null
echo 'install -d -m 700 -o 0 -g 0 $RPM_BUILD_ROOT/usr/lib/eicon/divas/mib' >> $TMP 2>/dev/null
echo 'install -d -m 700 -o 0 -g 0 $RPM_BUILD_ROOT/usr/lib/eicon/divas/httpd' >> $TMP 2>/dev/null
echo 'install -d -m 700 -o 0 -g 0 $RPM_BUILD_ROOT/usr/lib/eicon/divas/httpd/login' >> $TMP 2>/dev/null
echo 'install -d -m 700 -o 0 -g 0 $RPM_BUILD_ROOT/usr/lib/eicon/divas/httpd/images' >> $TMP 2>/dev/null
echo 'install -m 600 -o 0 -g 0 login $RPM_BUILD_ROOT/usr/lib/eicon/divas/httpd/login' >> $TMP 2>/dev/null

for nr in $(cat $filelist)
do
	ls -CF $nr | grep "\*" > /dev/null 2>&1
	if [ $(($?)) -eq 0 ]
	then
		mode="744"
	else
    mode="644"
	fi

	nr=$(echo $nr | sed -e "s/\/usr\/lib\/eicon\/divas\///" -)
	echo $nr | grep "/" > /dev/null 2>&1
	if [ $(($?)) -eq 0 ]
	then
		name=$nr
		nr=$(echo $nr | sed -e "s/^.*\///g;" -)
		dir=$(echo $name | sed -e "s/\/$nr$//" -)
		echo "install -m ${mode} -o 0 -g 0 ${nr} "'$RPM_BUILD_ROOT'"/usr/lib/eicon/divas/${dir}" >> $TMP 2>/dev/null
  else
		echo "install -m ${mode} -o 0 -g 0 ${nr} "'$RPM_BUILD_ROOT'"/usr/lib/eicon/divas" >> $TMP 2>/dev/null
	fi
	
done

echo "">> $TMP
echo '%clean' >> $TMP 2>/dev/null
echo '[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf $RPM_BUILD_ROOT' >> $TMP 2>/dev/null

echo "">> $TMP
echo '%files' >> $TMP
echo "">> $TMP
echo '/usr/lib/eicon/divas/httpd/login/login' >> $TMP 2>/dev/null
for nr in $(cat $filelist)
do
	echo $nr >> $TMP 2>/dev/null
done

# ---------------------------------------------------
#  Create default login file
# ---------------------------------------------------
echo XX > ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "Please replace 'XX' with your Diva Web interface access" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "password." >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "+ The password length should be not less as 7 characters" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "+ The owner of this file should be 'root'" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "+ Permissions of this file should be 600 or 400." >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "If password is too short or file has wrong owner/permission" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "then this file will be ignored and access to the Web" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "configuration interface will be not possible." >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login
echo "" >> ${rpm_base_dir}/SOURCES/${rpm_name}-${build_major}.${build_minor}/login

for nr in $(cat $filelist)
do
	dst=
	cp -f $nr ${rpm_src_dir}
done

tar -cf ${rpm_base_dir}/SOURCES/divasb.tar -C ${rpm_base_dir}/SOURCES ${rpm_name}-${build_major}.${build_minor}
rm -rf ${rpm_src_dir}
cp -f $TMP $rpm_name

echo "#+ LOG START PROCESS STEP: call rpmbuild" >> ${LOG} 2>&1

rpm_build_cmd="rpmbuild -bb --clean --target ${ARCHITECTURE} --rmsource $rpm_name"

${rpm_build_cmd} >$TMP  2>>${LOG}
if [ $(($?)) -ne $((0)) ]
then
	rm -rf ${rpm_base_dir}/SOURCES/divasb.tar
	rm -f filelist
	rm -f $rpm_name
	rm -f $TMP
	fatal_exit "${ErrMsg[$err_msg_exec_cmd]}" "${rpm_build_cmd}"
fi
rm -rf ${rpm_base_dir}/SOURCES/divasb.tar
rm -f $rpm_name
rm -f filelist

echo "#+ LOG END PROCESS STEP: call rpmbuild" >> ${LOG} 2>&1

echo ""
grep "divas4linux.*\.rpm$" $TMP > /dev/null 2>&1
if [ $(($?)) -eq $((0)) ]
then
	echo "RPM build complete. $(grep "divas4linux.*\.rpm$" $TMP 2>/dev/null)"
else
	echo "RPM build complete"
fi
echo ""

echo "#+ RPM build complete " >> ${LOG} 2>&1
if [ ! -z "${binary_build}" ]
then
	binary_rpm_name=$(sed -e "/debuginfo/d;" $TMP 2>/dev/null | grep "divas4linux.*\.rpm$" - 2>/dev/null | sed -e "s/^.* \//\//" -)
	echo "#+ RPM name: ${binary_rpm_name}" >> ${LOG} 2>&1
	cp "${binary_rpm_name}" / >> ${LOG} 2>&1
  if [ $(($?)) -ne 0 ]
	then
		echo "#+++ command 'cp ${binary_rpm_name} / failed'" >> ${LOG} 2>&1
		exit 1
	fi
  cat divas.log
fi

rm -f $TMP
cleanup_tmp_files

exit 0

