#!/bin/sh
tput clear
id=`id|awk '{print($2)}'|cut -f2 -d '('|cut -f1 -d ')'`
if [ "$id" != "root" ]
then
	echo "Only root can run this script"
	exit
fi
more etc/gpl.txt
echo -n "Enter (y) to  agree the above GPL TERMS and CONDITIONS ? y/n : "
read answer
[ "$answer" != "y" ] && exit
tput clear
Version=0.2.0
echo "VDRConvert $Version Installation"
echo "-----------------------------"
echo ""

if [ -f /etc/gentoo-release ]
then
	system=gentoo
	cat /etc/gentoo-release
elif [ -f /etc/SuSE-release ]
then
	system="suse"
	cat /etc/SuSE-release
elif [ -f /etc/redhat-release ]
then
	system="RedHat"
	cat /etc/redhat-release
elif [ -f "/etc/debian_version" ]
then
	system="debian"
	echo -n "Debian "
	cat /etc/debian_version
elif [ -f "/etc/slackware-version" ]
then
	system="sl"
	cat /etc/slackware-version
fi
kern=`uname -r`
echo "Kernel $kern"
echo ""

echo -n "Enter Installation prefix (/usr, /usr/local, ...) : "
read vdrconvertprefix
echo -n "Enter User : "
read vdrconvertuser
echo -n "Enter group : "
read vdrconvertgroup
groupcheck=`cat /etc/group|grep "${vdrconvertgroup}:"|cut -f6 -d ':'|wc -l`
if [ $groupcheck -ne 1 ]
then
	echo "group $vdrconvertgroup not found"
	echo -n "create group y/n ? "
	read creategroup
	if [ "$creategroup" = "y" -o  "$creategroup" = "yes" ]
	then
		groupadd $vdrconvertgroup
	else 
		exit
	fi
fi
homechk=`cat /etc/passwd|grep "^${vdrconvertuser}:"|cut -f6 -d ':'|wc -l`
if [ $homechk -ne 1 ]
then
	echo "user $vdrconvertuser  not found"
	echo -n "create account y/n ? "
	read createuser
	if [ "$createuser" = "y" -o  "$createuser" = "yes" ]
	then
		useradd -g $vdrconvertgroup $vdrconvertuser
	else 
		exit
	fi
fi
homedir=`cat /etc/passwd|grep "^${vdrconvertuser}:"|cut -f6 -d ':'`
[ ! -d "${homedir}/.vdrconvert" ] && mkdir -p "${homedir}/.vdrconvert"
chown ${vdrconvertuser}:${vdrconvertgroup} $homedir
chmod 0750 $homedir
chown ${vdrconvertuser}:${vdrconvertgroup} $homedir/.vdrconvert
chmod 0750 $homedir/.vdrconvert
if [ ! -d $vdrconvertprefix ]
then
	mkdir -p  $vdrconvertprefix
	chmod 0750 $vdrconvertprefix
	chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix 
fi

echo -n "Enter VDR's videodir (/video, /video0, ...) : "
read videodir
if [ ! -d $videodir ]
then
    echo "wrong videodir $videodir"
    exit
fi
chmod 0750 $videodir
chown ${vdrconvertuser}:${vdrconvertgroup} $videodir
echo -n "Enter target directory (/video, /video0, /film, /movies, ...) : "
read destdir
if [ ! -d $destdir ]
then
    echo "wrong destdir $destdir"
    exit
fi
chmod 0750 $destdir
chown ${vdrconvertuser}:${vdrconvertgroup} $destdir

mkdir -p /var/log/vdrconvert
chmod 0755 /var/log/vdrconvert
chown  ${vdrconvertuser}:${vdrconvertgroup} /var/log/vdrconvert
mkdir -p /var/run/vdrconvert
chmod 0755 /var/run/vdrconvert
chown  ${vdrconvertuser}:${vdrconvertgroup} /var/run/vdrconvert

echo "Select demuxer"
echo "--------------"
echo " 1) vdrsync.pl ( Need Perl )"
echo " 2) vdrsync.pl Developer version ( Need Perl )"
echo " 3) Project X ( Need Java and Xvfb )"
echo " 4) PVAStrumento ( Need wine and Xvfb )"
echo ""
echo -n "Your choice : "
read demu
case $demu in
    1)
	demuxer=vdrsync
    ;;
    2)
	demuxer=vdrsync2
    ;;
    3)
	demuxer=px
	echo "TERMS OF CONDITIONS:"
	echo "(1) this is a free Java based demux utility."
	echo "(2) It is intended for educational purposes only, as a non-commercial test project."
	echo "(3) It may not be used otherwise. Most parts are only experimental."
	echo "(4) released under the terms of the GNU GPL"
	echo "(5) there is NO WARRANTY of any kind attached to this software"
	echo "(6) use it at your own risk and for your own education as it was meant"
	echo ""	
	type java 2>/dev/null
	echo ""
	echo -n "Enter path to java binary or CTRl-C if you not agree the above terms = ? "
	read java
    ;;
    4)
	demuxer=pva
	echo ""
	echo "========================================================="
	echo "= Please edit wine config for user "
	echo "= $vdrconvertuser"
	echo "= (after wine configuration) in home directory"
	echo "= <$homedir/.wine/config> and set"
	echo "= PrivateColorMap to \"Y\" for Xvfb"
	echo "= in section [x11drv]"
	echo "========================================================="
	echo ""
    ;;
    *)
	echo "Unknown demuxer."
	exit
    ;;
esac

echo""
echo "Enable vdrconvert for :"
echo -n "DivX ? "
read usedivx
echo -n "DVD ? "
read usedvd
echo -n "VCD ? "
read usevcd
echo -n "SVCD ? "
read usesvcd
echo -n "MPG ? "
read usempg
echo -n "MP3 ? "
read usemp3
echo -n "AC3 ? "
read useac3
echo -n "Burning ? "
read useburn

if [ ! -d $prefix ]
then
    echo -n "create directory $vdrconvertprefix y/n : "
    read answer
    [ "$answer" = "y" -o "$answer" = "yes" ] && mkdir -p $prefix || exit
fi
mkdir -p /var/spool/vdrconvert
chown ${vdrconvertuser}:${vdrconvertgroup} /var/spool/vdrconvert

if [ ! -d $vdrconvertprefix/bin ]
then
    mkdir -p  $vdrconvertprefix/bin
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/bin
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/fonts ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/fonts/truetype
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/fonts/truetype
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/images ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/images
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/images
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/images/pal ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/images/pal
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/images/pal
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/images/buttons ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/images/buttons
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/images/buttons
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/images/buttons/duester ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/images/buttons/duester
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/images/buttons/duester
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/pva ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/pva
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/pva
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/pX ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/pX
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/pX
fi
if [ ! -d $vdrconvertprefix/share/vdrconvert/postscript ]
then
    mkdir -p  $vdrconvertprefix/share/vdrconvert/postscript
    chown ${vdrconvertuser}:${vdrconvertgroup} $vdrconvertprefix/share/vdrconvert/postscript
fi

let overwrite="n"
save=`date +"%d.%m.%y-%T"`
[ -f deinstall.sh ] && mv deinstall.sh deinstall.${save}.sh
echo "#!/bin/sh" >> deinstall.sh
echo "echo \"Deinstall vdrconvert y/n ? \"" >> deinstall.sh
echo "read deinst" >> deinstall.sh
echo "[ ! \"\$deinst\" = \"y\" -a ! \"\$deinst\" = \"yes\" ] && exit" >> deinstall.sh

for i in convstart.sh convnow.sh add.sh burn.sh cap.sh del.sh dojpeg2eps.ps \
	 marks2times.sh helperfunctions.sh grab.sh ins.sh jpeg2eps \
	 jpeg2eps.ps nq.sh preview.sh status.sh vdr2ac3.sh \
	 vdr2divx.sh vdr2dvd.sh vdr2mp3.sh vdr2mpg.sh vdr2svcd.sh \
	 vdr2vcd.sh vdrconvert.sh vdrsync-0.1.2.2-dev2.pl \
	 vdrsync-0.1.2.2.pl viewjpeg.ps writedvd.sh
do
    echo "echo \"Deintall $vdrconvertprefix/bin/$i\"" >> deinstall.sh
    echo "rm $vdrconvertprefix/bin/$i" >> deinstall.sh
    if [ ! -f $vdrconvertprefix/bin/$i ]
    then
        echo -n "Install $i : "
        install -o $vdrconvertuser -g $vdrconvertgroup -m 0755 bin/$i $vdrconvertprefix/bin/$i && echo "OK." || echo "Fail"
    else
	if [ "$overwrite" != "a" ]
	then
	    echo -n " $vdrconvertprefix/bin/$i already exists. Overwrite y/n/a ? "
	    read overwrite
	fi
	if [ "$overwrite" = "y" -o "$overwrite" = "yes" -o "$overwrite" = "a" -o "$overwrite" = "all" ]
	then
            echo -n "Install $i : "
	    install -o $vdrconvertuser -g $vdrconvertgroup  -m 0755 bin/$i $vdrconvertprefix/bin/$i && echo "OK." || echo "Fail"
	fi
    fi
done

echo "Install Demuxer PVAStrumento"
for i in share/vdrconvert/pva/*
do
        echo -n "Install $i : "
	install -o $vdrconvertuser -g $vdrconvertgroup  -m 0644 $i $vdrconvertprefix/share/vdrconvert/pva && echo "OK." || echo "Fail"
	ii=`echo $i|xargs basename`
	echo "echo \"Deinstall $vdrconvertprefix/share/vdrconvert/pva/$ii\"" >> deinstall.sh
	echo "rm $vdrconvertprefix/share/vdrconvert/pva/$ii" >> deinstall.sh
done
echo "rmdir $vdrconvertprefix/share/vdrconvert/pva" >> deinstall.sh

echo "Install Demuxer Project X"
for i in share/vdrconvert/pX/*
do
        echo -n "Install $i : "
	install -o $vdrconvertuser -g $vdrconvertgroup  -m 0644 $i $vdrconvertprefix/share/vdrconvert/pX && echo "OK." || echo "Fail"
	ii=`echo $i|xargs basename`
	echo "echo \"Deinstall $vdrconvertprefix/share/vdrconvert/pX/$ii\"" >> deinstall.sh
	echo "rm $vdrconvertprefix/share/vdrconvert/pX/$ii" >> deinstall.sh
done
echo "rmdir $vdrconvertprefix/share/vdrconvert/pX" >> deinstall.sh

echo "Install DVD Images"
for i in share/vdrconvert/images/silence-1s.mp2
do
        echo -n "Install $i : "
	install -o $vdrconvertuser -g $vdrconvertgroup  -m 0644 $i $vdrconvertprefix/share/vdrconvert/images && echo "OK." || echo "Fail"
	ii=`echo $i|xargs basename`
	echo "echo \"Deinstall $vdrconvertprefix/share/vdrconvert/images/$ii\"" >> deinstall.sh
	echo "rm $vdrconvertprefix/share/vdrconvert/images/$ii" >> deinstall.sh
done
echo "rmdir $vdrconvertprefix/share/vdrconvert/images" >> deinstall.sh
for i in share/vdrconvert/images/pal/*
do
        echo -n "Install $i : "
	install -o $vdrconvertuser -g $vdrconvertgroup  -m 0644 $i $vdrconvertprefix/share/vdrconvert/images/pal && echo "OK." || echo "Fail"
	ii=`echo $i|xargs basename`
	echo "echo \"Deinstall $vdrconvertprefix/share/vdrconvert/images/pal/$ii\"" >> deinstall.sh
	echo "rm $vdrconvertprefix/share/vdrconvert/images/pal/$ii" >> deinstall.sh
done
echo "rmdir $vdrconvertprefix/share/vdrconvert/images/buttons/duester" >> deinstall.sh
for i in share/vdrconvert/images/buttons/duester/*
do
        echo -n "Install $i : "
	install -o $vdrconvertuser -g $vdrconvertgroup  -m 0644 $i $vdrconvertprefix/share/vdrconvert/images/buttons/duester && echo "OK." || echo "Fail"
	ii=`echo $i|xargs basename`
	echo "echo \"Deinstall $vdrconvertprefix/share/vdrconvert/images/$ii\"" >> deinstall.sh
	echo "rm $vdrconvertprefix/share/vdrconvert/images/$ii" >> deinstall.sh
done
echo "rmdir $vdrconvertprefix/share/vdrconvert/images" >> deinstall.sh

echo "Install fonts"
for i in share/vdrconvert/fonts/truetype/*
do
        echo -n "Install $i : "
	install -o $vdrconvertuser -g $vdrconvertgroup  -m 0644 $i $vdrconvertprefix/share/vdrconvert/fonts/truetype && echo "OK." || echo "Fail"
	ii=`echo $i|xargs basename`
	echo "echo \"Deinstall $vdrconvertprefix/share/vdrconvert/fonts/truetype/$ii\"" >> deinstall.sh
	echo "rm $vdrconvertprefix/share/vdrconvert/fonts/truetype/$ii" >> deinstall.sh
done
echo "rmdir $vdrconvertprefix/share/vdrconvert/fonts/truetype" >> deinstall.sh
echo "rmdir $vdrconvertprefix/share/vdrconvert/fonts" >> deinstall.sh
echo "Install postscript"
for i in share/vdrconvert/postscript/*
do
        echo -n "Install $i : "
	install -o $vdrconvertuser -g $vdrconvertgroup  -m 0644 $i $vdrconvertprefix/share/vdrconvert/postscript && echo "OK." || echo "Fail"
	ii=`echo $i|xargs basename`
	echo "echo \"Deinstall $vdrconvertprefix/share/vdrconvert/postscript/$ii\"" >> deinstall.sh
	echo "rm $vdrconvertprefix/share/vdrconvert/postscript/$ii" >> deinstall.sh
done
echo "rmdir $vdrconvertprefix/share/vdrconvert/postscript" >> deinstall.sh
echo "rmdir $vdrconvertprefix/share/vdrconvert" >> deinstall.sh
echo "rmdir $vdrconvertprefix/share 2>/dev/null" >> deinstall.sh
echo ""

echo "Create environment file $homedir/.vdrconvert/vdrconvert.env"
if [ -f $homedir/.vdrconvert/vdrconvert.env ] 
then
	echo "echo \"Deinstall Directory $homedir/.vdrconvert\"" >> deinstall.sh
	echo "rm -rf $homedir/.vdrconvert" >> deinstall.sh
	save="$homedir/.vdrconvert/vdrconvert.env.`date +\"%d.%m.%y-%T\"`"
	mv $homedir/.vdrconvert/vdrconvert.env "$save"
	status=$?
	if [ $status -eq 0 ]
	then
		echo "Existing  $homedir/.vdrconvert/vdrconvert.env saved as $save"
	else
		echo "WARN : Fail save existing  $homedir/.vdrconvert/vdrconvert.env to $save"
	fi
	echo -n "Press enter to continue Ctrl-C to abort-> "
	read a
fi

# XXX Recode check


echo ""
echo "Create Environment in $homedir/.vdrconvert/vdrconvert.env"
cat << ENVEOF > $homedir/.vdrconvert/vdrconvert.env
VDRCONVERTDIR=$vdrconvertprefix
VDRCONVERTBINDIR=$vdrconvertprefix/bin
VCOQUEUEDIR=/var/spool/vdrconvert
VCOLOGDIR=/var/log/vdrconvert
VCORUNDIR=/var/run/vdrconvert
export VDRCONVERTDIR VDRCONVERTBINDIR VCOQUEUEDIR VCOLOGDIR VCORUNDIR

PATH=\$PATH:\$VDRCONVERTDIR/bin:/usr/X11R6/bin
export PATH

#DEBUG=yes
export LANG=de_DE@euro
export LC_CTYPE=de_DE@euro
ENVEOF

case $system in
	"RedHat")
cat << ENVEOF >> $homedir/.vdrconvert/vdrconvert.env
RECODE="recode latin1"
#RECODE=cat
#RECODE="recode latin1"
#RECODE="recode latin1..UTF-8"
#RECODE="recode greek..UTF-8"
ENVEOF
		;;
	"gentoo")
cat << ENVEOF >> $homedir/.vdrconvert/vdrconvert.env
RECODE="recode latin1"
#RECODE=cat
#RECODE="recode latin1"
#RECODE="recode latin1..UTF-8"
#RECODE="recode greek..UTF-8"
ENVEOF
		;;
	"suse")
cat << ENVEOF >> $homedir/.vdrconvert/vdrconvert.env
RECODE="recode latin1..UTF-8"
#RECODE=cat
#RECODE="recode latin1"
#RECODE="recode latin1..UTF-8"
#RECODE="recode greek..UTF-8"
ENVEOF
		;;
	"debian")
cat << ENVEOF >> $homedir/.vdrconvert/vdrconvert.env
RECODE="recode latin1"
#RECODE=cat
#RECODE="recode latin1"
#RECODE="recode latin1..UTF-8"
#RECODE="recode greek..UTF-8"
ENVEOF
		;;
	"sl")
cat << ENVEOF >> $homedir/.vdrconvert/vdrconvert.env
RECODE="recode latin1"
#RECODE=cat
#RECODE="recode latin1"
#RECODE="recode latin1..UTF-8"
#RECODE="recode greek..UTF-8"
ENVEOF
		;;
	*)
cat << ENVEOF >> $homedir/.vdrconvert/vdrconvert.env
RECODE=cat
#RECODE=cat
#RECODE="recode latin1"
#RECODE="recode latin1..UTF-8"
#RECODE="recode greek..UTF-8"
ENVEOF
		;;
esac

cat << ENVEOF >> $homedir/.vdrconvert/vdrconvert.env
#
SVDRPSEND=svdrpsend.pl

# Base Destination dir
DESTDIR=$destdir
# root of VDR video structure - usually /video or /video0
# default /video
VDRROOT=$videodir

# Select your Demuxer
# - vdrsync
# - vdrsync2 ( dev2 version )
# - px for Project X ( Need java and Xvfb )
# - pva for PVAStrumento ( Need wine and Xvfb )
#
# Please edit wine config for vdr-user
# (after runing wine) in home directory
# </home/vdr-user/.wine/config> and set
# PrivateColorMap to "Y" for Xvfb / PVA
# in section [x11drv]
#

DEMUXER=$demuxer
#
DVDMAINSILENCEFILE=$vdrconvertprefix/share/vdrconvert/images/silence-1s.mp2
DVDSUBSILENCEFILE=\$DVDMAINSILENCEFILE

ENVEOF

[ "$usedivx" = "y" -o "$usedivx" = "yes" ] && JOBS="$JOBS vdr2divx"
[ "$usedvd" = "y" -o "$usedvd" = "yes" ] && JOBS="$JOBS vdr2dvd"
[ "$usevcd" = "y" -o "$usevcd" = "yes" ] && JOBS="$JOBS vdr2vcd"
[ "$usesvcd" = "y" -o "$usesvcd" = "yes" ] && JOBS="$JOBS vdr2svcd"
[ "$usempg" = "y" -o "$usempg" = "yes" ] && JOBS="$JOBS vdr2mpg"
[ "$usemp3" = "y" -o "$usemp3" = "yes" ] && JOBS="$JOBS vdr2mp3"
[ "$useac3" = "y" -o "$useac3" = "yes" ] && JOBS="$JOBS vdr2ac3"
[ "$useburn" = "y" -o "$useburn" = "yes" ] && JOBS="$JOBS burn"

[ ! -z "$JOBS" ] && echo "CONVERT_JOBS=\"${JOBS}\"" >> $homedir/.vdrconvert/vdrconvert.env
[ ! -z "$java" ] && echo "JAVA=\"${java}\"" >> $homedir/.vdrconvert/vdrconvert.env

#

echo ""
echo "Create cdlabelgen with correct search path to postscript templates"
prefix=`echo $vdrconvertprefix/share/vdrconvert/postscript|sed 's/\//\\\\\//g'`
echo "s/VDRCONVERT_POSTSCRIPT_PATH/$prefix/g" > sedcmd
cat etc/cdlabelgen |sed -f sedcmd > $vdrconvertprefix/bin/cdlabelgen
chmod +x $vdrconvertprefix/bin/cdlabelgen
rm -f sedcmd
echo "echo \"Deinstall $vdrconvertprefix/bin/cdlabelgen\"" >> deinstall.sh
echo "rm $vdrconvertprefix/bin/cdlabelgen" >> deinstall.sh
echo "rmdir $vdrconvertprefix/bin 2>/dev/null" >> deinstall.sh

echo ""
echo "Create German recccmds.conf in current directory <VDRconvert-reccmds.conf>"
cat << RECEOF > VDRconvert-reccmds.conf
Zu DVD-Liste                    : $vdrconvertprefix/bin/ins.sh vdr2dvd
Entferne von DVD-Liste?         : $vdrconvertprefix/bin/del.sh vdr2dvd
Aktiviere DVD-Liste?            : $vdrconvertprefix/bin/convstart.sh vdr2dvd
Zeige DVD-Liste                 : $vdrconvertprefix/bin/cap.sh vdr2dvd
DVD Hintergrundbild (Hauptmenue): $vdrconvertprefix/bin/grab.sh "background.pnm"
DVD Hintergrundbild (Untermenue): $vdrconvertprefix/bin/grab.sh "subbackground.pnm"
Letzter status Grab             : cat /tmp/vdrgrab
Zu DivX-Liste                   : $vdrconvertprefix/bin/ins.sh vdr2divx
Entferne von DivX-Liste?        : $vdrconvertprefix/bin/del.sh vdr2divx
Aktiviere DivX?                 : $vdrconvertprefix/bin/convstart.sh vdr2divx
Zeige DivX-Liste                : $vdrconvertprefix/bin/cap.sh vdr2divx
Zu SVCD-Liste                   : $vdrconvertprefix/bin/ins.sh vdr2svcd
Entferne von SVCD-Liste?        : $vdrconvertprefix/bin/del.sh vdr2svcd
Aktiviere SVCD-Liste?           : $vdrconvertprefix/bin/convstart.sh vdr2svcd
Zeige SVCD-Liste                : $vdrconvertprefix/bin/cap.sh /vdr2svcd
Zu VCD-Liste                    : $vdrconvertprefix/bin/ins.sh vdr2vcd
Aktiviere VCD-Liste?            : $vdrconvertprefix/bin/convstart.sh vdr2vcd
Entferne von VCD-Liste?         : $vdrconvertprefix/bin/del.sh vdr2vcd
Zeige VCD-Liste                 : $vdrconvertprefix/bin/cap.sh vdr2vcd
Erstelle DivX sofort?           : $vdrconvertprefix/bin/convnow.sh vdr2divx
Erstelle DVD sofort?            : $vdrconvertprefix/bin/convnow.sh vdr2dvd
Erstelle SVCD sofort?           : $vdrconvertprefix/bin/convnow.sh vdr2svcd
Erstelle VCD sofort?            : $vdrconvertprefix/bin/convnow.sh vdr2vcd
Erstelle Mpeg ( sync )?         : $vdrconvertprefix/bin/convnow.sh vdr2mpg
Erstelle Mp3?                   : $vdrconvertprefix/bin/convnow.sh vdr2mp3
Erstelle AC3?                   : $vdrconvertprefix/bin/convnow.sh vdr2ac3
RECEOF

echo ""
echo "Create English recccmds.conf in current directory <VDRconvert-en-reccmds.conf>"
cat << RECENEOF > VDRconvert-en-reccmds.conf
To DVDLIST                      : $vdrconvertprefix/bin/ins.sh vdr2dvd
Remove from DVDLIST?            : $vdrconvertprefix/bin/del.sh vdr2dvd
Activate DVDLIST?               : $vdrconvertprefix/bin/convstart.sh vdr2dvd
Show DVDList                    : $vdrconvertprefix/bin/cap.sh vdr2dvd
DVD Grab background             : $vdrconvertprefix/bin/grab.sh "background.pnm"
DVD Grab subbackground          : $vdrconvertprefix/bin/grab.sh "subbackground.pnm"
Last GRAB status                : cat /tmp/vdrgrab
To DivxList                     : $vdrconvertprefix/bin/ins.sh vdr2divx
Remove from DivxList?           : $vdrconvertprefix/bin/del.sh vdr2divx
Activate Divx?                  : $vdrconvertprefix/bin/convstart.sh vdr2divx
Show DivxLIST                   : $vdrconvertprefix/bin/cap.sh vdr2divx
To SVCD List                    : $vdrconvertprefix/bin/ins.sh vdr2svcd
Remove from SVCDList?           : $vdrconvertprefix/bin/del.sh vdr2svcd
Activate SVCD?                  : $vdrconvertprefix/bin/convstart.sh vdr2svcd
Show SVCD List                  : $vdrconvertprefix/bin/cap.sh /vdr2svcd
To VCDList                      : $vdrconvertprefix/bin/ins.sh vdr2vcd
Activate VCD?                   : $vdrconvertprefix/bin/convstart.sh vdr2vcd
Remove from VCDList?            : $vdrconvertprefix/bin/del.sh vdr2vcd
Show VCD List                   : $vdrconvertprefix/bin/cap.sh vdr2vcd
Create DivX now?                : $vdrconvertprefix/bin/convnow.sh vdr2divx
Create DVD now?                 : $vdrconvertprefix/bin/convnow.sh vdr2dvd
Create SVCD now?                : $vdrconvertprefix/bin/convnow.sh vdr2svcd
Create VCD now?                 : $vdrconvertprefix/bin/convnow.sh vdr2vcd
Make Mpeg ( sync )?             : $vdrconvertprefix/bin/convnow.sh vdr2mpg
Make Mp3?                       : $vdrconvertprefix/bin/convnow.sh vdr2mp3
Make AC3?                       : $vdrconvertprefix/bin/convnow.sh vdr2ac3
RECENEOF

echo ""
echo "Create Hellenic (greeklish) recccmds.conf in current directory <VDRconvert-el-reccmds.conf>"
cat << RECELEOF > VDRconvert-el-reccmds.conf
Prosthesi stin Lista DVD        : $vdrconvertprefix/bin/ins.sh vdr2dvd
Aferesi apo lista DVD?          : $vdrconvertprefix/bin/del.sh vdr2dvd
Energopiisi lista DVD?          : $vdrconvertprefix/bin/convstart.sh vdr2dvd
Endiksi lista DVD               : $vdrconvertprefix/bin/cap.sh vdr2dvd
DVD Grab background             : $vdrconvertprefix/bin/grab.sh "background.pnm"
DVD Grab subbackground          : $vdrconvertprefix/bin/grab.sh "subbackground.pnm"
Last GRAB status                : cat /tmp/vdrgrab
Prosthesi stin lista Divx       : $vdrconvertprefix/bin/ins.sh vdr2divx
Aferesi apo lista DivX?         : $vdrconvertprefix/bin/del.sh vdr2divx
Energopiisi Divx?               : $vdrconvertprefix/bin/convstart.sh vdr2divx
Endiksi listas DivX             : $vdrconvertprefix/bin/convstart.sh vdr2divx
Prosthesi stin lista SVCD       : $vdrconvertprefix/bin/ins.sh vdr2svcd
Aferesi apo lista SVCD?         : $vdrconvertprefix/bin/del.sh vdr2svcd
Energopiisi listat SVCD         : $vdrconvertprefix/bin/convstart.sh vdr2svcd
Endiksi listat SVCD             : $vdrconvertprefix/bin/cap.sh /vdr2svcd
Prosthesi stin lista VCD        : $vdrconvertprefix/bin/ins.sh vdr2vcd
Energopiisi VCD?                : $vdrconvertprefix/bin/convstart.sh vdr2vcd
Aferesi apo lista VCD?          : $vdrconvertprefix/bin/del.sh vdr2vcd
Endiksi listas VCD              : $vdrconvertprefix/bin/cap.sh vdr2vcd
Metatropi se DivX tora?         : $vdrconvertprefix/bin/convnow.sh vdr2divx
Metatropi se DVD tora?          : $vdrconvertprefix/bin/convnow.sh vdr2dvd
Metatropi se SVCD tora?         : $vdrconvertprefix/bin/convnow.sh vdr2svcd
Metatropi se VCD tora?          : $vdrconvertprefix/bin/convnow.sh vdr2vcd
Metatropi se Mpeg tora?         : $vdrconvertprefix/bin/convnow.sh vdr2mpg
Metatropi se Mp3 tora?          : $vdrconvertprefix/bin/convnow.sh vdr2mp3
Metatropi se AC3 tora?          : $vdrconvertprefix/bin/convnow.sh vdr2ac3
RECELEOF

echo ""
echo "Create commands.conf in current directory <VDRConvert-commands.conf>"

PATH=$PATH:$vdrconvertprefix/bin
export PATH

GEN_PROGS="O:recode \
	   O:logrotate"

DIVX_PROGS="R:mencoder \
	    R:tcmplex \
	    O:lame"

MPG_PROGS="R:tcmplex \
	   O:tcmplex-panteltje"

MP3_PROGS="R:lame \
	   O:mpg123"

VCD_PROGS="R:tosvcd \
	   R:mpeg2enc \
	   R:yuvdenoise \
	   R:yuvscaler \
	   R:mplex \
	   R:vcdimager \
	   O:cdrdao"

DVD_PROGS="R:dvdauthor \
	   R:jpegtopnm \
	   R:mpeg2enc \
	   R:pnmtopng \
      	   R:pngtopnm \
	   R:ppmmake \
	   R:mplex \
      	   R:mpeg2enc \
	   R:pnmtojpeg \
      	   R:pnmfile \
      	   R:pnmtile \
	   R:gozer \
	   R:mkisofs \
	   R:transcode \
	   O:cdlabelgen \
	   O:jpeg2eps \
	   O:arecord \
	   O:mp2enc \
	   O:growisofs \
	   O:requant \
	   O:tcrequant \
	   O:dvd+rw-mediainfo \
	   O:dvd+rw-format \
	   O:dvd+rw-booktype"
	   

DVD__PROGS="O:tcmplex \
	    O:tcmplex-panteltje \
	    O:dvb-mplex"

	   
echo "Voraussetzungen werden ueberprueft"
echo "Check for additional required Software"
echo "elenchos prosthetou logismikou"
echo "--------------------------------------"
echo ""
    echo "General Check"|tee Check.txt
    echo "----------"
    for i in $GEN_PROGS
    do
	prg=`echo $i|cut -f2 -d ':'`
        type $prg >/dev/null 2>&1
        status=$?
        [ $status -eq 0 ] && (printf "%-20s OK" $prg|tee -a Check.txt) || (printf "%-20s not found" $prg | tee -a Check.txt)
	[ "`echo $i|cut -f1 -d ':'`" = "R" ] && (printf " (Required)\n"|tee -a Check.txt) || (printf " (Optional)\n"|tee -a Check.txt)
    done
    echo ""
    echo -n "Press Enter to continue -> "
    read a
echo ""
if [ "$usedivx" = "y" -o "$usedivx" = "yes" ]
then
    echo "DivX Check"|tee -a Check.txt
    echo "----------"
    for i in $DIVX_PROGS
    do
	prg=`echo $i|cut -f2 -d ':'`
        type $prg >/dev/null 2>&1
        status=$?
        [ $status -eq 0 ] && printf "%-20s OK" $prg|tee -a Check.txt || printf "%-20s not found" $prg|tee -a Check.txt
	[ "`echo $i|cut -f1 -d ':'`" = "R" ] && (printf " (Required)\n"|tee -a Check.txt) || (printf " (Optional)\n"|tee -a Check.txt)
    done
    echo "Be sure mencoder are compiled with libmp3lame support"
    echo ""
    echo -n "Press Enter to continue -> "
    read a
fi
if [ "$usempg" = "y" -o "$usempg" = "yes" ]
then
    echo "MPG Check"|tee -a Check.txt
    echo "----------"
    for i in $MPG_PROGS
    do
	prg=`echo $i|cut -f2 -d ':'`
        type $prg >/dev/null 2>&1
        status=$?
        [ $status -eq 0 ] && printf "%-20s OK" $prg|tee -a Check.txt || printf "%-20s not found" $prg|tee -a Check.txt
	[ "`echo $i|cut -f1 -d ':'`" = "R" ] && (printf " (Required)\n"|tee -a Check.txt) || (printf " (Optional)\n"|tee -a Check.txt)
    done
    echo ""
    echo -n "Press Enter to continue -> "
    read a
fi
if [ "$usemp3" = "y" -o "$usemp3" = "yes" ]
then
    echo "MP3 Check"|tee -a Check.txt
    echo "----------"
    for i in $MP3_PROGS
    do
	prg=`echo $i|cut -f2 -d ':'`
        type $prg >/dev/null 2>&1
        status=$?
        [ $status -eq 0 ] && printf "%-20s OK" $prg|tee -a Check.txt || printf "%-20s not found" $prg|tee -a Check.txt
	[ "`echo $i|cut -f1 -d ':'`" = "R" ] && (printf " (Required)\n"|tee -a Check.txt) || (printf " (Optional)\n"|tee -a Check.txt)
    done
    echo ""
    echo -n "Press Enter to continue -> "
    read a
fi
if [ "$useac3" = "y" -o "$useac3" = "yes" ]
then
    echo "AC3 Check"|tee -a Check.txt
    echo "----------"
    echo "The are no requirements for extracting AC3."
    echo ""
    echo -n "Press Enter to continue -> "
    read a
fi
if [ "$usevcd" = "y" -o "$usevcd" = "yes" -o "$usesvcd" = "y" -o "$usesvcd" = "yes" ]
then
    echo "(S)VCD Check"|tee -a Check.txt
    echo "----------"
    for i in $VCD_PROGS
    do
	prg=`echo $i|cut -f2 -d ':'`
        type $prg >/dev/null 2>&1
        status=$?
        [ $status -eq 0 ] && printf "%-20s OK" $prg|tee -a Check.txt || printf "%-20s not found" $prg|tee -a Check.txt
	[ "`echo $i|cut -f1 -d ':'`" = "R" ] && (printf " (Required)\n"|tee -a Check.txt) || (printf " (Optional)\n"|tee -a Check.txt)
    done
    echo ""
    echo -n "Press Enter to continue -> "
    read a
fi
if [ "$usedvd" = "y" -o "$usedvd" = "yes" ]
then
    echo "DVD Check"|tee -a Check.txt
    echo "----------"
    for i in $DVD__PROGS
    do
	prg=`echo $i|cut -f2 -d ':'`
        type $prg >/dev/null 2>&1
        status=$?
        [ $status -eq 0 ] && printf "%-20s OK" $prg|tee -a Check.txt || printf "%-20s not found" $prg|tee -a Check.txt
	[ "`echo $i|cut -f1 -d ':'`" = "R" ] && (printf " (Required)\n"|tee -a Check.txt) || (printf " (Optional)\n"|tee -a Check.txt)
    done
    echo ""
    echo -n "Press Enter to continue -> "
    read a 
    for i in $DVD_PROGS
    do
	prg=`echo $i|cut -f2 -d ':'`
        type $prg >/dev/null 2>&1
        status=$?
        [ $status -eq 0 ] && printf "%-20s OK" $prg|tee -a Check.txt || printf "%-20s not found" $prg|tee -a Check.txt
	[ "`echo $i|cut -f1 -d ':'`" = "R" ] && (printf " (Required)\n"|tee -a Check.txt) || (printf " (Optional)\n"|tee -a Check.txt)
    done
    echo ""
    echo -n "Press Enter to continue -> "
    read a
fi

if [ "$useburn" = "y" -o "$useburn" = "yes" ]
then
    echo ""
    echo "Drive selection :"
    if [ -f /proc/sys/dev/cdrom/info ]
    then
        echo "Available drives : "
	echo ""
        for i in `cat /proc/sys/dev/cdrom/info|grep "drive name"|cut -f2 -d ':'`
	do 
		printf "\t%s\n" "$i"
	done
    fi
    echo ""
    echo -n "Enter the name of your DVD/CD device (scdx, srx, hdx, ... ) : "
    read device
    if [ -x `which cdrdao` ]
    then
        iddevice=$(cdrdao scanbus 2>&1|tail -n 1|cut -f1 -d ':')
        [ "$iddevice" = "ATA" ] && iddevice=$(cdrdao scanbus 2>&1|tail -n 1|cut -f2 -d ':')
	echo "Id : $iddevice for $device maybe wrong in ATAPI/SCSI environment"
	echo -n "Insert line MEDIAWRITER=\"/dev/${device}:$iddevice\" to vdrconvert.env y/n ? "
	read mwriter
	if [ "$mwriter" = "y" -o "$mwriter" = "yes" ]
	then
          cat << BURNEOF >> $homedir/.vdrconvert/vdrconvert.env
MEDIAWRITER="/dev/$device:$iddevice"
BURNEOF
        fi
	for i in DVD DIVX MP3 MPG SVCD VCD
	do
	    echo -n "Enable Autowrite (Burn) for $i ? y/n ? "
	    read autow
	    if [ "$autow" = "y" -o "$autow" = "yes" ]
	    then
		echo "${i}_AUTOWRITE=yes" >> $homedir/.vdrconvert/vdrconvert.env
	    fi
	done
	echo -n "Use only DVD+ commands to burn media ? y/n ? "
	read wplus
	if [ "$wplus" = "y" -o "$wplus" = "yes" ]
	then
	  echo "DVD_USE_ONLY_PLUS_CMDS=yes" >> $homedir/.vdrconvert/vdrconvert.env
	fi
    fi
fi
if [ "$vdrconvertuser" = "root" ]
then
cat << COMEOF > VDRConvert-commands.conf
~~~~~~~~~ || V D R C O N V E R T    : echo "no function"
Show VDRConvert Status              : $vdrconvertprefix/bin/status.sh
Disable vdrconvert                  : /etc/init.d/vdrconvert stop >/dev/null 2>&1
Enable vdrconvert                   : /etc/init.d/vdrconvert start >/dev/null 2>&1
Restore DVD Link ( ${device} )           : rm /dev/dvd;ln -sf /dev/${device} /dev/dvd >/dev/null 2>&1
COMEOF
else
cat << COMEOF > VDRConvert-commands.conf
~~~~~~~~~ || V D R C O N V E R T    : echo "no function"
Show VDRConvert Status              : $vdrconvertprefix/bin/status.sh
Disable vdrconvert                  : /etc/init.d/vdrconvert stop >/dev/null 2>&1
Enable vdrconvert                   : /etc/init.d/vdrconvert start >/dev/null 2>&1
Restore DVD Link ( ${device} )           : sudo rm /dev/dvd;sudo ln -sf /dev/${device} /dev/dvd >/dev/null 2>&1
COMEOF
fi

case $system in
	"RedHat")
cat << INITEOF > /etc/init.d/vdrconvert
#! /bin/bash
#
#set -x
# Author: Dimitrios Dimitrakos mail@dimitrios.de
# Changes for RH9 by mgansser@ngi.de 28.8.2003
#
# Tags for Red Hat init configuration tools
# Provides:          vdrconvert
# chkconfig: 35 90 90
# Description:       VDR Universalconverter 

# Check for missing binaries/scripts (stale symlinks should not happen)
VDRCONVERTDIR=$vdrconvertprefix
VDR2DVDSYNC=\$VDRCONVERTDIR/bin/vdrconvert.sh
test -x \$VDR2DVDSYNC || exit 5

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
#. /etc/rc.status

# Source function library.
. /etc/init.d/functions

prog=vdrconvert

# Reset status of this service
RETVAL=0

# Return values acc. to LSB for all commands but status:
# 0	  - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.
CONV_USER=$vdrconvertuser
LOGFILE=/dev/null
#LOGFILE=/var/log/vdrconvert/vdrconvert.log

case "\$1" in
    start)
	run=\`ps -aef|grep "vdrconvert.sh"|grep -v grep|wc -l\`
	if [ \$run -eq 0 ]
	then
		echo -n "Starting VDRCONVERT"
		su - \$CONV_USER -c "$vdrconvertprefix/bin/vdrconvert.sh" >> \$LOGFILE 2>&1 &
	else
		echo -n "VDRCONVERT already running"
	fi
	# Remember status and be verbose
	RETVAL=\$?
	;;
    stop)
	echo -n "Shutting down VDRCONVERT"
	ps -aef|grep vdrconvert|grep -v grep|awk '{print(\$2)}'|sort -n|xargs kill -n 9 >/dev/null 2>&1
	killall vdrconvert
	RETVAL=\$?
	;;
    *)
	echo "Usage: \$0 {start|stop}"
	exit 1
	;;
esac
exit \$RETVAL
fi
INITEOF
    chmod 755 /etc/init.d/vdrconvert
    ;;
	"gentoo")
cat << GENEOF > /etc/init.d/vdrconvert
#!/sbin/runscript
# Distributed under the terms of the GNU General Public License, v2 or later

# WARNING !!!
# Change VDRCONVERTDIR to your Path to "vdrconvert.sh" .

# Change the terminal where you want to run vdrconvert.
# Default (terminal 2) : &>/dev/tty2 &

# Make this script executable and copy it to /etc/init.d
# run as root  " rc-update add vdrconvert boot "
# change in your /etc/init.d/vdr  (VDR startscript)
# depend () { before * }
# to
# depend() {
#            need vdrconvert
#            before *
# }
# run as root " rc-update add vdr default "
#
# run " /etc/init.d/vdr start2" if haven't be started or
# " /etc/init.d/vdr fullrestart " to let the show begin
# (all w/o quotes) ;)


CONV_USER=$vdrconvertuser
LOGFILE=/dev/null
#LOGFILE=/var/log/vdrconvert/vdrconvert.log

depend() {
	    need localmount
}

checkconfig() {
	    if [ ! -e $vdrconvertprefix/bin/vdrconvert.sh ] ; then
		eerror "ERROR : Couldn't find vdrconvert.sh. Check your installation"
		return 1
	    fi
}

start() {
	    checkconfig || return 1
	    ebegin "Starting vdrconvert"
            su - \$CONV_USER -c "$vdrconvertprefix/bin/vdrconvert.sh" >> \$LOGFILE 2>&1 &

	    eend \$? "Couldn't start vdrconvert.sh"
}

stop() {
	    ebegin "Stopping vdrconvert"
		 killall -15 vdrconvert.sh
		 /etc/init.d/vdrconvert zap
	    eend \$? "Couldn't stop/kill vdrconvert.sh. Check or use 'zap' instead"
}
GENEOF
        chmod 755 /etc/init.d/vdrconvert
	;;
	"suse")
cat << SUSEOF > /etc/init.d/vdrconvert
#! /bin/bash
#
#set -x
# Author: Dimitrios Dimitrakos mail@dimitrios.de
#
#
### BEGIN INIT INFO
# Provides:          vdrconvert
# Required-Start:    nfs
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: VDR Universalconverter 
# Description:       
### END INIT INFO

# Check for missing binaries/scripts (stale symlinks should not happen)
VDRCONVERTDIR=$vdrconvertprefix
VDR2DVDSYNC=\$VDRCONVERTDIR/bin/vdrconvert.sh

test -x \$VDR2DVDSYNC || exit 5

# Source LSB init functions
# providing start_daemon, killproc, pidofproc, 
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
. /etc/rc.status

# Reset status of this service
rc_reset

# Return values acc. to LSB for all commands but status:
# 0	  - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.
CONV_USER=$vdrconvertuser
LOGFILE=/dev/null
#LOGFILE=/var/log/vdrconvert/vdrconvert.log

case "\$1" in
    start)
	run=\`ps -aef|grep "vdrconvert.sh"|grep -v grep|wc -l\`
	if [ \$run -eq 0 ]
	then
	    echo -n "Starting VDRCONVERT "
	    su - \$CONV_USER -c "nice -n 15 $vdrconvertprefix/bin/vdrconvert.sh" >> \$LOGFILE 2>&1 &
	else
	    echo -n "VDRCONVERT already running"
	    rc_failed
	fi
	# Remember status and be verbose
	rc_status -v
	;;
    stop)
	echo -n "Shutting down VDRCONVERT"
	/sbin/killproc -9 vdrconvert.sh > /dev/null 2>&1
	rc_status -v
	;;
    *)
	echo "Usage: \$0 {start|stop}"
	exit 1
	;;
esac
rc_exit
SUSEOF
    chmod 755 /etc/init.d/vdrconvert
    ;;
	"debian")
cat << DEBEOF > /etc/init.d/vdrconvert
#! /bin/bash
#
#set -x
# Author: Dimitrios Dimitrakos mail@dimitrios.de
#
#
### BEGIN INIT INFO
# Provides:          vdrconvert
# Required-Start:    
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: VDR Universalconverter 
# Description:       
### END INIT INFO

# Check for missing binaries/scripts (stale symlinks should not happen)
VDRCONVERTDIR=$vdrconvertprefix
VDR2DVDSYNC=\$VDRCONVERTDIR/bin/vdrconvert.sh
test -x \$VDR2DVDSYNC || exit 5

# Source LSB init functions
# providing start_daemon, killproc, pidofproc, 
# log_success_msg, log_failure_msg and log_warning_msg.
# This is currently not used by UnitedLinux based distributions and
# not needed for init scripts for UnitedLinux only. If it is used,
# the functions from rc.status should not be sourced or used.
#. /lib/lsb/init-functions

# Shell functions sourced from /etc/rc.status:
#      rc_check         check and set local and overall rc status
#      rc_status        check and set local and overall rc status
#      rc_status -v     ditto but be verbose in local rc status
#      rc_status -v -r  ditto and clear the local rc status
#      rc_status -s     display "skipped" and exit with status 3
#      rc_status -u     display "unused" and exit with status 3
#      rc_failed        set local and overall rc status to failed
#      rc_failed <num>  set local and overall rc status to <num>
#      rc_reset         clear local rc status (overall remains)
#      rc_exit          exit appropriate to overall rc status
#      rc_active	checks whether a service is activated by symlinks
#      rc_splash arg    sets the boot splash screen to arg (if active)
#. /etc/rc.status

# Reset status of this service
# rc_reset

# Return values acc. to LSB for all commands but status:
# 0	  - success
# 1       - generic or unspecified error
# 2       - invalid or excess argument(s)
# 3       - unimplemented feature (e.g. "reload")
# 4       - user had insufficient privileges
# 5       - program is not installed
# 6       - program is not configured
# 7       - program is not running
# 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
# 
# Note that starting an already running service, stopping
# or restarting a not-running service as well as the restart
# with force-reload (in case signaling is not supported) are
# considered a success.

CONV_USER=$vdrconvertuser
LOGFILE=/dev/null
#LOGFILE=/var/log/vdrconvert/vdrconvert.log

case "\$1" in
    start)
	run=\`ps -aef|grep "vdrconvert.sh"|grep -v grep|wc -l\`	
	if [ \$run -eq 0 ]
	then
	    echo -n "Starting VDRCONVERT "
	    su - \$CONV_USER -c "nice -n 15 $vdrconvertprefix/bin/vdrconvert.sh" >> \$LOGFILE 2>&1 &
	else
	    echo -n "VDRCONVERT already running"
	fi
	;;
    stop)
	echo -n "Shutting down VDRCONVERT"
        ps -aef|grep vdrconvert|grep -v grep|awk '{print(\$2)}'|sort -n|xargs kill -9 >/dev/null 2>&1
	# rc_status -v
	;;
    *)
	echo "Usage: \$0 {start|stop}"
	exit 1
	;;
esac
# rc_exit
DEBEOF
    chmod 755 /etc/init.d/vdrconvert
    ;;
	"sl")
cat << SLEOF > /etc/rc.d/rc.vdrconvert
#! /bin/bash
#
#set -x
# Author: Dimitrios Dimitrakos mail@dimitrios.de
#
#
### BEGIN INIT INFO
# Provides:          vdrconvert
# Required-Start:    
# Default-Start:     3 5
# Default-Stop:      0 1 2 6
# Short-Description: VDR Universalconverter
# Description:       
### END INIT INFO

# Check for missing binaries/scripts (stale symlinks should not happen)
VDRCONVERTDIR=$vdrconvertprefix
VDR2DVDSYNC=\$VDRCONVERTDIR/bin/vdrconvert.sh
test -x \$VDR2DVDSYNC || exit 5
CONV_USER=$vdrconvertuser
LOGFILE=/dev/null
#LOGFILE=/var/log/vdrconvert/vdrconvert.log

case "\$1" in
    start)
	run=\`ps -aef|grep "vdrconvert.sh"|grep -v grep|wc -l\`	
	if [ \$run -eq 0 ]
	then
	    echo -n "Starting VDRCONVERT "
	    su - \$CONV_USER -c "nice -n 15 $vdrconvertprefix/bin/vdrconvert.sh" >>\$LOGFILE 2>&1 &
	else
	    echo -n "VDRCONVERT already running"
	fi
	;;
    stop)
	echo -n "Shutting down VDRCONVERT"
	killall -9 vdrconvert.sh >/dev/null 2>&1
	;;
    *)
	echo "Usage: \$0 {start|stop}"
	exit 1
	;;
esac
SLEOF
    chmod 755 /etc/rc.d/rc.vdrconvert
    ;;
  *)
    ;;
esac
echo -n "Start vdrconvert service at boot time y/n ? "
read vdrstartup
echo -n "Start vdrconvert service now y/n ? "
read vdrstartupnow

if [ "$vdrstartup" = "y" -o "$vdrstartup" = "yes" ]
then
    echo "/etc/init.d/vdrconvert stop" >> deinstall.sh
    case $system in
      suse)
	echo "insserv -r vdrconvert" >> deinstall.sh
	echo "echo \"Deinstall service /etc/init.d/vdrconvert\"" >> deinstall.sh
	echo "rm /etc/init.d/vdrconvert" >> deinstall.sh
    	insserv -d vdrconvert
	;;
      gentoo)
	echo "rc-update del vdrconvert" >> deinstall.sh
	echo "echo \"Deinstall service /etc/init.d/vdrconvert\"" >> deinstall.sh
	echo "rm /etc/init.d/vdrconvert" >> deinstall.sh
    	rc-update add vdrconvert default
	;;
      RedHat)
	echo "chkconfig --del vdrconvert" >> deinstall.sh
	echo "echo \"Deinstall service /etc/init.d/vdrconvert\"" >> deinstall.sh
	chkconfig --add vdrconvert
	;;
      debian)
	echo "update-rc.d vdrconvert stop" >> deinstall.sh
	echo "echo \"Deinstall service /etc/init.d/vdrconvert\"" >> deinstall.sh
	update-rc.d vdrconvert defaults 20
	;;
      sl)
	cp /etc/rc.d/rc.local /tmp/rc.local.$$ >/dev/null 2>&1
	status=$?
	if [ $status -eq 0 ]
	then
cat << RCLOCALEOF >> /tmp/rc.local.$$

if [ -x /etc/rc.d/rc.vdrconvert ]; then
        echo "Start vdrconvert"
        /etc/rc.d/rc.vdrconvert start
fi # Don't change this comment vdrconvert
RCLOCALEOF
	    cp /tmp/rc.local.$$ /etc/rc.d/rc.local
	    chmod +x /etc/rc.d/rc.local
	    echo "echo \"Deinstall service /etc/rc.d/rc.vdrconvert\"" >> deinstall.sh
	    echo "[ -f /etc/rc.d/rc.vdrconvert ] && rm /etc/rc.d/rc.vdrconvert" >> deinstall.sh
cat << RCDLOCALEOF >> deinstall.sh
cat /etc/rc.d/rc.local|grep -v vdrconvert > /tmp/rc.local.$$
status=$?
if [ $status -eq 0 ]
then
	cp /tmp/rc.local.$$ /etc/rc.d/rc.local
	chmod +x /etc/rc.d/rc.local
fi
RCDLOCALEOF
	    
	fi
	rm -f /tmp/rc.local.$$ > /dev/null 2>&1
	;;
      *)
	;;
    esac
fi

if [ "$vdrstartupnow" = "y" -o "$vdrstartupnow" = "yes" ]
then
    case $system in
      suse)
    	/etc/init.d/vdrconvert start
	;;
      gentoo)
    	/etc/init.d/vdrconvert start
	;;
      RedHat)
    	/etc/init.d/vdrconvert start
	;;
      debian)
    	/etc/init.d/vdrconvert start
	;;
      sl)
    	/etc/rc.d/rc.vdrconvert start
	;;
      *)
	;;
    esac
fi
if [ -d /etc/logrotate.d ]
then
	echo ""
	echo "Install logrotate configuration for vdrconvert"
	cp etc/logrotate.d/vdrconvert /etc/logrotate.d/vdrconvert
	echo "echo \"Deinstall logrotate file\"" >> deinstall.sh
	echo "[ -f /etc/logrotate.d/vdrconvert ] && rm -f /etc/logrotate.d/vdrconvert" >> deinstall.sh
else
	echo ""
	echo "Please create a mechanism to rotate the logs after installation ( logrotate )"
	echo "Example in subdir etc"
fi
chmod +x deinstall.sh
