#!/bin/bash
# This installation routine installs the video transcoding scripts from 
# http://fangornsrealm.eu
# It tests the availability of needed external programs fetches the 
# programs from the website and installs them. 

function test_depends ()
{
    if [ `type $1  | wc -l ` -lt 1 ] ; then
        echo "$depend is not installed. You might want to change this before running"
	echo "the programs or not all features will be available."
	echo "Some of the programs might not be available through your distributions"
	echo "package installation system. You have to install them by hand then."
	echo "In the head of the scripts is a list of dependencies and links where to"
	echo "get them." 
	echo "You can install them later if you want, but then you have to delete the"
	echo "configuration file in ~/.fangorn/config and start new after you installed"
	echo "them."
	return 1;
    fi
    return 0;
}

for depend in perl java mplayer mencoder ffmpeg mkvmerge mkvinfo MP4Box tsMuxeR ogmmerge neroAacEnc oggenc flac
do 
    test_depends $depend
done
echo "Please place BDSup2Sub.jar inside one of the directories in your PATH variable."
echo "Then change the location setting for it in ~/.fangorn/config if necessary." 
echo "Else conversion of SUP subtitles to VOBSub format will not be available."

if [ ! -d ~/bin ] ; then
    mkdir ~/bin
    if [ $? -ge 1 ] ; then
	return 1;
    fi
fi
if [ `grep $USER /etc/passwd | grep bash | wc -l` -gt 0 ] ; then
    if [ `grep "~/bin" "$PATH"  | wc -l` -eq 0 ] ; then
        echo "export PATH=~/bin:\$PATH" >> ~/.bashrc
        echo "adding ~/bin to your program search path."
        echo "you have to open a new terminal or execute the following command to activate the setting"
        echo " . ~/.bashrc"
    fi
else 
    echo "Please add ~/bin to your PATH environment variable (best put up front)"
    echo "and refresh your shell environment (or start a new shell)"
fi

function test_available () 
{
    if [ ! -f $prog ] ; then
        wget=`which wget`
	if [ -x $wget ] ; then
	    $wget http://progs.fangornsrealm.eu/$prog/$prog 2>&1 >> wget_log.txt
	    if [ $? -lt 1 ] ; then
	        echo "download of $prog was successfull"
            else 
	        return 1;
	    fi
	else 
	    echo "wget is not installed. You can install wget or download the programs yourself."
	    echo "exiting."
	    exit 1;
	fi
    fi
    return 0
}

function install_prog ()
{
    if [ -f $prog ] ; then
        cp $prog ~/bin/
	if [ $? -ge 1 ] ; then
	    return 1;
	fi
    fi
    if [ ! -x ~/bin/prog ] ; then
        chmod u+x ~/bin/$prog
	if [ $? -ge 1 ] ; then
	    return 1;
	fi
    fi
}

for prog in avi2mkv menc riptoh264 choose_video_parameters bluray-info dejunk toolbox_fangorn
do 
    test_available
    if [ $? -ge 1 ] ; then
	echo "there was an error in getting the program $prog. Exiting."
	exit 1; 
    fi
    install_prog
    if [ $? -ge 1 ] ; then
	echo "there was an error in the installation process of $prog. Exiting."
	exit 1; 
    fi
    
    rm $prog
done

rm wget_log.txt


echo "Calling a program to create a first version of the configuration file."
echo "If the necessary programs are already installed, they should be automatically found." 
echo "If one or the other program is not found you can edit the file /home/$USER/.fangorn/config"
echo "after you have installed a missing package."
echo "Or you can delete the file /home/$USER/.fangorn/config and run one of the programs again."

menc -h

