#!/bin/bash
#default install path
target_dir="/opt/pqlabs/platform"
platform_air_app_id="com.pqlabs.MultiTouchPlatform"
platform_air_app_id_of_rpm="com.pqlabs.multitouchplatform"

#get the install target from the arguments
if [ $# -gt 0 ]; then
	target_dir=$1
fi

# test the sudo
echo "testing the sudo ..."
sudo echo "OK."
if [ $? -ne 0 ]; then
   echo "Failed. please retry later, or switch to \"root\" by \"su\" and retry."
   exit 1 
fi

# remove
sudo killall -q pqmtpdaemon
sudo killall -q MultiTouchPlatformHelper

# close the enforcing selinux firstly
is_selinux_enabled=0
is_selinux_enforced=0
sudo id -Z
if [ $? -eq 0 ]; then
    echo "selinux enabled"
    is_selinux_enabled=1
    selinux_mode=$(getenforce)
    if [ "Enforcing" = ${selinux_mode} ]; then
        echo "selinux enforcing"
        is_selinux_enforced=1
        sudo setenforce 0
    fi
fi

# uninstall the UI
sudo ${target_dir}/arh -isAppInstalled "${platform_air_app_id}"
if [ 0 -eq $? ]; then
    echo " unintall UI ..."
    sudo ${target_dir}/arh -uninstallAppSilent "${platform_air_app_id}"
    
    sudo rm -f /usr/share/icons/com.pqlabs.multitouchplatform.png
fi
type -a rpm
if [ $? -eq 0 ];then
    sudo rpm -q ${platform_air_app_id_of_rpm}
    if [ $? -eq 0 ]; then
        sudo rpm -e ${platform_air_app_id_of_rpm}
    fi
fi

# uninstall the AIR runtime
sudo ${target_dir}/arh -isRuntimeInstalled
if [ 0 -eq $? ] && [ -e ${target_dir}/AdobeAIRInstaller.bin ]; then 
    echo " The \"Adobe AIR run time\" may be necessary for other applications."
    echo " Uninstall the \"Adobe AIR run time\"?(y/n)[n]"
    read input
    if [ "$input" = "y" ]; then
        sudo ${target_dir}/AdobeAIRInstaller.bin -uninstall
	type -a rpm
	if [ $? -eq 0 ]; then
	    sudo rpm -q adobeair
	    if [ $? -eq 0 ]; then
	        sudo rpm -e adobeair
	    fi
	fi
        sudo rm -f ${target_dir}/AdobeAIRInstaller.bin
    fi
fi

# remove the user command "pqmtplatform"
sudo rm -f /usr/local/bin/pqmtplatform

# remove the auto start
type -a rc-update
if [ $? -eq 0 ]; then #it's gentoo
    sudo rc-update delete pqmtd
else
    type -a update-rc.d
    if [ $? -eq 0 ]; then
        sudo update-rc.d -f pqmtd remove
    else
        is_chkconfig_exist=0
        type -a chkconfig
        if [ $? -eq 0 ]; then
            is_chkconfig_exist=1
        fi
        type -a systemctl
        if [ $? -eq 0 ]; then
            sudo systemctl disable pqmtd.service
            sudo rm -f /lib/systemd/system/pqmtd.service
        elif [ $is_chkconfig_exist -eq 1 ]; then 
            sudo chkconfig --del pqmtd
        else
            echo "no update-rc.d or chkconfig, ln manually"
            sudo rm /etc/rc0.d/K20pqmtd
            sudo rm /etc/rc1.d/K20pqmtd
            sudo rm /etc/rc2.d/S20pqmtd
            sudo rm /etc/rc3.d/S20pqmtd
            sudo rm /etc/rc4.d/S20pqmtd
            sudo rm /etc/rc5.d/S20pqmtd
            sudo rm /etc/rc6.d/K20pqmtd
        fi
    fi
fi
if [ -e /etc/init.d/pqmtd ]; then
    sudo rm -f /etc/init.d/pqmtd
fi
if [ -e /etc/X11/Xsession.d/99pqlocalhost_start ]; then
    sudo rm -f /etc/X11/Xsession.d/99pqlocalhost_start
fi
if [ -e /etc/X11/xinit/xinitrc.d/pqlocalhost.sh ]; then
    sudo rm -f /etc/X11/xinit/xinitrc.d/pqlocalhost.sh
fi

# remove the files in target dir
cd ${target_dir}
if [ -e ${target_dir}/AdobeAIRInstaller.bin ]; then
    sudo rm -rf $(ls ${target_dir} | grep -v "AdobeAIRInstaller.bin")
else
    sudo rm -rf ${target_dir}
fi

# restore the selinux
if [ 1 -eq ${is_selinux_enforced} ]; then
    sudo setenforce 1
fi

echo " Uninstall Complete."
exit 0
