* Print abort() echo 'Installation aborted' to stdout. Allow \n (ENTER) to be the printed default: yes. * Actually handling the null response instead of galaxy brained putting a 'continue' as a placeholder in a conditional tree.
202 lines
5.7 KiB
Bash
Executable File
202 lines
5.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
if [[ "$USER" == root ]]; then
|
|
echo "Please run 'bash install' as non-root user" 2>&1
|
|
exit 1
|
|
fi
|
|
|
|
DIR="$(dirname "$0")"
|
|
VERSION="$(cat $DIR/VERSION | tr -d '[:space:]')"
|
|
|
|
# target executable
|
|
SOURCE="$DIR/comfortable-swipe"
|
|
TARGET="/usr/local/bin/comfortable-swipe"
|
|
|
|
# compile targets
|
|
COMPILE="$DIR/compile"
|
|
COMPILE_SOURCE="$DIR/comfortable-swipe-main.cpp"
|
|
COMPILE_TARGET="/usr/local/bin/comfortable-swipe-buffer"
|
|
|
|
# configurations
|
|
CONF_SOURCE="$DIR/config/comfortable-swipe.conf"
|
|
CONF_TARGET="$HOME/.config/comfortable-swipe.conf"
|
|
OLD_CONF_TARGET="/usr/local/share/comfortable-swipe/comfortable-swipe.conf"
|
|
|
|
# autostart
|
|
AUTOSTART_SOURCE="$DIR/config/comfortable-swipe.desktop"
|
|
AUTOSTART_TARGET="$HOME/.config/autostart/comfortable-swipe.desktop"
|
|
|
|
# stop any running comfortable-swipe if it exists
|
|
comfortable-swipe stop > /dev/null 2>&1 || true
|
|
|
|
|
|
# shorthand to abort the installation
|
|
function abort {
|
|
echo "Installation aborted" >&2
|
|
exit 1
|
|
}
|
|
|
|
# shorthand to try a command with sudo if it fails
|
|
function trysudo {
|
|
$@ 2> /dev/null || sudo $@
|
|
}
|
|
|
|
# uninstall existing program
|
|
function uninstall_existing {
|
|
local PROGRAM="${1:?missing program name}"
|
|
# remove existing comfortable-swipe
|
|
if [[ -x "$(command -v "$PROGRAM")" ]]; then
|
|
echo -n "Removing previous $(which "$PROGRAM") ... "
|
|
trysudo rm -f "$(which "$PROGRAM")"
|
|
echo "ok"
|
|
fi
|
|
}
|
|
|
|
|
|
## CONFIGURATION FILE LOADING
|
|
# This section handles installation of configuration file.
|
|
# We determine first if any old configuration files exists.
|
|
# make sure config directory exists
|
|
function install_configuration_file {
|
|
mkdir -p "$(dirname "$CONF_TARGET")"
|
|
|
|
# check if configuration already exists
|
|
if [[ -f "$CONF_TARGET" ]]; then
|
|
# ask user if we overwrite configuration
|
|
echo "Old conf file found in $CONF_TARGET" >&2
|
|
read -r -p "Keep the old conf file? (default: yes) [Y/n] " response >&2
|
|
|
|
# If response is empty, consider it as 'yes' (default)
|
|
if [[ -z "$response" ]]; then
|
|
response="y"
|
|
fi
|
|
|
|
if [[ "${response,,}" =~ ^(no|n)$ ]]; then
|
|
# MAKE SURE they really want to overwrite
|
|
read -r -p "Conf file will be overwritten! Are you sure? [Y/n] " response >&2
|
|
if [[ -z "$response" ]]; then
|
|
response="y"
|
|
fi
|
|
if [[ "${response,,}" =~ ^(no|n)$ ]]; then
|
|
abort
|
|
else
|
|
# They agreed... replace configuration
|
|
cat "$CONF_SOURCE" > "$CONF_TARGET"
|
|
fi
|
|
fi
|
|
else
|
|
# target does not exist yet
|
|
# try copying from older configurations
|
|
if [[ -f "$OLD_CONF_TARGET" ]]; then
|
|
# move configuration from an older version (< v1.2)
|
|
echo "Moving configuration from $OLD_CONF_TARGET to $CONF_TARGET ..."
|
|
mv "$OLD_CONF_TARGET" "$CONF_TARGET"
|
|
# clean up old directory if possible
|
|
rmdir "$(dirname "$OLD_CONF_TARGET")" > /dev/null 2>&1 || true
|
|
else
|
|
# fresh installation! just copy default configuration
|
|
cat "$CONF_SOURCE" > "$CONF_TARGET"
|
|
fi
|
|
fi
|
|
|
|
echo "Installed: $CONF_TARGET"
|
|
}
|
|
|
|
|
|
## Installation of main program
|
|
#
|
|
# /usr/local/bin/comfortable-swipe
|
|
#
|
|
function install_main_program {
|
|
# copy source to target with executable permissions
|
|
# install to target, with hardcoded version
|
|
if [[ -f "$TARGET" ]]; then
|
|
trysudo rm "$TARGET"
|
|
fi
|
|
trysudo cp "$SOURCE" "$TARGET"
|
|
trysudo sed -E "s/^VERSION=.*/VERSION=$VERSION/" -i "$TARGET"
|
|
# allow execute permissions with group
|
|
trysudo chmod +x "$TARGET"
|
|
# make sure non-root user is owner
|
|
trysudo chown "$USER" "$TARGET"
|
|
echo "Installed: $TARGET"
|
|
}
|
|
|
|
|
|
|
|
## Compilation and installation of C++ buffer program:
|
|
#
|
|
# /usr/local/bin/comfortable-swipe-buffer
|
|
#
|
|
function install_cpp_program {
|
|
# compile program to temporary file first
|
|
TMP_TARGET="$(mktemp)"
|
|
$COMPILE "$COMPILE_SOURCE" -o "$TMP_TARGET" -DCOMFORTABLE_SWIPE_VERSION="\"$VERSION\"" -DCOMFORTABLE_SWIPE_CONFIG="\"$CONF_TARGET\"" -DCOMFORTABLE_SWIPE_AUTOSTART="\"$AUTOSTART_TARGET\""
|
|
# compilation ok, now try to install with sudo
|
|
trysudo mkdir -p "$(dirname "$COMPILE_TARGET")"
|
|
# remove existing file for permissions to work
|
|
if [[ -f "$COMPILE_TARGET" ]]; then
|
|
sudo rm "$COMPILE_TARGET"
|
|
fi
|
|
trysudo mv "$TMP_TARGET" "$COMPILE_TARGET"
|
|
# bugfix: add with group permissions
|
|
trysudo chmod go+x "$COMPILE_TARGET"
|
|
# make sure non-root user is owner
|
|
trysudo chown "$USER" "$COMPILE_TARGET"
|
|
echo "Installed: $COMPILE_TARGET"
|
|
}
|
|
|
|
|
|
|
|
## Installation of Autostart Desktop file
|
|
#
|
|
# /home/$USER/.config/autostart/comfortable-swipe.desktop
|
|
#
|
|
function install_autostart {
|
|
mkdir -p "$(dirname "$AUTOSTART_TARGET")"
|
|
cat "$AUTOSTART_SOURCE" > "$AUTOSTART_TARGET"
|
|
echo "Installed: $AUTOSTART_TARGET"
|
|
}
|
|
|
|
|
|
# install configuration files first but defer output later
|
|
# this is to ensure that the prompt runs first in the script
|
|
INSTALL_CONF_OUTPUT="$(install_configuration_file)"
|
|
|
|
# uninstall existing commands
|
|
uninstall_existing comfortable-swipe
|
|
uninstall_existing comfortable-swipe-buffer
|
|
|
|
# install new binaries
|
|
echo "Installing binaries ..."
|
|
install_cpp_program
|
|
install_main_program
|
|
echo "$INSTALL_CONF_OUTPUT"
|
|
install_autostart
|
|
|
|
# add permissions to input group (defer)
|
|
# GROUP=$(ls -l /dev/input/event* | awk '{print $4}' | head --line=1) || abort
|
|
|
|
# make sure comfortable-swipe is in path
|
|
if ! command -v comfortable-swipe > /dev/null 2>&1; then
|
|
cat <<EOF >&2
|
|
WARNING: $(dirname "$TARGET") is not in PATH!
|
|
Make sure to add the following in your ~/.profile:
|
|
|
|
export PATH="$PATH:$(dirname "$TARGET")"
|
|
|
|
EOF
|
|
fi
|
|
|
|
cat <<EOF
|
|
|
|
Successfully installed comfortable-swipe $VERSION
|
|
Autostart switched $("$TARGET" autostart status)
|
|
|
|
EOF
|
|
|
|
# start the program
|
|
"$TARGET" start
|