#! /bin/sh

LINUX_HEADERS=/home/sven/projekte/build_test/linux-build
LINUX_DEFAULT_VERSION=linux-4.5
IWYU_KERNEL_MAPPINGS=/home/sven/projekte/build_test/testhelpers/kernel_mappings.iwyu

spath="./net/batman-adv/"
bpath="./net/batman-adv/"

MAKE_CONFIG="CONFIG_BATMAN_ADV_DEBUG=y CONFIG_BATMAN_ADV_BLA=y CONFIG_BATMAN_ADV_DAT=y CONFIG_BATMAN_ADV_MCAST=y CONFIG_BATMAN_ADV_NC=y CONFIG_BATMAN_ADV_BATMAN_V=y KBUILD_SRC=${LINUX_HEADERS}/${LINUX_DEFAULT_VERSION}"

# don't touch main.h, bat_algo.h and files which are required by linux/wait.h, packet.h
sed -i 's/#include "main.h"/#include "main.h" \/\/ IWYU pragma: keep/' "${spath}"/*c "${spath}"/*.h
sed -i 's/#include "bat_algo.h"/#include "bat_algo.h" \/\/ IWYU pragma: keep/' "${spath}"/*c "${spath}"/*.h
sed -i 's/\/\* for linux\/wait.h \*\//\/\* for linux\/wait.h \*\/ \/\/ IWYU pragma: keep/' "${spath}"/*c "${spath}"/*.h
sed -i 's/\/\* for packet.h \*\//\/\* for packet.h \*\/ \/\/ IWYU pragma: keep/' "${spath}"/*c "${spath}"/*.h

make KERNELPATH="${LINUX_HEADERS}/${LINUX_DEFAULT_VERSION}" $MAKE_CONFIG || exit

make KERNELPATH="${LINUX_HEADERS}/${LINUX_DEFAULT_VERSION}" $MAKE_CONFIG clean
make KERNELPATH="${LINUX_HEADERS}/${LINUX_DEFAULT_VERSION}" -j1 -k CC="iwyu -Xiwyu --prefix_header_includes=keep -Xiwyu --no_default_mappings -Xiwyu --transitive_includes_only -Xiwyu --verbose=1 -Xiwyu --mapping_file=$IWYU_KERNEL_MAPPINGS" $MAKE_CONFIG 2> test


git add -f "${bpath}" "${spath}"
fix_include --nosafe_headers --noblank_lines --separate_project_includes="$(pwd)/${bpath}" < test

# remove extra noise
sed -i '/struct batadv_algo_ops;/d' "${bpath}"/main.h
sed -i '/struct batadv_hard_iface;/d' "${bpath}"/main.h
sed -i '/struct batadv_orig_node;/d' "${bpath}"/main.h
sed -i '/struct batadv_priv;/d' "${bpath}"/main.h
git diff > asd
git checkout -f
less asd
