kmajorminor=`echo $kernelver | cut -d . --complement -f 3-`
CONFIGURE_ARGS="--enable-sii-assign --disable-rt-syslog --disable-tool --disable-userlib --enable-generic --enable-ccat --disable-tty"
CONFIGURE_ARGS+=" --with-linux-dir=$kernel_source_dir"

PACKAGE_NAME="ethercat-dkms"
PACKAGE_VERSION="#MODULE_VERSION#"
AUTOINSTALL="YES"
BUILT_MODULE_NAME[0]="ec_master"
BUILT_MODULE_LOCATION[0]="master/"
DEST_MODULE_LOCATION[0]="/kernel/extra/ethercat"
BUILT_MODULE_NAME[1]="ec_generic"
BUILT_MODULE_LOCATION[1]="devices/"
DEST_MODULE_LOCATION[1]="/kernel/extra/ethercat"
BUILT_MODULE_NAME[2]="ec_ccat"
BUILT_MODULE_LOCATION[2]="devices/ccat/"
DEST_MODULE_LOCATION[2]="/kernel/extra/ethercat"
POST_BUILD="save_module_symvers $dkms_tree"

for km in e1000 igb igbvf ixgbevf igc; do
    if [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/$km/$km-${kmajorminor}-ethercat.h ] && grep -i -q "^CONFIG_${km}=m$" $kernel_source_dir/.config; then
        idx=${#BUILT_MODULE_NAME[@]}
        BUILT_MODULE_NAME[$idx]="ec_$km"
        BUILT_MODULE_LOCATION[$idx]="devices/$km/"
        DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
        CONFIGURE_ARGS+=" --enable-$km"
    else
        CONFIGURE_ARGS+=" --disable-$km"
    fi
done

if [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/e1000e/e1000-${kmajorminor}-ethercat.h ] && grep -i -q "^CONFIG_E1000E=[my]$" $kernel_source_dir/.config; then
    idx=${#BUILT_MODULE_NAME[@]}
    BUILT_MODULE_NAME[$idx]="ec_e1000e"
    BUILT_MODULE_LOCATION[$idx]="devices/e1000e/"
    DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
    CONFIGURE_ARGS+=" --enable-e1000e"
else
    CONFIGURE_ARGS+=" --disable-e1000e"
fi

for km in 8139too e100; do
    if [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/$km-$kmajorminor-ethercat.c ] && grep -i -q "^CONFIG_${km}=m$" $kernel_source_dir/.config; then
        idx=${#BUILT_MODULE_NAME[@]}
        BUILT_MODULE_NAME[$idx]="ec_$km"
        BUILT_MODULE_LOCATION[$idx]="devices/"
        DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
        CONFIGURE_ARGS+=" --enable-$km"
    else
        CONFIGURE_ARGS+=" --disable-$km"
    fi
done

if [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/r8169-$kmajorminor-ethercat.c ] && grep -q "^CONFIG_R8169=m$" $kernel_source_dir/.config; then
    idx=${#BUILT_MODULE_NAME[@]}
    BUILT_MODULE_NAME[$idx]="ec_r8169"
    BUILT_MODULE_LOCATION[$idx]="devices/"
    DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
    CONFIGURE_ARGS+=" --enable-r8169"
elif [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/r8169/r8169-${kmajorminor}-ethercat.h ] && grep -q "^CONFIG_R8169=m$" $kernel_source_dir/.config; then
    idx=${#BUILT_MODULE_NAME[@]}
    BUILT_MODULE_NAME[$idx]="ec_r8169"
    BUILT_MODULE_LOCATION[$idx]="devices/r8169/"
    DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
    CONFIGURE_ARGS+=" --enable-r8169"
else
    CONFIGURE_ARGS+=" --disable-r8169"
fi

if [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/stmmac/stmmac_pci-$kmajorminor-ethercat.c ] && grep -q "^CONFIG_STMMAC_PCI=[my]$" $kernel_source_dir/.config; then
    idx=${#BUILT_MODULE_NAME[@]}
    BUILT_MODULE_NAME[$idx]="ec_stmmac-pci"
    BUILT_MODULE_LOCATION[$idx]="devices/stmmac/"
    DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
    CONFIGURE_ARGS+=" --enable-stmmac-pci"
else
    CONFIGURE_ARGS+=" --disable-stmmac-pci"
fi

if [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/stmmac/dwmac-intel-$kmajorminor-ethercat.c ] && grep -q "^CONFIG_DWMAC_INTEL=[my]$" $kernel_source_dir/.config; then
    idx=${#BUILT_MODULE_NAME[@]}
    BUILT_MODULE_NAME[$idx]="ec_dwmac-intel"
    BUILT_MODULE_LOCATION[$idx]="devices/stmmac/"
    DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
    CONFIGURE_ARGS+=" --enable-dwmac-intel"
else
    CONFIGURE_ARGS+=" --disable-dwmac-intel"
fi

if [ -r $source_tree/ethercat-dkms-#MODULE_VERSION#/devices/genet/bcmgenet-$kmajorminor-ethercat.h ] && grep -q "^CONFIG_BCMGENET=m$" $kernel_source_dir/.config; then
    idx=${#BUILT_MODULE_NAME[@]}
    BUILT_MODULE_NAME[$idx]="ec_genet"
    BUILT_MODULE_LOCATION[$idx]="devices/genet/"
    DEST_MODULE_LOCATION[$idx]="/kernel/extra/ethercat"
    CONFIGURE_ARGS+=" --enable-genet"
else
    CONFIGURE_ARGS+=" --disable-genet"
fi

MAKE[0]="./configure ${CONFIGURE_ARGS} && make modules"
