#!/usr/bin/env bash
set -x

filename=$0
if [ "`echo $0 | grep "bin"`" != "" ]; then
    filename=`readlink $0`
fi
if [ "`echo $filename | grep "readlink"`" != "" ]; then
    filename=`ls -l  $0 | awk -F'->' '{print $2}' | tr -d ' '`
fi

dirname=`dirname $filename`

app=${dirname}/sqaxsafeforcnos_4
app_5=${dirname}/sqaxsafeforcnos_5

isqt5=0
if [ -f $app_5 ]; then
    ldd $app_5 2>~/.ldduierror.log
    errorinfo=`cat ~/.ldduierror.log`
    rm ~/.ldduierror.log
    if [[ ${errorinfo}x == ''x ]]; then
        info=(`ldd $app_5 | grep -i qt5 |  awk -F'=>' '{print $2~/Qt5/}'`)
        echo ${info[@]}
        if [[ ${#info[@]} -gt "1" ]] ; then
            if [[ "${info[@]}" =~ "1" ]]; then
                isqt5=1
            fi
            if [[ "${info[@]}" =~ "0" ]]; then
                isqt5=0
            fi
        fi
    fi
fi

if [ "$isqt5" = "1" ]; then
    app=$app_5
fi
log=/dev/null

nohup $app $@ > $log 2>&1 & 

if [ "$isqt5" = "1" ]; then
    sleep 1
    trayid=`ps -ef | grep sqaxsafeforcnos_5 | grep -v grep`
    ret=$?
    if [ $ret -eq 0 ]; then
        ok=1
    else
        nohup ${dirname}sqaxsafeforcnos_4 $@ > $log 2>&1 &
    fi
fi
