diff --git a/bin/dotfiles b/bin/dotfiles new file mode 100755 index 0000000..8a76277 --- /dev/null +++ b/bin/dotfiles @@ -0,0 +1,68 @@ +#!/bin/bash + +## +## binaryDiv's dotfiles management script +## + +# Define root dir for dotfiles management (defaults to $HOME, can be overridden +# with environment variables for debugging) +if [ -z "$DOTFILESROOT" ]; then + DOTFILESROOT="$HOME" +fi + +# Set variables +homedir="$DOTFILESROOT" +dotfilesdir="$DOTFILESROOT/.dotfiles" + + +# Check for command +if [ $# -eq 0 ]; then + echo "no command given" + exit 1 +fi + +# Fetch command +cmd=$1 +shift + + +# Parse command +case "$cmd" in + help) + # help: print usage help + echo "help: not implemented yet, sorry /o\\" + exit 1 + ;; + + ls) + # ls: list dotfiles via ls + echo "$dotfilesdir:" + ls -lAh --color=auto "$dotfilesdir/" + ;; + + git) + # git: do git commands (forwards all arguments) + git -C "$dotfilesdir" "$@" + ;; + + createlinks) + # createlinks: create or update links for dotfiles in home dir + + # (Use an extra script for this to separate the management script from + # user specific dotfiles) + if [ -f "$dotfilesdir/createlinks.sh" ]; then + # arguments: home dir and .dotfiles dir + bash "$dotfilesdir/createlinks.sh" "$homedir" "$dotfilesdir" + else + echo "no createlinks.sh found in $dotfilesdir" + exit 1 + fi + ;; + + *) + # unknown command + echo "unknown command '$cmd'" + exit 1 + ;; +esac + diff --git a/createlinks.sh b/createlinks.sh new file mode 100644 index 0000000..6e03b9c --- /dev/null +++ b/createlinks.sh @@ -0,0 +1,46 @@ +#!/bin/bash + +if [ $# -ne 2 ]; then + echo "wrong argument count" +fi + +# Set variables +homedir="$1" +dotfilesdir="$2" + +# Change directory to home dir +cd "$homedir" + +# Define a function that create a link unless it already exists +makelink() { + test $# -eq 2 || { echo "makelink: invalid function call"; exit 1; } + + if [ ! -e "$1" ]; then + echo "can't create link '$2': target '$1' does not exist" + return + fi + + if [ ! -e "$2" ]; then + # Create link + ln -sr "$1" "$2" + elif [ ! -L "$2" ]; then + # A file with the same name exists and it's not a link + # -> warn user! + echo "can't create link '$2': file already exists" + fi +} + +makelink_simple() { + test $# -eq 1 || { echo "makelink_simple: invalid function call"; exit 1; } + + makelink "$dotfilesdir/$1" "$1" +} + +# -- CREATE THE LINKS HERE -- + +# ~/bin +makelink "$dotfiles/bin/" bin + +# dotfiles +makelink_simple .bashrc +