From 023d74d9dfadef44b7c0cf031f0a2873d376467d Mon Sep 17 00:00:00 2001 From: Mark Riedesel Date: Tue, 7 Nov 2023 13:14:43 -0600 Subject: [PATCH] Add new dotfiles command to bin --- bin/dotfiles | 174 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100755 bin/dotfiles diff --git a/bin/dotfiles b/bin/dotfiles new file mode 100755 index 0000000..c9b20a4 --- /dev/null +++ b/bin/dotfiles @@ -0,0 +1,174 @@ +#!/bin/bash -e +# Installation: +# curl https://raw.github.com/Klowner/dotfiles/main/bin/dotfiles | bash +basedir="${DOTFILES_ROOT:-$HOME/.dotfiles}" +bindir="${DOTFILES_BIN:-$HOME/bin}" +repo="Klowner/dotfiles" +gitbase="git://github.com/${repo}.git" +gitremote="git@github.com:${repo}" +tarball="http://github.com/${repo}/tarball/master" + +function has() { + return $(which $1 &> /dev/null) +} + +function missing() { + ! has $1 +} + +function stat() { + GIT_DIR=${basedir}/.git git diff --stat +} + +function note() { + echo " * $*" +} + +function warn() { + echo " * $*" +} + +function die() { + warn $* + exit 1 +} + +function destination() { + path=$1 + path=${path/\_/.} + path=${path//\_//} + echo $path +} + +function dirname() { + path=$1 + path=${path%"${path##*[!/]}"} + path=${path%/*} + echo $path +} + +function reverse_destination() { + path=$1 + path=${path/\./_/} + echo $path + echo path $path +} + +function link() { + src=$1 + dst=$2 + if [ -e $dst ]; then + if [ -L $dst ]; then + # Symlink already exists + return + else + # Rename existing files with ".old" extension + warn "$dst already exists, renaming to $dst.old" + backup=$dst.old + if [ -e $backup ]; then + die "$backup already exists. Aborting." + fi + mv -v $dst $backup + fi + fi + # Update existing or create new symlink + ln -vsf $src $dst +} + +function bootstrap() { + note "Bootstrapping..." + if missing curl && missing wget; then + die "missing: curl or wget" + fi + if missing git; then + die "missing: git" + fi + + if [ -e $basedir ]; then + # Try to determine if these dotfiles already installed + if [ -e "${basedir}/.git" ]; then + local current_remote=$(GIT_DIR=${basedir}/.git git remote -v | grep origin\.\*\(fetch\) | awk '{print $2}') + if [ $current_remote = $gitremote ]; then + die "Dotfiles already installed!" + fi + fi + exit + warn "$basedir exists, moving to ${basedir}.bak" + mv "${basedir}" "${basedir}.bak" + fi +} + +function install() { + note "Symlinking configuration files..." + for path in _*; do + dst=$(destination $path) + dstdir=$(dirname $dst) + case $path in + .|..|.git) + continue + ;; + *) + echo $basedir/$path $HOME/$dst "$dst $dstdir" + ;; + esac + done + + note "Installing ~/bin script links... ${bindir}" + mkdir -v -p ${bindir} + if [ -d ${bindir} ]; then + for path in ${basedir}/bin/*; do + echo "link" $path ${bindir}/${path##*/} + done + fi + note "Done" +} + +function git_migrate() { + if missing git; then + return + fi +} + +function add() { + path=$1 + dst="${path/"$HOME"/}" # remove leading $HOME from path + dst="${dst/\//}" # remove leading slash + dst="${dst//[.|\/]/_}" # replace slashes with underscores + + note "Adding $path..." + cp -avr $path $basedir/$dst + mv $path $path.old + link $basedir/$dst $path +} + +function info() { + echo "location: ${basedir}" + echo "commands: ${bindir}" + echo "git: ${gitremote}" +} + +# Hopefully detect if this script is being piped to bash +if [ "${0/*\//}" != "dotfiles" ]; then + bootstrap && \ + install && \ + exit +fi + +git_migrate + +cmd=${1:-help} +case $cmd in + install) install;; + add) add ${@:2};; + info) info;; + status) stat;; + help) + echo "Commands:" + echo " - install Install to $basedir" + echo " - add " + echo " - info Print script variables" + echo " - status Show changed items in .dotfiles" + ;; +esac + +# vim:ts=2:sw=2:et: