#!/bin/sh
#*********************************************************************************************************
#*   __     __               __     ______                __   __                      _______ _______   *
#*  |  |--.|  |.---.-..----.|  |--.|   __ \.---.-..-----.|  |_|  |--..-----..----.    |       |     __|  *
#*  |  _  ||  ||  _  ||  __||    < |    __/|  _  ||     ||   _|     ||  -__||   _|    |   -   |__     |  *
#*  |_____||__||___._||____||__|__||___|   |___._||__|__||____|__|__||_____||__|      |_______|_______|  *
#* http://www.blackpantheros.eu | http://www.blackpanther.hu - kbarcza[]blackpanther.hu * Charles Barcza *
#*************************************************************************************(c)2002-2011********

#---------------------------------------------------------------
# Project         : blackPanther OS default-apps
# Module          : bin
# File            : default-aplayer (audioplayer)
# Version         : 0.1
# Author          : Charles Barcza
# Created On      : Mon Aug 29 2008
# Purpose         : launch a audio player
#---------------------------------------------------------------
 . /etc/blackPanther-default-apps.conf
 
appname=aplayer

if [ -f ~/.personal-$appname ];then
    . ~/.personal-$appname
    echo "Presonal conf found!"
fi

set_a_aplayer() {
echo "APLAYER=$1" > ~/.personal-$appname
echo "Personal $appname definied, please restart `basename $0` without parameter."
exit
}

strip_aplayer_var() {
    if [[ -n "$APLAYER" ]]; then
	if [[ `basename "$APLAYER"` == "default-${appname}" ]]; then
	    unset APLAYER
	else
	    set $APLAYER
	    if ! which $1 > /dev/null 2>&1; then
		unset APLAYER
	    fi
	fi
    fi
}

if [ "$1" = "-s" ];then
    echo "Define user ${appname}.."
    set_a_aplayer $2
elif [ "$1" = "-u" ];then
    rm -f $HOME/.personal-${appname}
    echo "Clear definied personal ${appname} conf.."
    exit
elif [ "$1" = "-h" ];then
    echo ""
    echo -e "${GRN}This is a$BLK blackPanther OS$GRN specific tool"
    echo ""
    echo -e "$MAG  Usage:"
    echo -e "$YEL       `basename $0` -s $DEF Set a personal ${appname} (You conf file: $HOME/.personal-${appname})"
    echo -e "$YEL       `basename $0` -u $DEF Unset definied ${appname} (Remove you conf file: $HOME/.personal-${appname})"
    echo -e "$YEL       `basename $0` -h $DEF This screen"
    echo ""
    echo -e "$CYN  [$RED Example:$CYN `basename $0` -s audacious$RED OR to autodetect:$CYN `basename $0`]$DEF"
    echo ""
    exit
fi

strip_aplayer_var
if [[ -z "$APLAYER" ]]; then
 
    #strip_aplayer_var
    
    [[ -z "$APLAYER" ]] && APLAYER=`which qmmp 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which audacious 2> /dev/null` 
    [[ -z "$APLAYER" ]] && APLAYER=`which exaile 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which amarok 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which bmp 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which beep-media-player 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which xine 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which xmms 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which smplayer 2> /dev/null`
    [[ -z "$APLAYER" ]] && APLAYER=`which mplayer 2> /dev/null`
fi


if [[ -n "$APLAYER" ]]; then 
    exec $APLAYER "$@"
else
    xmsg=`which kdialog 2>/dev/null || which zenity 2>/dev/null`
    [ -n "$DISPLAY" ] && $xmsg --error "Sorry. Audio Player applications not detected, please install on PC (etc: installing amarok)"
    [ ! -n "$DISPLAY" ] && echo -n "Sorry. Audio Player not detected! Please install on PC (etc: installing amarok)"
fi
