#!/usr/bin/env sh
# Script to download and install or completely remove the libsciter-gtk.so library for RustDesk.
# Library homepage: https://github.com/c-smile/sciter-sdk
#
ico="/usr/share/pixmaps/rustdesk.png"
name="sciter-installer"
path="/usr/lib/rustdesk"
lib="libsciter-gtk.so"
md5_x86_64="91a95f199569afe4fbf9e433853f8f97"
md5_arm64="34f15f8bee68ce43367183b2a1be10c4"
msg_1=$(cat <<E_O_F
File $path/$lib allready exist.
Do you want to remove it?
E_O_F
)
msg_2=$(cat <<E_O_F
<b>NOTE:</b> To use RustDesk program, you need to get a third-party shared library - $lib.

Do you want to download and install it now?
E_O_F
)
msg_3=$(cat <<E_O_F
The md5 hash mismatch or file not found! Try to start download again!
E_O_F
)

function do_install {
arch=$(uname -m)

if [[ $arch != "x86_64" && $arch != "aarch64" ]]; then
  zenity --icon=$ico --width=400 --height=200 --info --text="Error! Unsupported architecture detected." --title="$name" && exit 1
fi

if [[ $arch == "x86_64" ]]; then
wget -O /tmp/$lib https://raw.githubusercontent.com/c-smile/sciter-sdk/master/bin.lnx/x64/$lib 2>&1 | \
sed -u 's/.*\ \([0-9]\+%\)\ \+\([0-9.]\+\ [KMB\/s]\+\)$/\1\n# Downloading \2/' | \
sed -u 's/.* \([0-9]\+%\)\ \+\([0-9.]\+.\) \(.*\)/\1\n# Speed \2\/s, remained \3/' | \
zenity --progress --percentage=0 --title="Downloading..." --auto-close || { zenity --icon=$ico --info --text="Installation canceled." --title="$name"; exit 1; }
down_md5=$(md5sum /tmp/$lib | awk '{ print $1 }')
  if [ "$md5_x86_64" == "$down_md5" ]; then
    pkexec mv /tmp/$lib $path || { zenity --icon=$ico --info --text="Installation canceled." --title="$name"; exit 1; }
  else
    zenity --width=400 --height=200 --icon=$ico --info --text="$msg_3" --title="$name" && exit 1
  fi
fi

if [[ $arch == "aarch64" ]]; then
wget -O /tmp/$lib https://raw.githubusercontent.com/c-smile/sciter-sdk/master/bin.lnx/arm64/$lib 2>&1 | \
sed -u 's/.*\ \([0-9]\+%\)\ \+\([0-9.]\+\ [KMB\/s]\+\)$/\1\n# Downloading \2/' | \
sed -u 's/.* \([0-9]\+%\)\ \+\([0-9.]\+.\) \(.*\)/\1\n# Speed \2\/s, remained \3/' | \
zenity --progress --percentage=0 --title="Downloading..." --auto-close || { zenity --icon=$ico --info --text="Installation canceled." --title="$name"; exit 1; }
down_md5=$(md5sum /tmp/$lib | awk '{ print $1 }')
  if [ "$md5_arm64" == "$down_md5" ]; then
    pkexec mv /tmp/$lib $path || { zenity --icon=$ico --info --text="Installation canceled." --title="$name"; exit 1;}
  else
    zenity --width=400 --height=200 --icon=$ico --info --text="$msg_3" --title="$name" && exit 1
  fi
fi
}

function do_remove {
pkexec rm -f $path/$lib || { zenity --icon=$ico --info --text="Removal canceled." --title="$name"; exit 1; }
}

if [ -e "$path/$lib" ]; then
  if zenity --width=400 --height=200 --icon=$ico --question --text="$msg_1" --title="$name"; then
  do_remove
  zenity --icon=$ico --info --text="Completely removed!" --title="$name"
  else
  zenity --icon=$ico --info --text="Removal canceled." --title="$name"
  fi
else
  if zenity --width=400 --height=200 --icon=$ico --question --text="$msg_2" --title="$name"; then
  do_install
  zenity --icon=$ico --info --text="Installation complete!" --title="$name"
  else
  zenity --icon=$ico --info --text="Installation canceled." --title="$name" && exit 1
  fi
fi

exit 0
