#!/bin/bash
# mmInstaller
# for x64
# c2007-2016 by Paul Sherman   psherman2001@gmail.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
############## put working path here ####################################
pth="/usr/src/restricted4_x64/"
##############################################################
# patent disclaimer ################################################
Xdialog --title "DISCLAIMER" \
        --yesno "The multimedia files are patent-encumbered in some countries,\nwhich is why they are not \
	installed on Absolute by default.\nIt is your responsibility to know your own country's legislation.\n \
	If your country's laws prohibit you from using this software\nwe ask you not to install\
	these files.\n\nDo you wish to continue?\n\n[If yes, please close all other open programs]\n\n" 0 0

if [ $? == 1 ]; then
	exit
fi
#
########### check if any of the files are installed before we start #################
########### remove them if they are there and user wants to re-compile tham... #######
inFlag=0
for f in "libdvdcss-*" "ffmpeg-*" "mplayer-codecs*"; do 
    z=`find /var/log/packages -name "$f"`
    if [ ! "$z" == "" ]; then
		if [[ $z =~ "free" ]]; then
			continue
		fi
		inFlag=1
   fi
done

if [ $inFlag -eq 1 ]; then

	Xdialog --title "Already Installed" \
			--yesno "It seems that one or more of the Multimedia Packages 
			has already been installed.\n
			If you wish to recompile and reinstall them, 
			then choose YES.\n\n" 0 0

	if [ ! $? == 0 ]; then
		exit 0
	fi
fi
##############################################################
####  START DOWNLOAD of installer package ###################################
##############################################################
Xdialog --title "Multimedia Installer" --no-buttons --infobox "Now checking if installer can be downloaded... \nhang on a couple minutes.\n" 0 0 2000
# download the "restricted" folder
dl=ftp://ftp.absolutelinux.org/Extra/restricted4_x64.tar.bz2
cd /usr/src
rm -rf restricted.tar.bz2 >/dev/null 2>&1
rm -rf restricted2.tar.bz2 >/dev/null 2>&1
rm -rf restricted3.tar.bz2 >/dev/null 2>&1
rm -rf restricted4.tar.bz2 >/dev/null 2>&1
if [ ! -e restricted4_x64.tar.bz2 ]; then
	xterm -e wget -c $dl
fi
sleep 1
if [ ! -e restricted4_x64.tar.bz2 ]; then
	Xdialog --title "ERROR" --msgbox "Could not download the multimedia installer at this time.\n\nIf you have an internet connection,\ntry again in a few minutes.\n\n" 0 0
	exit 1
fi
# clear out old restricted files if present
rm -rf /usr/src/restricted >/dev/null 2>&1
rm -rf /usr/src/restricted2 >/dev/null 2>&1
rm -rf /usr/src/restricted3 >/dev/null 2>&1
rm -rf /usr/src/restricted4 >/dev/null 2>&1
rm -rf /usr/src/restricted4_x64 >/dev/null 2>&1
# unpack the new download
tar -xjvf restricted4_x64.tar.bz2 >/dev/null 2>&1 || exit 1
#
if [ ! -d $pth ]; then
	Xdialog --title "ERROR" --msgbox "The installation files in $pth\nseem to be missing.\n\n" 0 0
	exit
fi

###############################################################
# DOWNLOADED and UNPACKED
##########################################################
##########################################################
######################      FUNCTIONS       #########################
##########################################################
############## deletes any expanded/downloaded files in any folders  ############
delpacks(){
	cd $pth
	for file in *
	do
        	if [ -d "$file" ]; then
                	rm -r "$file" 2>/dev/null
                	continue
        	fi
	done
}
delTXZs(){
	cd $pth
	for i in $( ls *.txz );
	do
		if [[  $i != *mplayer* ]]; then
			rm -f "$i" 2>/dev/null
		fi		
	done
}
delPackages(){
	for f in "libdvdcss-*" "ffmpeg-*"; do
    	z=`find /var/log/packages -name "$f" -exec basename {} \;`
    	if [ ! "$z" == "" ]; then
        	removepkg "$z" 2>/dev/null
	    fi
	done
}
checkDialog(){
	t=`ps -e | grep Xdialog`
	if [ "$t" == "" ]; then
		Xdialog --title "INSTALL STOP" --infobox "The installation has been interrupted.\n\nCleaning up..." 0 0 5000
		delpacks
		delTXZs
		exit 0
	fi
}

#########################################################################
buildpacks(){
	Xdialog --title "BUILDING PACKAGES" --ok-label "Cancel Install"  --msgbox "Please wait, \nthe files needed to run the installer are being compiled.\n\nThis can take QUITE some time...\n(nearly an hour on a typical system)\n\n" 0 0 &
	ZPID=$!
	for pfolder in "ffmpeg" "libdvdcss"
	do
		cd $pth
		if [ ! -d "$pfolder" ];then
    			Xdialog --title "INSTALL HALTED" --infobox "Installation has been stopped.\nInstall files for $pfolder not found.\nCleaning up..." 0 0 5000
        		delpacks
			delTXZs
			exit 0	
		fi
		cd $pth$pfolder
		sh SlackBuild
		if [ $? -eq 1 ]; then
			if (( "$pfolder" == "libdvdcss" )); then

				Xdialog --title "CONTINE WITHOUT $pfolder?" \
        				--yesno "$pfolder requires a live internet connection to download and install.\n \
						There has been an error building $pfolder, which may be due to a lack \n \
						of connectivity. This may effect playback of DVDs and/or other video \n \
						files (but not MP3s.)\n\nWould you like to continue installing the media \n \
						files without this? Answer NO if you would like to try again later.\n\n" 0 0

			if [ $? == 0 ]; then
				continue
			else
				delpacks
				delTXZs
				exit 0
			fi
			fi
			Xdialog --title "INSTALL HALTED" --infobox "Installation has been stopped.\nPackage creation for $pfolder failed.\nCleaning up..." 0 0 5000
			delpacks
			delTXZs
			exit 0
		fi
		sleep 1
		checkDialog
		sleep 1
	done	
	kill $ZPID >/dev/null 2>&1
}	
##########################################################
###################    installation begins   ##########################
##########################################################
######  Clear the deck before we start...
#delTXZs
delpacks

cd $pth
######## UNPACK INSTALLERS ################
Xdialog --title "PREPARING FOR INSTALL" --ok-label "Cancel Install"  --msgbox "Please wait, \nthe files needed to run the installer are being unpacked..." 0 0 &
YPID=$!
for f in $(find $PWD -name '*.gz')
do 
    tar zxf $f
    sleep 1
    checkDialog
    sleep 1
done
kill $YPID >/dev/null 2>&1
############ BEGIN INSTALLING PACKAGES ########################
buildpacks
Xdialog --title "READY TO INSTALL" --no-buttons --infobox "\n       Software compilation is complete.      \nThe packages will now be installed\n" 0 0 4000

# packages built successfully, so let's clear out any old packages
delPackages
sleep 2

cd $pth
for i in $( ls *.txz );
do
	if [[ $i =~ "mplayer" ]]; then
	    continue
	fi
	Xdialog --title "INSTALLING" --no-buttons --infobox "\n        Now installing the package:         \n$i\n\n" 0 0 &
        ZPID=$!
	installpkg $i >/dev/null 2>&1
	sleep 2
	kill $ZPID >/dev/null 2>&1
done
################################################################################################
# now install the MPlayer version with support for new packages, deleting the old one first...
cd /var/log/packages
ret=`find /var/log/packages -name "mplayer*"`
if [ "$ret" ]; then
	removepkg "$ret" >/dev/null 2>&1
fi
# delete twice in case someone/somehow got another mplayer installed
ret=`find /var/log/packages -name "mplayer-1*"`
if [ "$ret" ]; then
	removepkg "$ret" >/dev/null 2>&1
fi
z=`find $pth -name "mplayer-*txz"`
if [ ! "$z" == "" ]; then
	Xdialog --title "Update" --no-buttons --infobox "\n Installing updated MPlayer\n\n$z\n" 0 0 2000
	installpkg $z  >/dev/null 2>&1
fi
#######################################################################################################
# now install the audacious-plugin version with support for new packages, deleting the old one first...
cd /var/log/packages
ret=`find /var/log/packages -name "audacious-plugins*"`
if [ "$ret" ]; then
	removepkg "$ret" >/dev/null 2>&1
fi
z=`find $pth -name "audacious-plugins-*txz"`
if [ ! "$z" == "" ]; then
	Xdialog --title "Update" --no-buttons --infobox "\n Installing updated Audacious Plugins\n\n$z\n" 0 0 2000
	installpkg $z  >/dev/null 2>&1
fi
# now install the updated audacity package deleting the old one first...
cd /var/log/packages
ret=`find /var/log/packages -name "audacity*"`
if [ "$ret" ]; then
	removepkg "$ret" >/dev/null 2>&1
fi
z=`find $pth -name "audacity-*txz"`
if [ ! "$z" == "" ]; then
	Xdialog --title "Update" --no-buttons --infobox "\n Installing updated Audacity package\n\n$z\n" 0 0 2000
	installpkg $z  >/dev/null 2>&1
fi
# nothing left but cleanup and notification
Xdialog --title "CLEANUP" --no-buttons --infobox "All the packages have been installed. \nCleaning up build directories and temporary files.\n" 0 0 3000
delpacks
Xdialog --title "COMPLETE" --msgbox "Installation of multimedia packages is complete.\n\nThe built packages have been left in /usr/src/restricted\nfor future use or reference.\n\n" 0 0
