Last active
January 14, 2018 10:51
-
-
Save ffflorian/71d02ec1c33f22c4d76b to your computer and use it in GitHub Desktop.
Automatically download and start xflux
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
SCRIPT_NAME="${0##*/}" | |
TMP_DIR="$(mktemp -d)" | |
LAT="52.51" | |
LONG="13.39" | |
FLUX_URL="https://justgetflux.com/linux/xflux64.tgz" | |
STR_RUNNING="xflux is already running! If you want to stop it, use '${SCRIPT_NAME} stop'." | |
STR_STOPPED="xflux is not running! If you want to start it, use '${SCRIPT_NAME} start'." | |
STR_DL="Downloading xflux..." | |
STR_ASKDL="xflux is currently not installed. Do you want me to download it? [Y/n] " | |
STR_NODL="Neither wget nor curl is installed. Can not download xflux." | |
STR_DLERR="Uh-oh, something went wrong while downloading xflux..." | |
STR_USAGE="Usage: ${SCRIPT_NAME} start [--yes|-y] | stop | pid " | |
# Change to script's directory to ensure we're in the | |
# correct folder. | |
# http://stackoverflow.com/questions/3349105/how-to-set-current-working-directory-to-the-directory-of-the-script | |
cd "${0%/*}" || exit 1 | |
_cleanup() { | |
rm -rf "${TMP_DIR}" | |
} | |
command_exist() { | |
command -v "${1}" > /dev/null | |
} | |
start_xflux() { | |
if pgrep "xflux" > /dev/null; then | |
echo "${STR_RUNNING}" | |
exit 0 | |
else | |
echo -n "Starting xflux for (${LAT}, ${LONG})..." | |
if (command_exist "xflux"); then | |
BIN="xflux" | |
elif (command_exist "${HOME}/bin/xflux"); then | |
BIN="${HOME}/bin/xflux" | |
else | |
download_xflux | |
fi | |
if [ ! -z "${BIN}" ]; then | |
"${BIN}" -l "${LAT}" -g "${LONG}" > /dev/null && echo "OK" | |
fi | |
exit 0 | |
fi | |
} | |
stop_xflux() { | |
if (pgrep "xflux" > /dev/null); then | |
echo -n "Stopping xflux..." | |
kill "$(pgrep -f xflux)" | |
echo "OK" | |
else | |
echo "${STR_STOPPED}" | |
fi | |
exit 0 | |
} | |
get_pid() { | |
if (pgrep "xflux" > /dev/null); then | |
pgrep -f xflux | |
else | |
echo "${STR_STOPPED}" | |
fi | |
exit 0 | |
} | |
ask_download() { | |
read -r -p "${STR_ASKDL}" RESPONSE | |
case "${RESPONSE}" in | |
[nN][oO]|[nN] ) exit 0 ;; | |
* ) download_xflux ;; | |
esac | |
} | |
download_xflux() { | |
if (command_exist wget); then | |
echo -n "${STR_DL}" | |
wget --directory-prefix="${TMP_DIR}" --quiet "${FLUX_URL}" | |
elif (command_exist curl); then | |
echo -n "${STR_DL}" | |
curl --silent --output "${TMP_DIR}/xflux64.tgz" "${FLUX_URL}" | |
else | |
echo "${STR_NODL}" | |
exit 1 | |
fi | |
if [ -f "${TMP_DIR}/xflux64.tgz" ]; then | |
echo "OK" | |
mkdir -p "${HOME}/bin" | |
tar -xzf "${TMP_DIR}/xflux64.tgz" -C "${HOME}/bin" | |
rm -rf "${TMP_DIR}" | |
start_xflux | |
else | |
echo "${STR_DLERR}" | |
exit 1 | |
fi | |
} | |
case "${1}" in | |
start ) | |
if (command_exist xflux || command_exist "${HOME}/bin/xflux"); then | |
start_xflux | |
else | |
case "${2}" in | |
--yes | -y ) | |
download_xflux | |
;; | |
* ) | |
ask_download | |
;; | |
esac | |
fi | |
;; | |
stop ) | |
stop_xflux | |
;; | |
pid ) | |
get_pid | |
;; | |
* ) | |
echo "${STR_USAGE}" | |
exit 3 | |
;; | |
esac | |
trap _cleanup EXIT |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment