#!/usr/bin/env bash TRACKED_FILES=( bin/ dot_config/ dot/gitconfig dot/tmux.conf dot/mbsyncrc dot_gnupg/gpg-agent.conf dot_ssh/config dot_mblaze/mblaze dot/msmtprc dot/npmrc ) TRACKED_LOCATIONS=( ~/bin/ ~/.config/ ~/.gitconfig ~/.tmux.conf ~/.mbsyncrc ~/.gnupg/gpg-agent.conf ~/.ssh/config ~/.mblaze/profile ~/.msmtprc ~/.npmrc ) find_files() { if [[ ${TRACKED_FILES[$1]} == */ ]]; then find "$PWD/${TRACKED_FILES[$1]}" -type d -exec find {} -maxdepth 1 -type f \; | sed "s|$PWD/${TRACKED_FILES[$1]}||" | sed "s|^/||" fi } link_file() { if [[ ! -L $2 && ! -f $2 && ! -d $2 ]]; then echo "Creating a link from $1 to $2" mkdir -p $2 rmdir $2 ln -s $1 $2 fi } export -f link_file unlink_file() { if [[ -L $1 ]]; then echo "Unlinking $1" unlink "$1" fi } export -f unlink_file move_file() { if [[ ! -L $1 ]] && [[ -f $1 || -d $1 ]]; then echo "Moving $1 to backup/$1" mkdir -p "backup/$1" rmdir "backup/$1" mv $1 "backup/$1" fi } export -f move_file for ((i = 0; i < ${#TRACKED_FILES[@]}; ++i)); do if [ "$1" == "cleanup" ]; then unlink_file "${TRACKED_LOCATIONS[$i]}" find_files "$i" | xargs -I {} bash -c "unlink_file ${TRACKED_LOCATIONS[$i]}{}" continue fi if [ "$1" == "override" ]; then move_file ${TRACKED_LOCATIONS[$i]} find_files "$i" | xargs -I {} bash -c "move_file ${TRACKED_LOCATIONS[$i]}{}" fi if [ "$1" != "cleanup" ]; then link_file "$PWD/${TRACKED_FILES[$i]}" "${TRACKED_LOCATIONS[$i]}" find_files "$i" | xargs -I {} bash -c "link_file $PWD/${TRACKED_FILES[$i]}{} ${TRACKED_LOCATIONS[$i]}{}" fi done