#!/bin/bash

#########################################################################
## Ubuntu Studio Launcher ##						#
############################						#
# SCRIPT:  ubuntustudio							#
# VERSION: 2.0.0							#
# AUTHOR:  Dana Olson <dana@ubuntustudio.com>				#
# HOME:    http://www.ubuntustudio.com/wiki/index.php/Studio_Launcher	#
# DATE:    11feb06							#
# LICENSE: GNU GPL version 2						#
# FEATURES:								#
#  - Automated music studio applications detection			#
#  - Uses set_rlimits if installed (PAM recommended instead)		#
#  - Remembers previously launched application choices			#
#  - Launches QjackCtl first if it was selected				#
#  - Checks jackd started if using QjackCtl, if not, aborts script	#
#  - Pass --all to bypass blacklist					#
#  - Launch delays for CPU usage					#
#  - User-configurable delays in ~/.ubuntustudio2config			#
#  - Blacklist for non-studio/JACK audio apps to clean up the menu	#
#  - User-configurable blacklist in ~/.ubuntustudio2config		#
#  - User-configurable window size in ~/.ubuntustudio2config		#
# NOTES:								#
#  - Please don't fork this script for the exact same purpose. I am	#
#    more than willing to listen to suggestions, feature requests, etc.	#
# THANKS:								#
#  - Crimsun, for the IFS tips						#
#  - Everyone who ever worked on Linux & apps (specifically audio apps)	#
#########################################################################

function us_jackcheck () {
	ps aux | grep jackd | grep -v grep
}

function us_blacklistcheck () {
	echo "`which ubuntustudio` $BLACKLIST" | grep -i "`grep "^Exec=" $1 | awk -F= '{ print $2 }' | awk -F" " '{ print $1 }'`"
}

APPTITLE="Ubuntu Studio Launcher"
APPVERSION="2.0.0"

CONFIGPATH="$HOME/.ubuntustudio2config"
DESKTOPPATH="/usr/share/applications"

SETRLIMITS=`which set_rlimits`

touch $CONFIGPATH

APPWINW=`grep -i "width:" $CONFIGPATH | awk '{ print $2 }'`
if [ ! "$APPWINW" ] ; then
	APPWINW=650
fi
APPWINH=`grep -i "height:" $CONFIGPATH | awk '{ print $2 }'`
if [ ! "$APPWINH" ] ; then
	APPWINH=650
fi
JACKDELAY=`grep -i "jackdelay:" $CONFIGPATH | awk '{ print $2 }'`
if [ ! "$JACKDELAY" ] ; then
	JACKDELAY=5
fi
APPDELAY=`grep -i "appdelay:" $CONFIGPATH | awk '{ print $2 }'`
if [ ! "$APPDELAY" ] ; then
	APPDELAY=3
fi
if [ "$1" != "--all" ] ; then
	BLACKLISTAPPS=`grep -i "blacklist:" $CONFIGPATH | awk -F: '{ print $2 }'`
	if [ ! "$BLACKLISTAPPS" ] ; then
		BLACKLISTAPPS="gnome-cd gnome-sound-recorder rhythmbox serpentine sound-juicer totem vumeter"
	fi
	for i in $BLACKLISTAPPS ; do
		BLACKLIST="$BLACKLIST `which $i`"
	done
fi
LAUNCHLIST=`grep -i "launchlist:" $CONFIGPATH | awk -F: '{ print $2 }'`

APPLIST=""

for i in `grep -i "^Categories=.*Audio" $DESKTOPPATH/*.desktop | awk -F: '{ print $1 }' | uniq` ; do
	if ! us_blacklistcheck $i ; then
		if echo "$LAUNCHLIST" | grep -i "`grep "^Exec=" $i | awk -F= '{ print $2 }'`"
		then
			APPLIST="${APPLIST}TRUE|"
		else
			APPLIST="${APPLIST}FALSE|"
		fi
		APPLIST="${APPLIST}`grep "^Name=" $i | awk -F= '{ print $2 }'`|"
		APPLIST="${APPLIST}`grep "^Comment=" $i | awk -F= '{ print $2 }'`|"
		APPLIST="${APPLIST}`which \`grep "^Exec=" $i | awk -F= '{ print $2 }'\``|"
	else
		echo $i IS BLACKLISTED
	fi
done

if [ ! "$APPLIST" ] ; then
	zenity --info --title "$APPTITLE $APPVERSION" --text "There were no music studio applications found installed on your system. Install some and then re-run $APPTITLE."
	exit 1
fi

IFS="|"
LAUNCHLIST=`zenity --list --title "$APPTITLE $APPVERSION" --checklist --width=$APPWINW --height=$APPWINH --print-column=3 --separator=" " \
	 --column="Run" --column="Name" --column="Description" --column="Path" $APPLIST`
IFS=" "

echo $LAUNCHLIST

if [ ! "$LAUNCHLIST" ] ; then
	exit 1
fi

echo \# SETTINGS UPDATED ON `date` \# > $CONFIGPATH
echo "LaunchList: $LAUNCHLIST" >> $CONFIGPATH
echo "BlackList: $BLACKLISTAPPS" >> $CONFIGPATH
echo "Width: $APPWINW" >> $CONFIGPATH
echo "Height: $APPWINH" >> $CONFIGPATH
echo "JACKDelay: $JACKDELAY" >> $CONFIGPATH
echo "AppDelay: $APPDELAY" >> $CONFIGPATH

if echo "$LAUNCHLIST" | grep -i "`which qjackctl`" ; then
	$SETRLIMITS `which qjackctl` &
	sleep $JACKDELAY
	if ! us_jackcheck ; then
		sleep $JACKDELAY
		if ! us_jackcheck ; then
			zenity --info --title "$APPTITLE $APPVERSION" --text "The JACK server does not appear to be running. Double-check your settings."
			exit 1	
		fi
	fi
	LAUNCHLIST=`echo $LAUNCHLIST | awk -F"/usr/bin/qjackctl" '{ print $1 $2 }'`
fi

for i in $LAUNCHLIST ; do
	$SETRLIMITS `which $i` &
	sleep $APPDELAY
done

exit 0
