#!/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"
# 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
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`"
LC_ALL=C DEBIAN_FRONTEND=noninteractive sudo apt-get install -y $deps
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
git add .
)
echo "Metadot skeleton module $1 created at $MODULES/$1"
}
# Update your dotfiles
function metadot_update {
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