#!/usr/bin/env bash ############# # Variables # ############# # Define formatting variables fb="$(tput bold)" ; readonly fb fr="$(tput sgr0)" ; readonly fr dot_files_src='' ; readonly dot_files_src # Set dot file directory source deb_prefix='' ; readonly deb_prefix # Set Debian prefix fed_prefix='' ; readonly fed_prefix # Set Fedora prefix scripts_dir_src='' ; readonly scripts_dir_src # Set scripts directory source # Set scripts directory destination for new system scripts_dir_dest="" ; readonly scripts_dir_dest # Create required variables array required_vars=( 'dot_files_src' 'deb_prefix' 'fed_prefix' 'scripts_dir_src' 'scripts_dir_dest' ) ; readonly required_vars ############# # Functions # ############# display_usage() { local help_msg help_msg="Usage: bash ${0} Purpose: Configure a Debian or Fedora GNU/Linux shell environment. Variables to set: deb_prefix The string prefix that has been chosen to specify files with Debian-specific dot file content (e.g., dbn_). dot_files_src The name of the directory that contains the files that serve as the basis for the new system's dot files (e.g., dot_files/). fed_prefix The string prefix that has been chosen to specify files with Fedora-specific dot file content (e.g., fda_). scripts_dir_dest The destination directory on the new system to copy scripts to. scripts_dir_src The name of the directory that contains the scripts to be copied to the new system (e.g., scripts/)." echo "${help_msg}" } # Verify object exists and is a directory val_extant_dir() { local to_test to_test="${1}" [[ -d "${to_test}" ]] ; echo "${?}" } # Define starting point for execution of the program main() { # Parse arguments parsed_args="$(getopt \ -n "${0##*/}" \ -o h \ --long help \ -- "${@}")" inv_ents="${?}" # Check for invalid entries if [[ "${inv_ents}" -ne 0 ]]; then echo '' display_usage exit 1 fi # Set positional parameters using parsed argument string eval set -- "${parsed_args}" # Evaluate options while true; do case "${1}" in '-h' | '--help') display_usage exit 0 ;; '--') ## End of options shift break ;; esac done # Create unset variables array declare -a unset_vars # Verify that required variables are set for required_var in "${required_vars[@]}"; do if ! [[ "${!required_var}" ]]; then unset_vars+=("${required_var}") fi done # Exit script if variables are not set if [[ "${unset_vars[*]}" ]]; then err_msg='\nSet the following variable(s) before ' err_msg+='running the script again:' echo -e "${err_msg}\n${fb}${unset_vars[*]}${fr}" 1>&2 exit 1 fi abs_path="$(realpath "${0}")" # Get absolute path of script # Get absolute path of script configuration directory dir_path="${abs_path%/*}" cd "${dir_path}" || exit 1 # Change to directory script is running from # Filter out extraneous files, customize command prompt if grep -iq 'debian' '/etc/os-release'; then # Loop through each .txt file in dot_files_src for f in ./${dot_files_src}/*; do # Filter out Fedora-specific files if ! [[ "${f}" =~ ${dot_files_src}/${fed_prefix}.+ ]]; then f_base="${f%.txt}" # Remove Debian prefix from filename if [[ "${f_base}" =~ ${dot_files_src}/${deb_prefix}+ ]]; then f_base="${f_base/${deb_prefix}/}" fi # Create file basename f_base="${f_base##*/}" # Append file content cat "${f}" >> "${HOME}/.${f_base}" fi done elif grep -iq 'fedora' '/etc/os-release'; then for f in ./${dot_files_src}/*; do if ! [[ "${f}" =~ ${dot_files_src}/${deb_prefix}.+ ]]; then f_base="${f%.txt}" if [[ "${f_base}" =~ ${dot_files_src}/${fed_prefix}.+ ]]; then f_base="${f_base/${fed_prefix}/}" fi f_base="${f_base##*/}" cat "${f}" >> "${HOME}/.${f_base}" fi done else err_msg='\nUnable to verify supported GNU/Linux ' err_msg+='distribution.' echo -e "${err_msg}" 1>&2 exit 1 fi pass_or_fail="$(val_extant_dir "${scripts_dir_dest}")" if [[ "${pass_or_fail}" -ne 0 ]]; then mkdir "${scripts_dir_dest}" # Create script directory fi # Copy over scripts for f in ${dir_path}/${scripts_dir_src}/*; do cp -f "${f}" "${scripts_dir_dest}" done chmod -R 700 "${scripts_dir_dest}" # Make scripts executable # Display log out message echo -e '\nLog out and log in to see changes take effect.' } ########### # Program # ########### main "${@}" # Start program exit 0 # Exit script with successful exit status