From 428333c5c17d9b954a2113b79dafa7319fbb6cbf Mon Sep 17 00:00:00 2001
From: Chris Coley <chris@codingallnight.com>
Date: Fri, 27 Jan 2023 19:26:23 -0800
Subject: [PATCH] Refactor install script to allow running from any directory

---
 LICENSE    |  2 +-
 README.md  | 11 ++++----
 install    | 40 ++++++++++++++++++++++++++++++
 install.sh | 73 ------------------------------------------------------
 4 files changed, 47 insertions(+), 79 deletions(-)
 create mode 100755 install
 delete mode 100755 install.sh

diff --git a/LICENSE b/LICENSE
index ee5b327..fcbf849 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,6 +1,6 @@
 MIT License
 
-Copyright (c) 2019 Chris Coley
+Copyright (c) 2023 Chris Coley
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
diff --git a/README.md b/README.md
index fb57e9e..d80414f 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,11 @@
-To use this repository, you need to clone the repo into your home directory. Then `cd` into the cloned repo and run the install script.
+To use this repository, you need to clone the repo and run the install script. I recommend cloning the repo into your home directory, but it should work from any directory.
 
 ```bash
-cd ~
-git clone https://git.codingallnight.com/chris/dotfiles.git [directory-name]
-cd <directory-name>
-./install.sh
+# Clone the repo
+git clone https://git.codingallnight.com/chris/dotfiles.git
+
+# Run the install script
+dotfiles/install
 ```
 
 The install script will take any pre-existing dotfiles that would be overwritten by the installation process and rename them by appending `.old` to the end of the filename. For example, your `.bashrc` file will be renamed to `.bashrc.old`. Then, the script creates symlinks in your home directory that point to files in the cloned repository.
diff --git a/install b/install
new file mode 100755
index 0000000..50eaa28
--- /dev/null
+++ b/install
@@ -0,0 +1,40 @@
+#!/bin/bash
+
+# Get the base directory relative to the home directory
+BASEDIR="$(realpath --relative-to=${HOME} "$(dirname ${BASH_SOURCE[0]})")"
+
+
+# Bash
+[[ -h ~/.bashrc ]] && { rm ~/.bashrc; } || { mv ~/.bashrc{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/bashrc ~/.bashrc
+[[ -h ~/.bash_aliases ]] && { rm ~/.bash_aliases; } || { mv ~/.bash_aliases{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/bash_aliases ~/.bash_aliases
+
+
+# Vim
+[[ -h ~/.vimrc ]] && { rm ~/.vimrc; } || { mv ~/.vimrc{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/vimrc ~/.vimrc
+[[ -h ~/.vim ]] && { rm ~/.vim; } || { mv ~/.vim{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/vim ~/.vim
+
+
+# dircolors
+[[ -h ~/.dircolors ]] && { rm ~/.dircolors; } || { mv ~/.dircolors{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/dircolors.ansi-dark ~/.dircolors
+
+
+# Git
+[[ -h ~/.gitconfig ]] && { rm ~/.gitconfig; } || { mv ~/.gitconfig{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/gitconfig ~/.gitconfig
+[[ -h ~/.git-completion.bash ]] && { rm ~/.git-completion.bash; } || { mv ~/.git-completion.bash{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/git-completion.bash ~/.git-completion.bash
+[[ -h ~/.git-prompt.sh ]] && { rm ~/.git-prompt.sh; } || { mv ~/.git-prompt.sh{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/git-prompt.sh ~/.git-prompt.sh
+
+
+# PostgreSQL
+[[ -h ~/.psqlrc ]] && { rm ~/.psqlrc; } || { mv ~/.psqlrc{,.old} 2> /dev/null; }
+ln -s ${BASEDIR}/psqlrc ~/.psqlrc
+
+
+#vi: set ts=4 sw=4 et ft=sh:
diff --git a/install.sh b/install.sh
deleted file mode 100755
index 3fe44cd..0000000
--- a/install.sh
+++ /dev/null
@@ -1,73 +0,0 @@
-#!/bin/bash
-
-# Run this from the dotfiles dir
-DOTFILES=`pwd`
-
-
-
-# bash
-if [ -h ~/.bashrc ]
-then rm ~/.bashrc
-else mv ~/.bashrc{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/bashrc ~/.bashrc
-
-if [ -h ~/.bash_aliases ]
-then rm ~/.bash_aliases
-else mv ~/.bash_aliases{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/bash_aliases ~/.bash_aliases
-
-
-
-# vim
-if [ -h ~/.vimrc ]
-then rm ~/.vimrc
-else mv ~/.vimrc{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/vimrc ~/.vimrc
-
-if [ -h ~/.vim ]
-then rm ~/.vim
-else mv ~/.vim{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/vim ~/.vim
-
-
-
-# dircolors
-if [ -h ~/.dircolors ]
-then rm ~/.dircolors
-else mv ~/.dircolors{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/dircolors.ansi-dark ~/.dircolors
-
-
-
-# Git
-if [ -h ~/.gitconfig ]
-then rm ~/.gitconfig
-else mv ~/.gitconfig{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/gitconfig ~/.gitconfig
-
-if [ -h ~/.git-completion.bash ]
-then rm ~/.git-completion.bash
-else mv ~/.git-completion.bash{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/git-completion.bash ~/.git-completion.bash
-
-if [ -h ~/.git-prompt.sh ]
-then rm ~/.git-prompt.sh
-else mv ~/.git-prompt.sh{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/git-prompt.sh ~/.git-prompt.sh
-
-
-
-# PostgreSQL
-if [ -h ~/.psqlrc ]
-then rm ~/.psqlrc
-else mv ~/.psqlrc{,.old} 2> /dev/null
-fi
-ln -s $DOTFILES/psqlrc ~/.psqlrc
-- 
GitLab