From 27ef0d98fa9671d94fe40aed30f47c394b37bb50 Mon Sep 17 00:00:00 2001 From: Kris Lamoureux Date: Fri, 10 Jan 2020 01:12:27 -0500 Subject: [PATCH] Backup regular files and replace broken links --- install.sh | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/install.sh b/install.sh index 2c80bde..c274295 100755 --- a/install.sh +++ b/install.sh @@ -1,28 +1,46 @@ #!/bin/bash + +# This script loops through files in this repository, creating symlinks in your +# home directory targeting here. It will prompt you to backup regular files and +# replace broken symlinks but will ignore good symlinks regardless of their +# target. + DOTFILES="$(pwd)/.*[a-z]" -DATE=$(date '+%Y%m%d%H%M%S') for FILE in $DOTFILES ; do BASEFILE=$(basename $FILE) + NEWLINK=0 # Exclude the repo's .git folder if [ "$BASEFILE" != ".git" ] ; then - # If dotfile is not a symlink and exists then back it up - if [ ! -L ~/$BASEFILE ] ; then - read -p "Press [ENTER] to move ~/$BASEFILE to ~/$BASEFILE-$DATE" - echo "mv ~/$BASEFILE ~/$BASEFILE-$DATE" + # If basefile is a link + if [ -L ~/$BASEFILE ] ; then + # If basefile is not a file, and therefore is a broken symlink + if [ ! -f ~/$BASEFILE ] ; then + read -p "Press [ENTER] to replace broken symlink ~/$BASEFILE. CTRL+C aborts" + rm ~/$BASEFILE + NEWLINK=1 + else + echo "~/$BASEFILE is already linked." + fi + # If basefile is a regular file + elif [ -f ~/$BASEFILE ] ; then + DATE=$(date '+%Y%m%d%H%M%S') + read -p "Press [ENTER] to move ~/$BASEFILE to ~/$BASEFILE-$DATE. CTRL+C aborts" + mv ~/$BASEFILE ~/$BASEFILE-$DATE + NEWLINK=1 + else + echo "Nothing in location ~/$BASEFILE" + NEWLINK=1 fi - # If dotfile doesn't exist then link it. - if [ ! -e "~/$BASEFILE" ] ; then - echo "ln -s $FILE ~/$BASEFILE" - - else - echo "~/$BASEFILE is already a symlink. Nothing to do." - + # Create a new symlink + if [ $NEWLINK -eq 1 ] ; then + echo "Creating new link to $(pwd)/$BASEFILE from" ~ + ln -s $(pwd)/$BASEFILE ~/$BASEFILE + source ~/$BASEFILE fi fi - done