#!/bin/sh

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

xmsg=$(which pydialog 2>/dev/null || which kdialog 2>/dev/null)

case $LANG in 
	hu*)
	    download="Repülőgép-sziluettek és logók letöltése a VirtualRadar-hoz. <br>Amikor végzett a VirtualRadar automatikusan elindul.<br><br>Indítsuk most?"
	    title="Beépülőmodul letöltő"
	    yeslabel="Igen"
	    nolabel="Nem"
	    progress="<h2>VirtualRadar</h2>Repülőgép-sziluettek és logók letöltése.."
	    wrapper="$title"
	    platformhead="<h2>blackPanther OS 64bit</h2>"
	    platformsg="A VRS jelenleg nem fut 64 bites rendszeren. Ha ez változik, frissítést fogsz kapni."
	;;
	en*)
	    download="Airplane siluettes and logos download for VirtualRadar. <br>When finishehd the VirtualRadar start by automatically.<br><br>Start now?"
	    title="Plugin downloader"
	    yeslabel="Yes"
	    nolabel="No"
	    progress="<h2>VirtualRadar</h2>Airplane siluettes and logos download.."
	    wrapper="$title"
	    platformhead="<h2>blackPanther OS 64bit</h2>"
	    platformsg="The VRS currently does not run on 64 bit systems. If this is changed, you will get update."
	;;
esac

showmsg() {
if [ -n "$DISPLAY" ];then
    if [ -x /usr/bin/bubbled ];then 
	bubblemsg other "$1 $2"
     elif [ -x /usr/bin/notify-send ];then
	notify-send -t 12000 -u normal -i natpos "$1" "$2"
    fi
 else
 	$ECHO $E "$1" "$2"
	$ECHO ""
fi
}

run() {
    dump1090 --quiet --net --net-ro-size 500 --net-ro-rate 5 --net-buffer 5 &
    sleep 2
}

stop() {
    #[ "$(uname -p 2>/dev/null)" = "x86_64" ] && $xmsg --title "Sorry" --error "$platformhead$platformsg"
  for PID in $(pidof dump1090) ;do
    showmsg "Dump1090" "Stopping backend..."
    kill $PID 2>/dev/null
    done

}

prepar() {
    showmsg "VirtaulRadar" "Server is preparing..."
    list="
de-DE
fr-FR
pt-BR
ru-RU
zh-CN
AjaxMin.dll
Newtonsoft.Json.dll
VirtualRadar.Database.dll
HtmlAgilityPack.dll
InterfaceFactory.dll
Interop.NATUPNPLib.dll
KdTreeLib.dll
Microsoft.FlightSimulator.SimConnect.dll
VirtualRadar.exe
VirtualRadar.exe.config
VirtualRadar.Headless.dll
VirtualRadar.Interface.dll
VirtualRadar.Interop.dll
VirtualRadar.Library.dll
VirtualRadar.Localisation.dll
VirtualRadar.Resources.dll
VirtualRadar.SQLiteWrapper.dll
VirtualRadar.WebServer.dll
VirtualRadar.WebSite.dll
VirtualRadar.WinForms.dll
virtualradar
Plugins/BaseStationDatabaseWriter
Plugins/CustomContent
Plugins/DatabaseEditor
Plugins/VRS-Operator-Flags
Plugins/WebAdmin
"

mkdir -p $HOME/.virtualradar/Plugins

INSTPATH=$(readlink -f /usr/bin/virtualradar | sed 's|/virtualradar$||')

if [ ! -f "$INSTPATH/VirtualRadar.exe" ];then
     showmsg "AutoPath" "Find installed application..."
elif [ -f "/usr/lib64/VirtualRadar/VirtualRadar.exe" ];then
    INSTPATH="/usr/lib64/VirtualRadar"
else
     showmsg "ERROR" "INSTALLL FAILED :("
     exit
fi

cp -rf $INSTPATH/Web $HOME/.virtualradar
cp -rf $INSTPATH/.local $HOME/.virtualradar
cp -rf $INSTPATH/.mono $HOME/.virtualradar
cp -f $INSTPATH/Checksums.txt $HOME/.virtualradar

for LINK in $list ; do 
    
 # "$INSTPATH/$LINK --- > $HOME/.virtualradar/$LINK"
 ln -s "$INSTPATH/$LINK"  "$HOME/.virtualradar/$LINK"

done

if [ -f "$HOME/.virtualradar/.local/share/VirtualRadar/Configuration.xml" ];then
    sed -i "s|XXVRSXX|$HOME/.virtualradar|" $HOME/.virtualradar/.local/share/VirtualRadar/Configuration.xml
 else
     showmsg "ERROR" "INSTALLL FAILED :("
     exit
fi

}

down() {
if [ -x "$xmsg" ];then
$xmsg --title "$title" --yesno "$download" --yes-label "$yeslabel" --no-label "$nolabel"
ret=$?
 if [ "$ret" = "1" ];then
    continue
 else
     ref=`$xmsg --progressbar "$progress" $count --title "$wrapper"`
     echo "$xmsg --progressbar \"$progress\" $count --title \"$wrapper\""
     echo "$ref"
    sleep 3
    qdbus $ref Set org.kde.kdialog.ProgressDialog value 5;

    showmsg "VirtaulRadar" "Server is preparing..."
    qdbus $ref Set org.kde.kdialog.ProgressDialog value 10;
    if [ ! -d $HOME/.virtualradar ];then
	mkdir -p $HOME/.virtualradar
    fi
    cd $HOME/.virtualradar
    
    qdbus $ref Set org.kde.kdialog.ProgressDialog value 15;

    [ -d sbs-r-logos ]&& rm -rf sbs-r-logos
    qdbus $ref Set org.kde.kdialog.ProgressDialog value 20;

    wget -c http://www.kinetic.co.uk/repo/sbs-r-logos.zip

    qdbus $ref Set org.kde.kdialog.ProgressDialog value 30;
    unzip sbs-r-logos.zip

    qdbus $ref Set org.kde.kdialog.ProgressDialog value 40;
    [ -d SilhouettesLogos ]&& rm -rf SilhouettesLogos

    qdbus $ref Set org.kde.kdialog.ProgressDialog value 50;
    wget -c http://www.kinetic.co.uk/repo/SilhouettesLogos.zip

    qdbus $ref Set org.kde.kdialog.ProgressDialog value 60;
    unzip SilhouettesLogos.zip

    qdbus $ref Set org.kde.kdialog.ProgressDialog value 70;
    sleep 4
    rm -f sbs-r-logos.zip
    qdbus $ref Set org.kde.kdialog.ProgressDialog value 80;

    rm -f SilhouettesLogos.zip
    qdbus $ref Set org.kde.kdialog.ProgressDialog value 99;
    qdbus $ref org.kde.kdialog.ProgressDialog.close

 fi
fi

}

own() {
if [ -x "$xmsg" ];then
$xmsg --title "$title" --yesno "$akcept" --yes-label "$yeslabel" --no-label "$nolabel"
ret=$?
[ "$ret" = "1" ]&&exit
$xmsg --title "$title" --yesno "$welcome" --yes-label "$yeslabel" --no-label "$nolabel"
ret=$?
else
if [ -x "$(which zenity)" ] ;then
zenity --error "$error - SYSTEM ERROR"
else 
notify-send -i cancel "ERROR" "$error - System Error"
exit 1
 fi
fi

}


if [ "--help" = "$1" ];then
    echo
    echo " --------------VIRTUAL RADAR SERVER HELP ---------------"
    echo
    echo " $(basename $0) --help  -->  this screen"
    echo
    echo " $(basename $0) --pluginst  -->  siluettes and logos install"
    echo
    echo " $(basename $0) --nogui  -->  running in background without gui"
    echo
    exit
fi

if [ "--pluginst" = "$1" ];then
    if [ -f "$HOME/.virtualradar/VirtualRadar.exe" ];then
	down
	exit
    else
	showmsg "Please install first!" "Installer preparing..."
	sleep 2
	prepar
	down
    fi
fi

if [ ! -d $HOME/.virtualradar ];then
    prepar
    sleep 2
    down
elif [ ! -f $HOME/.virtualradar/VirtualRadar.exe ];then
    prepar
    sleep 2
    down
fi

if [ ! -n "$(pidof dump1090 2>/dev/null)" ];then
    run
    [ ! -n "$(pidof dump1090 2>/dev/null)" ] && showmsg "VirtualRadar Daemon" "Can't started! Check dump1090 error messages." && exit
else
    showmsg "VirtualRadar Daemon" "Already running..."
fi


if [ "--nogui" = "$1" ];then
    echo "Default admin/passwd is: admin/admin"
    #ono $HOME/.virtualradar/VirtualRadar.exe -nogui -createAdmin:name -password:password
    HOME=$HOME/.virtualradar mono $HOME/.virtualradar/VirtualRadar.exe -nogui
    default-browser http://127.0.0.1:8080/VirtualRadar
    stop
    exit
fi


if [ -f "./VirtualRadar.exe" ];then
    HOME=. mono VirtualRadar.exe
else
    HOME=$HOME/.virtualradar mono $HOME/.virtualradar/VirtualRadar.exe
    
fi

stop

