#!/bin/bash

# This script modified for pclinuxos gnome
# Assuming that packages are already installed.
# Using source in the share folder 
cd /usr/share/webcamstudio/vloopback
mkdir /tmp/webcamstudio-vloopback
cp * /tmp/webcamstudio-vloopback
cd /tmp/webcamstudio-vloopback

# Compiling the source code
zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--question --text="Will now compile source code."
if [ $? != "0" ]; then
	exit;
fi
gksu make | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Compiling..."
RESULT=$?
if [ $RESULT != "0" ]; then
    zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--error --text="An error occured when compiling source code..."
    exit;
fi

# if everything went well, we install the module
zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--question --text="Will now install new module vloopback into your system."
if [ $? != "0" ]; then
	exit;
fi
gksu make install | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Installing..."
RESULT=$?
if [ $RESULT != "0" ]; then
    zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--error --text="An error occured when installing module..."
    exit;
fi

# Ok, modules is not in place
# Now setting the module to auto-load on boot
grep vloopback /etc/modules  | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Auto-Loading..."
RESULT=$?
if [ $RESULT != "0" ]; then
    zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--question --text="Will now Install module vloopback to auto-load on the start-up."
	if [ $? != "0" ]; then
		exit;
	fi
    gksu bash -c "echo #WebcamStudio virtual webcam  >> /etc/modules" | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Auto-Loading..."
    gksu bash -c "echo vloopback >> /etc/modules" | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Auto-Loading..."
    RESULT=$?
    if [ $RESULT != "0" ]; then
	zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--error --text="An error occured when adding module vloopback to auto-load on boot..."
        exit;
    
    fi
else
    zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--question --text="Module vloopback already installed to auto-load."
	if [ $? != "0" ]; then
		exit;
	fi
fi

# loading the module for instant use

lsmod | grep vloopback  | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Unloading..."
RESULT=$?
if [ $RESULT == "0" ]; then
    zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--question --text="Will now unload current vloopback from memory."
	if [ $? != "0" ]; then
		exit;
	fi

    gksu rmmod vloopback  | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Unloading..."
    RESULT=$?
    if [ $RESULT != "0" ]; then
	zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--error --text="An error occured when unloading  module vloopback..."
	exit;
    fi
fi

zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--question --text="Will now load module vloopback in memory"
if [ $? != "0" ]; then
	exit;
fi
gksu modprobe vloopback  | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Loading..."
RESULT=$?
if [ $RESULT != "0" ]; then
    zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--error --text="An error occured when loading module vloopback..."
    exit;
fi

# Clean-up of compiled binaries...
gksu make clean | zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --progress --auto-close --pulsate --title "VLoopback Installer" --text="Cleaning..."

# Now it should work
zenity --window-icon=/usr/share/pixmaps/webcamstudio.png --title "VLoopback Installer" \
	--info --text="Module vloopback is now installed and ready to use..."

gksu "chmod 666 /dev/video*"
gksu "rm -R /tmp/webcamstudio-vloopback"


