comfortable-swipe/install
Rico Tiongson 66b2568c86
Add config command line tools and simplify source code structure (#81)
* Add simplified shell script

* Update stop command

* Update defaults.conf

* Add help dialog

* Simplify file checkpoint

* Improve tests

* Remove old library files

* Update install log

* Bump to version 1.2.0

* Apply clang format

* Add command line tools for configurations

* Bugfix keyswipe gesture not passing test

* Reformat files with clang and prettier

* Update dispatch script

* Add abort statuement

* Improve command line

* Add --bare flag

* Apply clang-format

* Update README

* Add to CHANGELOG for v1.2.0
2020-05-02 02:54:21 +08:00

182 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
set -e
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"
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,,}" =~ ^(yes|y)$ ]]; then
# MAKE SURE they really want to overwrite
read -r -p "Conf file will be overwritten! Are you sure? [Y/n] " response >&2
if [[ "${response,,}" =~ ^(yes|y)$ ]]; then
# They agreed... replace configuration
cat "$CONF_SOURCE" > "$CONF_TARGET"
else
abort
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
TRYSUDO=
if ! touch "$TARGET" 2> /dev/null; then
TRYSUDO=sudo
fi
# install to target, with hardcoded version
$TRYSUDO sed -E "s/^VERSION=.*/VERSION=$VERSION/" "$SOURCE" > "$TARGET"
# allow execute permissions
$TRYSUDO chmod +x "$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
# eheck permissions maybe if will need sudo
TRYSUDO=
if ! touch "$COMPILE_TARGET" 2> /dev/null; then
TRYSUDO=sudo
fi
# move executable to target
$TRYSUDO mv "$TMP_TARGET" "$COMPILE_TARGET"
echo "Installed: $COMPILE_TARGET"
}
## Installation of Autostart Desktop file
#
# /home/$USER/.config/autostart/comfortable-swipe.desktop
#
function install_autostart {
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