diff --git a/git/setup b/git/setup index ce81e0d..a956439 100755 --- a/git/setup +++ b/git/setup @@ -1,18 +1,95 @@ #!/bin/sh config="${HOME}/.config" +uninstall=1 +remove=1 +copy=1 +link=0 +_usage() { + printf "\n" >&2 + printf "Usage: setup [OPTIONS]\n" >&2 + printf "\n" >&2 + printf "Installs or Uninstalls git 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/git 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 +} + +_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() { - test ! -d "${config}" && mkdir "${config}" + if ! test -d "${config}"; then + mkdir "${config}" + fi } _remove_git() { - test -d "${config}/git" && rm "${config}/git" + echo "Removing git directory..." + test -d "${config}/git" && rm -r "${config}/git" } _link_git() { + echo "Linking git configuration..." ln -sfv "${PWD}/git" "${config}" } +_copy_git() { + echo "Copying git configuration..." + cp -r "${PWD}/git" "${config}" +} + +_install() { + _make_dirs + + test "$copy" -eq 0 && _copy_git && return "$?" + test "$link" -eq 0 && _link_git && return "$?" + echo "Neither link or copy was passed, see --help for usage" + exit 1 +} + #------------------------------- main ------------------------------- -_make_dirs && _link_git +main() { + _parse_options "$@" + + test "$remove" -eq 0 && _remove_git && exit "$?" + test "$uninstall" -eq 0 && _remove_git && exit "$?" + + _install && exit "$?" +} + +main "$@"