1#!/bin/bash
2#******************************************************************************
3#
4# Bash-Script to create Plustek-Scannerdriver modules for Kernel 2.4, 2.6 and 3.x
5# out of the backend sources...
6#
7
8BUILD_DIR=$PWD/build
9SRC_DIR=$PWD/../../backend
10MAKEFILE=$PWD/Makefile.kernel26
11KERNEL_V=`uname -r`
12OSMINOR=`uname -r | cut -b 3`
13OSMAJOR=`uname -r | cut -b 1`
14
15#
16# some intro ;-)
17#
18echo "This script will try and build a suitable kernel-module for your system."
19echo "If you'd like to make the module WITH debug output, restart this script"
20echo "with as follows:"
21echo "./MakeModule.sh DEBUG=y"
22echo "Press <ENTER> to continue or <CTRL><C> to cancel."
23read
24
25#
26# we need to be root user...
27#
28echo -n "Check for root..."
29if [ $EUID -ne 0 ]; then
30	echo -e "\b\b\b - failed"
31	echo "Please retry as root user."
32	exit -1
33fi
34echo -e "\b\b\b - done."
35
36#
37# Version checks...
38#
39echo -e "\nCheck for kernelversion:"
40if [ "$OSMAJOR" == "3" ];then
41	echo "Using makefile for kernel 2.6.x - okay for kernel 3 as well..."
42	MAKEFILE=$PWD/Makefile.kernel26
43elif [ "$OSMINOR" == "6" ]; then
44	echo "Using makefile for kernel 2.6.x"
45	MAKEFILE=$PWD/Makefile.kernel26
46elif [ "$OSMINOR" == "4" ]; then
47	echo "Using makefile for kernel 2.4.x"
48	MAKEFILE=$PWD/Makefile.kernel24
49else
50	echo "Your kernelversion >"$OSMAJOR"."$OSMINOR"< is probably not supported"
51	exit -2
52fi
53
54#
55# Setup...
56#
57echo -e "Build-directory: \n"$BUILD_DIR
58echo -n "Removing build-directory..."
59rm -rf $BUILD_DIR
60echo -e "\b\b\b - done."
61
62echo -n "Creating build-directory..."
63mkdir $BUILD_DIR
64cd $BUILD_DIR
65echo -e "\b\b\b - done.\n"
66
67echo -n "Linking source files..."
68C_FILES=`ls $SRC_DIR/plustek-pp_*.c`
69H_FILES=`ls $SRC_DIR/plustek-pp_*.h`
70
71for F in $C_FILES $H_FILES $SRC_DIR/plustek-pp.h $SRC_DIR/plustek_pp.c; do
72	ln -s $F .
73done
74echo -e "\b\b\b - done."
75
76echo -n "Copying Makefile to build-directory..."
77cp $MAKEFILE Makefile
78echo -e "\b\b\b - done."
79
80#
81# Building the module...
82#
83echo "Making the module..."
84if [ "$OSMAJOR" == "2" -a "$OSMINOR" == "4" ]; then
85	make all $1
86else
87	make -C /lib/modules/$KERNEL_V/build/ SUBDIRS=$BUILD_DIR modules $1
88fi
89RES=$?
90cd ..
91if [ $RES != 0 ]; then
92	echo "There were some build errors..."
93	exit -1
94fi
95echo "done."
96
97echo "Should I install the module?"
98echo "Press <ENTER> to continue or <CTRL><C> to cancel."
99read
100
101make -C $BUILD_DIR install
102
103echo "Should I try and load the module?"
104echo "If this step fails, check the kernel-log."
105echo "Press <ENTER> to continue or <CTRL><C> to cancel."
106read
107
108make -C $BUILD_DIR load
109echo "done."
110
111echo "Should I remove the build directory?"
112echo "Press <ENTER> to continue or <CTRL><C> to cancel."
113read
114
115rm -rf $BUILD_DIR
116echo "done."
117