#!/bin/bash

# Ndiswrapper module build script. 
# Run this in the same directory as the build script and needed files.
# Also needs linux-4.8.17-patched.txz available on the Tinycore site.
TCUSER=`cat /etc/sysconfig/tcuser`
HERE=`pwd`
VERSION=1.61


export CFLAGS="-march=i486 -mtune=i686 -O2 -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -O2 -pipe"

[ -d linux-4.8.17 ] && rm -r linux-4.8.17

if [ -f linux-4.8.17-patched.tar.xz ]; then
	[ -d linux-4.8.17 ] && rm -r linux-4.8.17
	tar xf linux-4.8.17-patched.tar.xz
else
	echo "linux-4.8.17-patched.tar.xz is not found.  Exiting.."
	exit 1
fi
[ -e /lib/modules/4.8.17-tinycore/build ] && rm /lib/modules/4.8.17-tinycore/build
[ -e /lib/modules/4.8.17-tinycore/source ] && rm /lib/modules/4.8.17-tinycore/source
ln -sf "$HERE"/linux-4.8.17 /lib/modules/4.8.17-tinycore/build 
ln -sf "$HERE"/linux-4.8.17 /lib/modules/4.8.17-tinycore/source 
if [ -f config-4.8.17-tinycore-devtmpfs ]; then
	cp config-4.8.17-tinycore-devtmpfs "$HERE"/linux-4.8.17/.config
else
	echo "config-4.8.17-tinycore-devtmpfs not found.  Exiting.."
	exit 1
fi 
cd /lib/modules/4.8.17-tinycore/build
make oldconfig
make prepare
make modules_prepare
if [ -f "$HERE"/ndiswrapper-"$VERSION".tar.gz ]; then
	[ -d $HERE"/ndiswrapper-"$VERSION" ] && rm -r $HERE"/ndiswrapper-"$VERSION"
	tar xvf "$HERE"/ndiswrapper-"$VERSION".tar.gz -C "$HERE"
	cd "$HERE"/ndiswrapper-"$VERSION"
else
	echo "ndiswrapper-"$VERSION".tar.gz not available, Exiting.."
	exit 1
fi

make
make install



