# ~/.profile: executed by Bourne-compatible login shells.

if [ "$BASH" ]; then
  if [ -f ~/.bashrc ]; then
    . ~/.bashrc
  fi
fi

mesg n 2> /dev/null || true

export TSLIB_CONSOLEDEVICE=none
export TSLIB_FBDEVICE=/dev/fb0
export TSLIB_PLUGINDIR=/lib/ts
export TSLIB_CONFFILE=/etc/ts.conf
export TSLIB_CALIBFILE=/etc/pointercal

export LD_LIBRARY_PATH=/lib/qt5lib:/lib/qt5plugins:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/lib/libV17:$LD_LIBRARY_PATH

export QT_QPA_PLATFORM_PLUGIN_PATH=/lib/qt5plugins/platforms
export QT_QPA_PLATFORM=linuxfb:fb=/dev/fb0
export QT_QPA_FONTDIR=/usr/share/fonts

export QT_QPA_FB_DRM=1
export QT_QPA_FB_DISABLE_INPUT=0    #不使用qt自带的evdev
export QT_QPA_FB_TSLIB=1            #使用tslib接管
export QT_PLUGIN_PATH=/lib/qt5plugins

s3='TSLIB_TSDEVICE=/dev/input/event'
#s4='QT_QPA_GENERIC_PLUGINS=tslib:/dev/input/event'
#sTslib='QT_QPA_EVDEV_TOUCHSCREEN_PARAMETERS=/dev/input/event'


#------------------------new found input event-----------------------------------------
device_file=/proc/bus/input/devices

record_event=0	#判断是否可以记录event 0:不记录 1:记录touch 2:记录mouse
touch_event_index=-1
mouse_event_index=-1

#数字大，优先级越高
touch_array[0]="HID TOUCH HID Touch Panel Touchscreen"
touch_array[1]="ILITEK ILITEK-TP"
touch_array[2]="ILITEK Multi-Touch-V3000"               #电容屏
touch_array[3]="PixArt Dell MS116 USB Optical Mouse"
touch_array[4]="Silicon Integrated System Co. SiS HID Touch Controller"
touch_array[5]="eGalax Inc. USB TouchController"
touch_array[6]="Weida Hi-Tech CoolTouch® System"

mouse_array[0]="HID TOUCH HID Touch Panel Mouse"
mouse_array[1]="ILITEK ILITEK-TP Mouse"
mouse_array[2]="ILITEK Multi-Touch-V3000 Mouse"
mouse_array[3]="eGalax Inc. USB TouchController Stylus"

# touch_array 查找触摸事件，从后往前遍历数组, 可以确定优先级
length_touch=${#touch_array[@]}
for ((i=length_touch-1; i>=0; i--)); do
    cur_touch_name=${touch_array[i]}
    
    found_touch=0
    while read -r line; do
	    if [ -n "$line" ] && [[ "$line" == *"Name="* ]]; then #判断事件名称 -n不为空
	        if [[ "$line" == *"\"$cur_touch_name\""* ]]; then
	            found_touch=1
	        fi
	    fi
	    
        if [[ $found_touch -eq 1 && "$line" == *"Handlers="* ]]; then
            #echo "found Handlers"
            s5=$(echo $line | awk -F'event' '{print $2}' | awk '{print $1}')
            #s5=${line##*event}
            s2=`expr ${s5}`
            if [ $found_touch -eq 1 ];then
                touch_event_index=$s2
                break
            fi 
        fi   
    done < $device_file
    
    if [ $found_touch -eq 1 ]; then    #-eq 等于
        echo "pick name: "$cur_touch_name", touch_event_index: "$touch_event_index
        break   
    fi
done

# length_mouse 查找触摸事件，从后往前遍历数组, 可以确定优先级
length_mouse=${#mouse_array[@]}
for ((i=length_mouse-1; i>=0; i--)); do
    cur_mouse_name=${mouse_array[i]}
    
    found_mouse=0
    while read -r line; do
	    if [ -n "$line" ] && [[ "$line" == *"Name="* ]]; then #判断事件名称 -n不为空
	        if [[ "$line" == *"\"$cur_mouse_name\""* ]]; then
	            found_mouse=1
	        fi
	    fi
	    
        if [[ $found_mouse -eq 1 && "$line" == *"Handlers="* ]]; then
            s5=$(echo $line | awk -F'event' '{print $2}' | awk '{print $1}')
            #s5=${line##*event}
            s2=`expr ${s5}`
            if [ $found_mouse -eq 1 ];then
                mouse_event_index=$s2
                break
            fi 
        fi   
    done < $device_file
    
    if [ $found_mouse -eq 1 ]; then    #-eq 等于
        echo "pick name: "$cur_mouse_name", mouse_event_index: "$mouse_event_index
        break   
    fi
done


echo "touch_event_index="$touch_event_index",mouse_event_index"=$mouse_event_index
#------------------------new found input event------------------

export ${s3}${touch_event_index}
#export ${s4}${s2}
#export ${sTslib}${s2}:rotate=270
#export QT_QPA_FB_HIDECURSOR=0
export QT_QPA_EVDEV_MOUSE_PARAMETERS=/dev/input/event${mouse_event_index}

cd /root/bin-AOI
chmod -R 777 *

#>>>>>>>>>>>>>>>>>>>>update>>>>>>>>>>>>>>>>>>>>
file_gz="newupdate.tar.gz"
file_rar="newupdate.rar"
file_xz="newupdate.tar.xz"
file_zip="newupdate.zip"
tardir="./newupdate"
cammerdir="cammer*"
fileupdate="./update.sh"

isfindupdate=0
if [ -f $file_gz -o -f $file_rar -o -f $file_xz -o -f $file_zip ];
then
    rm -rf $tardir 
    mkdir $tardir
	isfindupdate=$[$isfindupdate+1]
	
	if [ -f $file_xz ];	#tar.xz
	then
		tar -Jxvf $file_xz -C $tardir
		rm -f $file_xz
	elif [ -f $file_rar ];	#rar
	then
		mv $file_rar $tardir
		cd $tardir
		rar x $file_rar
		rm -f $file_rar
	elif [ -f $file_gz ];	#tar.gz
	then
		tar -zxvf $file_gz -C $tardir
		rm -f $file_gz
	elif [ -f $file_zip ];   #zip
	then
		unzip -o $file_zip -d $tardir
		rm -f $file_zip
	fi
fi

cd /root/bin-AOI
if [ $isfindupdate == 1 ];
then
	chmod -R 777 *
    if ls $tardir/$cammerdir > $tardir/cam 2>&1;then
    rm -rf $cammerdir
    rm -rf $tardir/cam
    fi
    cp -rf $tardir/* ./
    if [ -f $fileupdate ];
    then
        $fileupdate
        cd /root/bin-AOI
        rm -f $fileupdate
    fi
    rm -rf $tardir
fi
#<<<<<<<<<<<<<<<<<<<<update<<<<<<<<<<<<<<<<<<<<

cd /root/bin-AOI
rmmod pinctrl_sunrisepoint
mknod /dev/led c 233 0
insmod led.ko

#>>>>>>>>>>>>>>>>>>>>special_action>>>>>>>>>>>>>>>>>>>>
#执行特殊脚本
cd /root/bin-AOI
framerun="framerun.sh"
if [ -f $framerun ];
then 
    ./$framerun
fi
rm -f $framerun

#更新时间
rm /etc/localtime
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#hwclock --hctosys 
#<<<<<<<<<<<<<<<<<<<<special_script<<<<<<<<<<<<<<<<<<<<

cd /root/bin-AOI
mkdir -p /root/bin-AOI/cnt_net/workspace


#备份标准输出日志
name2=$(date +%Y-%m-%d_%H:%M:%S)
stdoutlog="/root/bin-AOI/cnt_net/workspace/log_stdout"
mkdir -p $stdoutlog
if [ -f $stdoutlog/log.txt ];
then
    mv -f $stdoutlog/log.txt $stdoutlog/log$name2.txt
fi

#内核日志输出级别
echo 1 > /proc/sys/kernel/printk

./application > $stdoutlog/log.txt 2>&1




