#!/bin/bash

if [[ $USER = "root" ]]; then
    echo "You should run this script as user, not as root."
    exit 1
fi

# app files
INSTALL_FOLDER="/opt/ikeybase/"

if [ -d "$INSTALL_FOLDER" ]; then
    sudo rm -r "$INSTALL_FOLDER"
fi

sudo mkdir -p "$INSTALL_FOLDER"
sudo cp -a ./lib "$INSTALL_FOLDER"
sudo cp -a ./libexec "$INSTALL_FOLDER"
sudo cp -a ./translations "$INSTALL_FOLDER"
sudo cp iKeyBase "$INSTALL_FOLDER"
sudo cp README.txt "$INSTALL_FOLDER"
sudo cp README.ru.txt "$INSTALL_FOLDER"
sudo cp qt.conf "$INSTALL_FOLDER"
sudo cp ikeybase.png /usr/share/icons/hicolor/128x128/apps/
sudo cp ikeybase.desktop /usr/share/applications/
sudo chmod +x /usr/share/applications/ikeybase.desktop
sudo update-desktop-database

# add a link to the desktop
cp ikeybase.desktop "`xdg-user-dir DESKTOP`"

# rules for usb-based devices
sudo cp 99-hid.rules /etc/udev/rules.d

# permissions for devices
sudo adduser $USER dialout
sudo adduser $USER tty
sudo adduser $USER uucp

echo "You should install 'xcb-xinerama' if application doesn't start."
echo "On Debian-based distros you should run: sudo apt-get install libxcb-xinerama0"
echo ""
echo "Check README.txt for details."
