From 248bd93d39adb5276ca789ed012c0d8d2258bc16 Mon Sep 17 00:00:00 2001
From: Salt <rehashedsalt@cock.li>
Date: Sun, 16 Sep 2018 12:13:54 -0500
Subject: [PATCH] replicate.sh: Huge refactor Doesn't change the behavior of
 the script, but does centralize logging and make the code look a lot nicer

---
 replicate.sh | 83 ++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 61 insertions(+), 22 deletions(-)

diff --git a/replicate.sh b/replicate.sh
index 891be5f..78d24a1 100755
--- a/replicate.sh
+++ b/replicate.sh
@@ -1,32 +1,71 @@
 #!/bin/sh
-name="$(basename "$0")"
-deps="git rsync"
-for dep in $deps; do
-	if ! which $dep > /dev/null 2>&1; then
-		printf "$0: error: could not find program \"$dep\" in PATH\n" 1>&2
-		exit 1
-	fi
-done
-mkdir Desktop Documents Downloads Games Music Pictures Projects System Videos
 
-tempfolder="/tmp/dots-`whoami`"
+## Set up some variables
+name="$(basename "$0" .sh)"
+deps="git rsync mktemp basename"
+
+logfile="${PWD}/${name}.log"
+tempfolder="$(mktemp -d)"
 gitdir="$PWD/.dotfiles"
 homedir="$PWD"
 repo='git@gitlab.com:rehashedsalt/home.git'
 
-git clone --recursive --depth 100 --separate-git-dir="$gitdir" "$repo" "$tempfolder"
+## Define some functions
+log() {
+	[ -z ${1+x} ] && return 1
+	printf "${name}: $1\n"
+}
+
+validatedep() {
+	if ! which $1 > /dev/null 2>&1; then
+		return 1
+	fi
+	return 0
+}
+
+## Do the do
+# Rotate the logfile, if necessary
+if [ -f "$logfile" ]; then
+	log "Found old logfile. Rotating"
+	mv "$logfile" "$logfile.old"
+fi
+# Ensure we have the proper dependencies
+log "Validating dependencies"
+for dep in $deps; do
+	if ! validatedep "$dep"; then
+		log "Could not find critical dependency \"$dep\""
+		exit 1
+	fi
+done
+
+# Build Home folder skeleton
+log "Building skeleton folder layout"
+mkdir Desktop Documents Downloads Games Music Pictures Projects System Videos
+
+# Clone the dotfiles
+log "Cloning repository"
+git clone --recursive --depth 100 --separate-git-dir="$gitdir" "$repo" "$tempfolder" >> "$logfile" 2>&1
 error="$?"
-if ! [ "$error" == "0" ]; then
-	printf "$name: error: failed cloning repository \"$repo\": $error\n" 1>&2
-	printf "Do you have the appropriate permissions? Does the remote host know your key?\n"
+if ! [ "$error" -eq "0" ]; then
+	log "Failed cloning repository \"$repo\": git returned $error"
+	log "See $logfile for details"
+	log "Do you have the appropriate permissions? Does the remote host know your key?"
 	exit 2
 fi
-rsync -rvl --exclude ".git" $tempfolder/ $homedir/
-rm -r $tempfolder
-git --git-dir="$gitdir" --work-tree="$homedir" submodule update --init --recursive --remote
-git --git-dir="$gitdir" --work-tree="$homedir" config status.showUntrackedFiles no
-printf "$name: dotfiles set up successfully\n"
-printf "Be sure and configure the following before issuing any commits:\n"
-printf "    git config --global user.email\n"
-printf "    git config --global user.name\n"
+# Move them to where they should be
+log "Moving cloned repo from \"$tempfolder\" into \"$homedir\""
+rsync -rvl --exclude ".git" "$tempfolder/" "$homedir/" >> "$logfile"
+rm -r "$tempfolder"
+rmdir "$tempfolder"
+# Finish syncing
+log "Updating submodules and performing basic configuration"
+git --git-dir="$gitdir" --work-tree="$homedir" submodule update --init --recursive --remote >> "$logfile" 2>&1
+git --git-dir="$gitdir" --work-tree="$homedir" config status.showUntrackedFiles no >> "$logfile" 2>&1
+
+# Print some post-install instructions
+log "Dotfiles set up successfully"
+log "Be sure and configure the following before issuing any commits:"
+log "    git config --global user.email"
+log "    git config --global user.name"
+log "You may now delete the script"
 exit 0