#!/bin/bash
#
# Basic System update script
# (CC) BY-NC-SA by speefak
#
########################
### define variables ### 
########################

CODENAME=$(lsb_release -c | tr -d : | cut -f 2)
ARCHITECTURE=$(getconf LONG_BIT)

########################
### define functions ###
########################

function CFGMSCR ()

{
	mkdir 	/tmp/tbsdrv
	cp tbs-linux-drivers_v120827.zip /tmp/tbsdrv
	cd	/tmp/tbsdrv
	unp tbs-linux-drivers_v120827.zip
	unp linux-tbs-drivers.tar.bz2
	cd linux-tbs-drivers

	if [ "$(uname -m)" == "x86_64" ] ; then
		echo
		echo "Configure compiler for 64 bit system"
		echo
		./v4l/tbs-x86_64.sh
	else
		echo
		echo "Configure compiler for 32 bit system"
		./v4l/tbs-x86.sh
		echo
	fi

	make
	make install
}

#######################
### starting script ###
#######################

## checking root status ##

echo
	if [ `whoami` == "root" ] ; then
		echo
		echo "You Are Root - fine :)"
		echo
	else
		echo "Are You Root ?"
		exit
		echo
	fi

	apt-get remove --purge -y s2-liplianin-dkms v4l-dvb-dkms
	apt-get install w-scan dvb-apps

echo -n "Copy channels.conf to /var/lib/vdr  ? Y/N"
proceed=
read -t 5 -n 1 proceed
	if   [ "$proceed" == "y" -o "$proceed" == "Y" ] ; then 
		echo
		cp  channels.conf /var/lib/vdr/

	elif [ "$proceed" == "n" -o "$proceed" == "N" ] ; then
		echo
		echo
		echo "channels.conf not copied"
	else
		echo
		echo
		echo "No or wrong Input - continue without copying channels.conf"
		echo
	fi


echo -n "Download and build driver for TBS 6981 DVB-S2 satellite card // $ARCHITECTURE bit architecture ? Y/N"
proceed=
read -t 5 -n 1 proceed
	if   [ "$proceed" == "y" -o "$proceed" == "Y" ] ; then
		echo
		CFGMSCR

	elif [ "$proceed" == "n" -o "$proceed" == "N" ] ; then
		echo
		echo
		echo "Proceeding without building driver"
	else
		echo
		echo
		echo "No or wrong Input - Proceeding without building driver"
		echo
	fi



echo -n "Reboot System ? Y/N"
proceed=
read -n 1 proceed
	if   [ "$proceed" == "y" -o "$proceed" == "Y" ] ; then
		shutdown -r now

	else
		echo
	fi

### channel scanning
# w_scan -fs -s S19E2 -o 7 >> channels.conf

