From 051ea5217225a790a6f4c529560f48ca2eb0c49d Mon Sep 17 00:00:00 2001 From: Michael Housh Date: Sat, 1 Jan 2022 14:48:25 -0500 Subject: [PATCH] Updated zsh setup script --- zsh/setup | 111 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 6 deletions(-) diff --git a/zsh/setup b/zsh/setup index 7aec1c4..92053c4 100755 --- a/zsh/setup +++ b/zsh/setup @@ -1,21 +1,120 @@ #!/bin/sh +set -e + config="${HOME}/.config" local_rc="${PWD}/config/.zshrc" local_env="${PWD}/config/.zshenv" local_funcs="${PWD}/config/zsh-functions" +uninstall=1 +remove=1 +copy=1 +link=0 -_make_dirs() { - mkdir "${config}" - mkdir "${config}/zsh" +_usage() { + printf "\n" >&2 + printf "Usage: setup [OPTIONS]\n" >&2 + printf "\n" >&2 + printf "Installs or Uninstalls zsh configuration files.\n" >&2 + printf "\n" >&2 + printf "Options:\n" >&2 + printf "\t-c | --copy : Copy the configuration files, instead of creating symlinks.\n" >&2 + printf "\t-h | --help : Print usage information.\n" >&2 + printf "\t-l | --link : Create symlinks to the configuration files (Default).\n" >&2 + printf "\t-r | --remove : Removes the entire ~/.config/zsh directory.\n" >&2 + printf "\t-u | --uninstall : Uninstalls configuration files.\n" >&2 + printf "\n" >&2 + printf "If called without the uninstall option then it will install the configuration files.\n" >&2 + printf "The copy and link options are ignored if called with the uninstall or remove option.\n" >&2 + printf "\n" >&2 } -_make_links() { +_parse_options() { + arg= + while ! test -z "$1"; do + arg="$1" + case $arg in + -c | --copy) + copy=0 + link=1 + shift;; + -h | --help) + _usage + exit;; + -l | --link) + copy=1 + link=0 + shift;; + -r | --remove) + remove=0 + shift;; + -u | --uninstall) + uninstall=0 + shift;; + *) + echo "Unknown option $arg" >&2 + shift;; + esac + done +} + +_make_dirs() { + if ! test -d "${config}"; then + mkdir "${config}" + fi + if ! test -d "${config}/zsh"; then + mkdir "${config}/zsh" + fi +} + +_remove_zsh() { + echo "Removing ~/.config/zsh directory..." >&2 + test -e "${HOME}/.zshenv" && rm "${HOME}/.zshenv" + test -d "${config}/zsh" && rm -r "${config}/zsh" +} + +_uninstall_zsh() { + echo "Uninstalling zsh configuration..." >&2 + test -e "${HOME}/.zshenv" && rm "${HOME}/.zshenv" + test -e "${config}/zsh/.zshrc" && rm "${config}/zsh/.zshrc" + test -e "${config}/zsh/zsh-functions" && rm "${config}/zsh/zsh-functions" +} + +_link_zsh() { + echo "Linking zsh configuration..." >&2 ln -sfv "${local_env}" "${HOME}" ln -sfv "${local_rc}" "${config}/zsh" ln -sfv "${local_funcs}" "${config}/zsh" } +_copy_zsh() { + echo "Copying zsh configuration..." >&2 + cp "${local_env}" "${HOME}" + cp "${local_rc}" "${config}/zsh" + cp "${local_funcs}" "${config}/zsh" +} + +_install_zsh() { + _make_dirs + # copy if in copy mode. + test "$copy" -eq 0 && _copy_zsh && return 0 + # link if in link mode. + test "$link" -eq 0 && _link_zsh && return 0 + # we don't know what to do error. + echo "Neither link or copy option was set, see --help for usage." >&2 + exit 1 +} + # -------------- main ------------------- -_make_dirs -_make_links +main() { + _parse_options "$@" + + # check if in remove mode. + test "$remove" -eq 0 && _remove_zsh && exit "$?" + # check if in uninstall mode. + test "$uninstall" -eq 0 && _uninstall_zsh && exit "$?" + # install + _install_zsh +} + +main "$@"