Skip to content

Instantly share code, notes, and snippets.

@mubeeniqbal
Last active August 29, 2015 14:10
Show Gist options
  • Save mubeeniqbal/8654310f2698f53bf030 to your computer and use it in GitHub Desktop.
Save mubeeniqbal/8654310f2698f53bf030 to your computer and use it in GitHub Desktop.
Update files at correct paths in the system.
#!/usr/bin/env bash
#
# Update files at correct paths in the system.
# Edit the paths for your system before using the script.
# Reset prompt colors
readonly COLOR_OFF='\e[0m'
# Regular prompt colors
readonly RED='\e[0;31m'
readonly GREEN='\e[0;32m'
readonly YELLOW='\e[0;33m'
# Home path
readonly HOME="$(eval echo "~${SUDO_USER}")"
# Source paths
declare -A SRC
# Config paths
SRC['conkyrc']="${HOME}/git-repos/conkyrc/conkyrc"
SRC['i3config']="${HOME}/git-repos/i3config/i3config"
SRC['i3status']="${HOME}/git-repos/i3status/i3status.conf"
SRC['Xresources']="${HOME}/git-repos/Xresources/Xresources"
# Script paths
SRC['conky-i3bar']="${HOME}/git-repos/conky-i3bar/conky-i3bar"
SRC['i3exit']="${HOME}/git-repos/i3exit/i3exit"
# Make src array constant.
readonly SRC
# Destination paths
declare -A DEST
# Config paths
DEST['conkyrc']="${HOME}/.conkyrc"
DEST['i3config']="${HOME}/.i3/config"
DEST['i3status']="${HOME}/.i3status.conf"
DEST['Xresources']="${HOME}/.Xresources"
# Script paths
DEST['conky-i3bar']='/usr/local/bin/conky-i3bar'
DEST['i3exit']='/usr/local/bin/i3exit'
# Make dest array constant.
readonly DEST
# Prompt strings
readonly SF_OK="${GREEN}[S-✔]${COLOR_OFF}" # Source file exists
readonly SF_NO="${RED}[S-✘]${COLOR_OFF}" # Source file doesn't exist
readonly DD_OK="${GREEN}[Dd✔]${COLOR_OFF}" # Destination directory exists
readonly DD_NEW="${YELLOW}[Dd+]${COLOR_OFF}" # New destination directory
readonly DF_UPDATE="${GREEN}[D-↑]${COLOR_OFF}" # Update destination file
readonly DF_NEW="${YELLOW}[D-+]${COLOR_OFF}"; # New destination file
#######################################
# Print out error messages to STDERR.
# Globals:
# None
# Arguments:
# Error
# Returns:
# None
#######################################
err() {
echo "[$(date +'%Y-%m-%d %H:%M:%S%z')] error: $@" 1>&2
}
main() {
if [[ "${EUID}" -ne 0 ]]; then
err 'update-files: you cannot perform this operation unless you are root: Permission denied'
exit 1
fi
echo -e "Update files: source ${YELLOW}=>${COLOR_OFF} destination\n"
local all_sources_exist=true
local all_dest_dirs_exist=true
local some_dest_files_exist=false
local some_dest_files_dont_exist=false
local key
for key in "${!SRC[@]}"; do
# Another way to write if...then...else statements:
#
# if <condition>; then
# <if_statements>
# else
# <else_statements>
# fi
#
# can be rewritten as
#
# <condition> && <if_statements>
# <condition> || <else_statments>
[[ -f "${SRC[${key}]}" ]] && echo -en "${SF_OK} "
[[ -f "${SRC[${key}]}" ]] || { echo -en "${SF_NO} "; all_sources_exist=false; }
[[ -d "${DEST[${key}]%/*}" ]] && echo -en "${DD_OK} "
[[ -d "${DEST[${key}]%/*}" ]] || { echo -en "${DD_NEW} "; all_dest_dirs_exist=false; }
[[ -f "${DEST[${key}]}" ]] && { echo -en "${DF_UPDATE} "; some_dest_files_exist=true; }
[[ -f "${DEST[${key}]}" ]] || { echo -en "${DF_NEW} "; some_dest_files_dont_exist=true; }
echo -e "'${SRC[${key}]}' ${YELLOW}=>${COLOR_OFF} '${DEST[${key}]}'"
done
echo
# Directly execute command true/false in condition (i.e. without [[) to test
# since bash has no boolean variable types.
"${all_sources_exist}" && echo -e "${SF_OK} All source files exist."
"${all_sources_exist}" || echo -e "${SF_NO} Source files are missing."
"${all_dest_dirs_exist}" && echo -e "${DD_OK} All destination directories exist."
"${all_dest_dirs_exist}" || echo -e "${DD_NEW} New destination directories will be created."
"${some_dest_files_exist}" && echo -e "${DF_UPDATE} Already existing destination files will be updated."
"${some_dest_files_dont_exist}" && echo -e "${DF_NEW} New destination files will be created."
echo
if ! "${all_sources_exist}";then
err 'update-files: source files are missing'
exit 1
fi
read -rp 'Proceed with update? [Y/n] '
# If input is not Y|y then exit with an error code of 1.
[[ "${REPLY}" =~ ^[Yy]$ ]] || exit 1
echo
for key in "${!SRC[@]}"; do
if [[ ! -d "${DEST[${key}]%/*}" ]]; then
mkdir -vp "${DEST[${key}]%/*}" || return
fi
cp -vf "${SRC[${key}]}" "${DEST[${key}]}" || return
done
exit 0
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment