#!/usr/bin/bash
CFG=legacy
CONFIG=./mkuird.cfg
EXCLUDE=qqqqqqqqqqq
BUILD=uird
KERNEL=$(uname -r)
date > ./notfound.log

for arg in $@
do
  case "${arg}" in
    "-c" | "--config" ) config=yes;;
    "-e" | "--exclude" ) exclude=yes;;
    "-n" | "--name" ) name=yes;;
    "-k" | "--kernel" ) kernel=yes;;
    "-m" | "kmodpath" ) kmodpath=yes;;
    "--kmod" ) BUILD=kmod;;
    "--addon" ) BUILD=addon;;
    "-"*[A-Za-z]*) echo "$(basename "$0"): invalid option -- '$(echo ${arg} | tr -d '-')'" >&2; exit 1;;
    *) if [ "${config}" == "yes" ]; then CONFIG="${arg}"
		elif [ "${exclude}" == "yes" ]; then EXCLUDE="${arg}"
		elif [ "${name}" == "yes" ]; then NAME="${arg}"
		elif [ "${kernel}" == "yes" ]; then KERNEL="${arg}"
		elif [ "${kmodpath}" == "yes" ]; then KMODPATH="${arg}"
		else CFG="${arg}" ; fi
        config="no"; exclude="no"; name="no" ; kernel="no" ; kmodpath="no" ;;
  esac
done

[ -d  $(realpath ${KMODPATH}/$KERNEL 2>/dev/null) ] && ln -s $(realpath ${KMODPATH}/$KERNEL) /lib/modules/$KERNEL
if ! [ -d /lib/modules/$KERNEL -o -L /lib/modules/$KERNEL  ] ; then
	echo "Kernel modules for $KERNEL  not found"
	exit 1
fi

if ! depmod -A "$KERNEL" ; then
	echo  "depmod error..."
	exit 2
fi


. $CONFIG
EXCLUDE=$(echo "$EXCLUDE" |sed "s/,/|/g")

if ! [ -f ./configs/uird_configs/$CFG ]; then
	echo "./configs/uird_configs/$CFG is not exist"
	exit 3
fi

cd dracut/modules.d
ln -s ../../modules.d/* ../modules.d/ 2>/dev/null
cd ../..

notfound() {
echo "Not found: $1 - $2"
echo "Not found: $1 - $2" >> ./notfound.log	
}

testMOd () {
mod=$2 ; type=$1 
if [ "$type" == "KM" -o "$type" == "FS" ] ;then
		if echo $mod | egrep -q "^=" ; then 
			return 0 
		elif /sbin/modinfo $mod -k $KERNEL >/dev/null 2>&1 ; then
			return 0
		elif for  a in $BUILTIN;do echo $a  ; done |grep -q $mod ; then
			echo "Built in kernel: $type - $mod"
			return 1
		fi
elif [ "$type" == "BIN" ] ;then
		which $mod >/dev/null 2>&1 && 	return 0
elif [ "$type" == "DM" ] ;then
		ls -1 ./dracut/modules.d |egrep -q "..${mod}$" && return 0
fi
notfound $type  $mod 
return 1
}

parser () {
PREFIX=$1
for item in $(cat $CONFIG |grep ^$1 |awk -F= '{print $1}') ; do
if echo "$item" | egrep -q "$EXCLUDE" ; then 
	echo "excluded all: $item"
	continue
fi
for   mod in ${!item} ; do
	if echo "$mod" | egrep -q  "$EXCLUDE" ; then 
		echo "excluded: ${PREFIX} - $mod"
		continue
	fi
	if testMOd $PREFIX $mod ; then 
			eval $PREFIX="\" \${$PREFIX} $mod \"" 
	fi
done
done
}
BUILTIN=$(cat /lib/modules/$KERNEL/modules.builtin |while read a ; do basename $a ; done |sed 's/.ko$//')


if [ "$BUILD" == "kmod" ] ; then
	parser KM ; parser FS ; parser DM
	echo "MAKE UIRD (Kernel modules only)...."
	uirdfile=uird.kmod.cpio.xz
	./dracut/dracut.sh -l -N -f -m "$DM_base" \
	-d "$KM" \
	--filesystems "$FS" \
	--kernel-only \
	-c dracut.conf -v -M $uirdfile $KERNEL >dracut_kmod.log 2>&1
elif [ "$BUILD" == "addon" ] ; then
	echo "MAKE UIRD addon.... "
	uirdfile=uird.addon.cpio.xz
	./dracut/dracut.sh -l -N -f -m "$ExtraDM" \
	--no-kernel \
	-c dracut.conf -v -M $uirdfile $KERNEL >dracut_addon.log 2>&1
else
	parser KM ; parser FS ; parser DM ; parser BIN
	UIRD_BINS="$BIN"
	export UIRD_BINS
	echo "MAKE UIRD ${CFG}...."
	uirdfile="uird.$CFG.cpio.xz"
	./dracut/dracut.sh -l -N --strip -f -m "$DM" \
	-d "$KM" \
	--filesystems "$FS" \
	-i initrd /  \
	-i configs /  \
	--kernel-cmdline "uird.basecfg=/uird_configs/$CFG" \
	-c dracut.conf -v -M $uirdfile $KERNEL  >dracut_magos.log 2>&1
fi
[ "$KMODPATH" -a -L "/lib/modules/$KERNEL"  ]  && rm /lib/modules/$KERNEL
[ "$NAME" ] && mv "$uirdfile" "$NAME"
