#!/bin/sh

OS=`uname -s 2>/dev/null`
PLATFORM=`uname -i 2>/dev/null`
UDEV_DIR="/etc/udev"
UDEV_CONF_FILE="$UDEV_DIR/udev.conf"


echo ""
echo "WiNRADiO G526 driver and API installation"
echo ""

if [ $(id -ur) != 0 ]; then
    echo "Please, run this installer as root."
    echo ""
    echo "Press ENTER to exit."
    read
    exit 1
fi

if [ "$OS" != "Linux" ]; then
    echo "Your operating system is not supported."
    echo ""
    echo "Press ENTER to exit."
    read
    exit 1
fi

if [ "$PLATFORM" != "i386" ] && [ "$PLATFORM" != "x86_64" ]; then
    echo "Your hardware platform is not supported."
    echo "i386 or x86_64 hardware platform is required."
    echo ""
    echo "Press ENTER to exit."
    read
    exit 1
fi

echo "Compiling G526 kernel module"

cd "driver"

if [ $? != 0 ]; then
    echo "Could not find G526 driver source code."
    echo ""
    echo "Press ENTER to exit."
    read
    exit 1
fi

make clean
make

echo ""

if [ ! -f "wrg526.ko" ]; then
    cd ..
    echo "Compilation of G526 kernel module failed."
    echo "Please, check if development tools and development package"
    echo "for building kernel modules are installed and check if your kernel"
    echo "supports loadable modules."
    echo ""
    echo "Press ENTER to exit."
    read
    exit 1
fi

echo "Writing udev rule"

if [ -f "$UDEV_CONF_FILE" ]; then
    udev_rules=""
    
    source "$UDEV_CONF_FILE"
    
    if [ "$udev_rules" != "" ]; then
	WiNRADiO_RULES="$udev_rules/WiNRADiO.rules"
    else
	WiNRADiO_RULES="$UDEV_DIR/rules.d/WiNRADiO.rules"
    fi
    
    if [ -f "$WiNRADiO_RULES" ]; then
	WRG5=`less "$WiNRADiO_RULES" | grep wrg5`
	
	if [ "$WRG5" == "" ]; then
	    if [ -s "$WiNRADiO_RULES" ]; then
    		echo "" >> "$WiNRADiO_RULES"
    	    fi
	
    	    echo "# WiNRADiO G5xx devices" >> "$WiNRADiO_RULES"
	    echo "KERNEL==\"wrg5*\"				MODE=\"0644\"" >> "$WiNRADiO_RULES"
	else
	    echo "G526 udev rule exists."	    
	fi
    else
	echo "# WiNRADiO G5xx devices" > "$WiNRADiO_RULES"
	echo "KERNEL==\"wrg5*\"				MODE=\"0644\"" >> "$WiNRADiO_RULES"
    fi
else
    echo "Warning: unable to find udev configuration file."
    echo "udev is not installed or its configuration file"
    echo "is another than $UDEV_CONF_FILE."
    echo "G526 udev rule will not be written."
    echo "It is possible only root will have the accces to"
    echo "the G526 devices."
    echo ""
fi

echo "Installing G526 kernel module"
make uninstall >/dev/null 2>&1
make install

echo ""

if [ $? != 0 ]; then
    cd ..

    echo "G526 kernel module installation failed."
    echo ""
    echo "Press ENTER to exit."
    read
    exit 1
fi

cd ..

/sbin/modprobe wrg526

echo "Installing G526 API"

install -m 755 lib/wrg526api.so /usr/lib

if [ $? != 0 ]; then
    if [ "$PLATFORM" == "i386" ]; then
	echo "Installation of G526 API failed."
	echo ""
	echo "Press ENTER to exit."
	read
	exit 1
    else
	echo "Installation of 32bit G526 API failed."
	echo ""
	echo "Press ENTER to exit."
	read
	exit 1
    fi
fi

if [ "$PLATFORM" == "i386" ]; then
    echo "G526 API (wrg526api.so) installed to /usr/lib"
else
    echo "32bit G526 API (wrg526api.so) installed to /usr/lib"
fi

if [ "$PLATFORM" == "x86_64" ]; then
    install -m 755 lib64/wrg526api.so /usr/lib64
    
    if [ $? != 0 ]; then
	echo "Installation of 64bit G526 API failed."
	echo ""
	echo "Press ENTER to exit."
	read
	exit 1
    fi
    
    echo "64bit G526 API (wrg526api.so) installed to /usr/lib64"
fi

echo ""
echo "Done."
echo ""
echo "Press ENTER to exit."
read

exit 0