#!/bin/bash
clear
echo "Your architecture is 64 bit"
echo ""

#Set current working directory to the directory of the script
cd "$(dirname "$0")"

echo "ScanaStudio install script for Ubuntu "
echo "====================================="
echo "(c)2023 IKALOGIC S.A.S. FRANCE"
echo ""
echo "This Install script requires SUDO privileges"
echo ""
sudo -s echo "" #ask for user's password once for all



echo "[1/6]--- Installing drivers"

sudo cp -f driver/* /etc/udev/rules.d



echo "[2/6]--- Activating menu icons..."

# verify if gconf tool exists
GCONFTOOL=`whereis gconftool-2 | awk '{ print $2 }'`
[ -f "$GCONFTOOL" ] && $GCONFTOOL --type boolean --set /desktop/gnome/interface/menus_have_icons true
# gconftool-2 --type boolean --set /desktop/gnome/interface/menus_have_icons true



echo "[3/6]--- Installing ScanaStudio at ~/Ikalogic/ScanaStudio/"

# make dirs if they don't exist
mkdir -p ~/Ikalogic/ScanaStudio/

# copy ScanaStudio bin
cp -f bin/ScanaStudio ~/Ikalogic/ScanaStudio/
chmod ugo+x ~/Ikalogic/ScanaStudio/ScanaStudio

# copy update file
cp -f bin/update ~/Ikalogic/ScanaStudio/
chmod ugo+x ~/Ikalogic/ScanaStudio/update

# copy scanastudio_launcher file
cp -f bin/scanastudio_launcher.sh ~/Ikalogic/ScanaStudio/
chmod ugo+x ~/Ikalogic/ScanaStudio/scanastudio_launcher.sh

make_dir_and_copy() {
  local dir_name=$1
  
  # make dirs if they don't exist
  mkdir -p ~/Ikalogic/ScanaStudio/$dir_name
  
  # copy
  cp -f -r bin/$dir_name/* ~/Ikalogic/ScanaStudio/$dir_name/
  
  # change right
  chmod 0755 ~/Ikalogic/ScanaStudio/$dir_name/*
}

make_dir_and_copy bearer
make_dir_and_copy imageformats
make_dir_and_copy lib
make_dir_and_copy platforminputcontexts
make_dir_and_copy platforms
make_dir_and_copy position
make_dir_and_copy sensorgestures
make_dir_and_copy sqldrivers
make_dir_and_copy xcbglintegrations



echo "[4/6]--- Installing custom Library"
copy_custom_lib() {
  local lib_name=$1
  
  # copy
  cp -f -r custom_lib/$lib_name ~/Ikalogic/ScanaStudio/$lib_name
  
  # change right
  chmod 0755 ~/Ikalogic/ScanaStudio/$lib_name
}

copy_custom_lib libftd2xx.a
copy_custom_lib libftd3xx.so
sudo ldconfig

cp custom_lib/libssl*.so* ~/Ikalogic/ScanaStudio/
cp custom_lib/libcrypto*.so* ~/Ikalogic/ScanaStudio/


echo "[5/6]--- Installing mime types"

# make dirs if they don't exist (very rare...)
mkdir -p ~/.local/share/mime/packages
mkdir -p ~/.local/share/icons
mkdir -p ~/.local/share/icons/hicolor
mkdir -p ~/.local/share/applications

# copy mime type xml file & update
cp -f mime/ScanaStudio.xml ~/.local/share/mime/packages

# copy icons for application and workspace
cp -f -R mime/hicolor/* ~/.local/share/icons/hicolor
cp -f mime/application-x-ScanaStudio.png ~/.local/share/icons

# copy the desktop file
cp -f mime/ScanaStudio.desktop ~/.local/share/applications

echo "Exec=$HOME/Ikalogic/ScanaStudio/scanastudio_launcher.sh" >> ~/.local/share/applications/ScanaStudio.desktop
echo "Path=/home/$LOGNAME/Ikalogic/ScanaStudio" >> ~/.local/share/applications/ScanaStudio.desktop



echo "[6/6]--- Updating mime database (This may take a few seconds...)"

update-mime-database  ~/.local/share/mime
update-desktop-database ~/.local/share/applications

chmod -R 777 ~/Ikalogic/



echo "[---]--- DONE!"
echo ""
echo "You may need to log off and log in again to finalize this install, then you can type ScanaStudio in the Dash to launch it."
echo ""
echo "IKALOGIC teams wishes you happy electronics debugging with ScanaStudio!"
echo ""
