#!/bin/bash
#
# Metadot: a dotfile management system.
#
# Copyright (C) 2013 Silvio Rhatto - rhatto at riseup.net
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License,
# or any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# Set real home folder
if [ ! -z "$PREFIX" ]; then
if [ ! -d "$PREFIX" ]; then
echo "Destination folder not found: $PREFIX"
else
DEST="$PREFIX"
fi
else
DEST="$HOME"
fi
# Parameters
OPT="$1"
DATE="`date +%Y%m%d%I%M%S`"
BASENAME="`basename $0`"
DIRNAME="`dirname $0`"
DOT="$DEST/.dotfiles"
MODULES="$DOT/modules"
BACKUPS="$DEST/.backups/$DATE"
DEFAULT="https://git.fluxo.info/rhatto/dotfiles"
# Make sure we're running git directly and not any existing wrapper
GIT="/usr/bin/git"
# Backup a file
function metadot_backup {
local file="$DEST/$1"
if [ -e "$file" ] || [ -h "$file" ]; then
local folder="$BACKUPS/`dirname $1`"
#echo "Backing up `basename $1`..."
mkdir -p $folder
mv $file $folder
fi
}
# Find contents of a module
function __metadot_find {
local module="$1"
( cd $MODULES/$module && find -name '*.link' -or -name '*.dot.link' ) | grep -v '.git/modules' | sed -e 's|./||'
}
# Load a module
function metadot_load {
local module="$1"
local destname
local dirname
if [ -d "$MODULES/$module" ]; then
echo "Loading module $module..."
for file in `__metadot_find $module`; do
echo "Processing $file..."
# Get the dirname, replacing string.dot with .string
dirname="`echo $file | sed -e 's|\([^/]*\).dot/|.\1/|g'`"
dirname="`dirname $dirname`"
if echo $file | grep -q '.dot.link'; then
destname=".`basename $file .dot.link`"
else
destname="`basename $file .link`"
fi
if [ "$dirname" != "." ]; then
#echo "Creating $DEST/$dirname..."
mkdir -p $DEST/$dirname
else
dirname=""
fi
metadot_backup "$dirname/$destname"
#echo "Installing symlink $dirname/$destname..."
ln -s $MODULES/$module/$file $DEST/$dirname/$destname
done
else
echo "No such module $module"
fi
}
# Load a module
function metadot_deps {
local module="$1"
local destname
local dirname
local deps_to_install
if [ -d "$MODULES/$module" ]; then
echo "Resolving dependencies for $module.."
if [ -e "/etc/debian_version" ] && [ -e "$MODULES/$module/dependencies/debian" ]; then
deps="`grep -v '^#' $MODULES/$module/dependencies/debian | xargs`"
# Remove installed packages from list
for dep in $deps; do
#if ! dpkg-query -W -f='${Status}' $dep | grep -q '^install ok'; then
if ! dpkg -l $dep 2> /dev/null | grep -q "^ii"; then
deps_to_install="$deps_to_install $dep"
fi
done
LC_ALL=C DEBIAN_FRONTEND=noninteractive sudo apt-get install -y $deps_to_install
fi
else
echo "No such module $module"
fi
}
# Create a new module
function metadot_create {
mkdir -p $MODULES/$1
(
cd $MODULES/$1
git init
echo "# $1 dotfile module" > README.md
echo "" >> README.md
echo "This is the repository for $1 configuration." >> README.md
echo "More information at https://git.fluxo.info/metadot" >> README.md
#echo "TODO" > TODO.md
#echo "====" >> TODO.md
#echo "" >> TODO.md
#echo "* Nothing here? :P" >> TODO.md
echo "# As we are handling with config files, it might be better to" > .gitignore
echo "# use a paranoid config by default." >> .gitignore
echo "#" >> .gitignore
echo "# Comment that while in development." >> .gitignore
echo '*' >> .gitignore
cp $DIRNAME/LICENSE .
git add -f .
)
echo "Metadot skeleton module $1 created at $MODULES/$1"
}
# Fetch dotfiles
function metadot_fetch {
if [ -d "$DOT/.git" ]; then
( cd $DOT && $GIT fetch --all && $GIT log --show-signature -n 1 --remotes --branches=origin/master )
fi
}
# Merge
function metadot_merge {
if [ -d "$DOT/.git" ]; then
(
cd $DOT && $GIT merge origin/master && \
$GIT submodule sync --recursive && \
$GIT submodule update --init --recursive #--recommend-shallow
)
fi
}
# Update your dotfiles
function metadot_update {
echo "please run 'metadot fetch && metadot merge' instead"
exit 1
#if [ -d "$DOT/.git" ]; then
# ( cd $DOT && git pull origin master && git submodule update --init --recursive )
#else
# for module in `ls $MODULES`; do
# ( cd $MODULES/$module && git pull origin master && git submodule update --init --recursive )
# done
#fi
}
# Basic usage
function metadot_usage {
echo "usage: $BASENAME