Add new dotfiles command to bin
This commit is contained in:
parent
35f5e8472b
commit
023d74d9df
1 changed files with 174 additions and 0 deletions
174
bin/dotfiles
Executable file
174
bin/dotfiles
Executable file
|
@ -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 "[32;1m * [0m$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
function warn() {
|
||||||
|
echo "[31;1m * [0m$*"
|
||||||
|
}
|
||||||
|
|
||||||
|
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 <path>"
|
||||||
|
echo " - info Print script variables"
|
||||||
|
echo " - status Show changed items in .dotfiles"
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# vim:ts=2:sw=2:et:
|
Loading…
Add table
Add a link
Reference in a new issue