#!/bin/bash
# uses kernel-mini.config to create a kernel .config file

. run_profile.txt
. $base_dir/plans/commands/common.fns

[ -f $base_dir/etc/global.cfg ] && source $base_dir/etc/global.cfg
if [ "$SRC_DIR" = "" ]; then
	SRC_DIR="/usr/local/src"
fi

kernel_info
src_dir="$SRC_DIR/linux-$kernel_id"
cfg_dest="$all_pkgs_dir/$pkg_name"

if [ ! -d $src_dir ]; then
    echo "ERROR:  missing src_dir: $src_dir"
    exit -11
fi

SUT=`hostname -s`
log_dir=$run_dir/logs/$SUT
[ -d $log_dir ] || mkdir -p $log_dir

sut_type=`uname -m | sed -e s/i.86/i386/ -e s/sun4u/sparc/ -e s/arm.*/arm/ \
     -e s/s390.*/s390/ -e s/sa110/arm/ -e s/ppc64/powerpc/ -e s/parisc64/parisc/`

if [ -e "$all_suts_dir/$SUT/profile.txt" ]; then
    sut_type=`grep ^sut_type= $all_suts_dir/$SUT/profile.txt | cut -d '=' -f 2`
fi

# Look for a kernel-mini.config for this specific package by machine name
if [ -e "$all_pkgs_dir/$pkg_name/kernel-mini.config-$SUT" ]; then
    default_config="$all_pkgs_dir/$pkg_name/kernel-mini.config-$SUT"

# Look for a .config for this specific package by machine type
elif [ -e "$all_pkgs_dir/$pkg_name/kernel-mini.config-$sut_type" ]; then
    default_config="$all_pkgs_dir/$pkg_name/kernel-mini.config-$sut_type"

# Look for a .config for this specific package in general
elif [ -e "$all_pkgs_dir/$pkg_name/kernel-mini.config" ]; then
    default_config="$all_pkgs_dir/$pkg_name/kernel-mini.config"

# Look for a .config for this machine name
elif [ -e "$all_suts_dir/$SUT/kernel-mini.config" ]; then
    default_config="$all_suts_dir/$SUT/kernel-mini.config"

# Look for a .config for this machine type
elif [ -e "$all_suts_dir/kernel-mini.config-$sut_type" ]; then
    default_config="$all_suts_dir/kernel-mini.config-$sut_type"
else
    echo "No kernel-mini.config file found for SUT:$SUT, type:$sut_type"
    exit -13
fi

pwd=`/bin/pwd`
cd $src_dir

KCONFIG_ALLCONFIG=$default_config make allnoconfig >$log_dir/kernel.miniconfig.log 2>&1
[ ! -e $src_dir/.config ] && echo "ERROR: no .config created" && exit 1

kexec_arg=""
[ $kexec_reboot = "1" ] && kexec_arg="-x"
[ $kernel_id ] && kernel_id="-i $kernel_id"
[ $kernel_label ] && kernel_label="-k $kernel_label"
[ $default_config ] && default_config="-c $default_config"
[ "$kernel_args" ] && kernel_args="-a $kernel_args"
[ $log_dir ] && log_dir="-l $log_dir"

build_kernel $kernel_id $kernel_label $default_config $log_dir $kexec_arg "$kernel_args"
