commit 4cf6b17654dd6209367c3303cce464f106f27f07
parent feeb4c4a193a29359cdff132e69a35efddcad9bf
Author: Alex Balgavy <a.balgavy@gmail.com>
Date: Sun, 3 Nov 2019 00:52:16 -0400
conf: pretty much done
There are still a few more features it should have, but it seems to be
working decently.
Former-commit-id: f7a84e0299eb26094242182b53d4a5165e8cda6a
Diffstat:
2 files changed, 95 insertions(+), 22 deletions(-)
diff --git a/map.conf b/dotmap.conf
diff --git a/scripts/conf b/scripts/conf
@@ -1,38 +1,111 @@
-#!/bin/bash
+#!/usr/bin/env bash
die() {
echo "$1" >&2
exit 1
}
[ -z "$DOTFILES" ] && die '$DOTFILES variable not set.'
-cd "$DOTFILES"
-
-[ -f "./map.conf" ] || die "map.conf file not present in $DOTFILES"
-
-nested_dir=""
+cd "$DOTFILES" || die "Can't read $DOTFILES"
link() {
- echo "$1 => $2"
+ if [ -e "$2" ]; then
+ echo "$2 already exists, renaming to $2.bak"
+ mv "$2" "$2.bak"
+ fi
+ ln -svf "$(pwd)/$1" "$2"
}
-# TODO: handle linking a specific thing, check if stuff exists, handle multiple nesting levels
-while read -r map; do
- if [ ! -z "$map" ] && [[ ! "$map" == "#"* ]]; then
- mapping=($(echo "$map" | sed 's/^ *- /-/' | awk -F ': ' '{ print $1 " " $2 }'))
+# TODO: handle multiple nesting levels, support unlinking
- # Top level items
- if [[ ! "${mapping[0]}" == "-"* ]]; then
- if [ -z "${mapping[1]}" ]; then
- nested_dir="${mapping[0]/:/}"
+declare -A mappings
+parse_mapfile() {
+ if [ ! -f "$1" ]; then
+ die "Mapfile $1 does not exist."
+ fi
+ local nestdir=""
+ local lineno=1
+ homedir="$(echo -n "$HOME" | tr -d '\n\r')"
+ while read -r map; do
+ if [ ! -z "$map" ] && [[ ! "$map" == "#"* ]]; then
+ mapping=($(echo -n "$map" | sed -e 's/^ *- /-/' -e "s:~:$homedir:" | awk -F ': ' '{ print $1 " " $2 }'))
+
+ # Top level items
+ if [[ ! "${mapping[0]}" == "-"* ]]; then
+ nestdir=""
+ if [ -z "${mapping[1]}" ]; then
+ # nestdir
+ nestdir="${mapping[0]/://}"
+ else
+ # one-off mapping
+ if [ ! -e "$nestdir${mapping[0]}" ]; then
+ die "error in mapfile: $nestdir${mapping[0]} does not exist (line $lineno)"
+ fi
+ mappings["$nestdir${mapping[0]}"]="${mapping[1]}"
+ fi
else
- nested_dir=""
- link "${mapping[0]}" "${mapping[1]}"
+ if [ ! -e "$nestdir${mapping[0]/-/}" ]; then
+ die "error in mapfile: $nestdir${mapping[0]/-/} does not exist (line $lineno)"
+ fi
+ mappings["$nestdir${mapping[0]/-/}"]="${mapping[1]}"
fi
- # Nested items
- else
- link "$nested_dir/${mapping[0]/-/}" "${mapping[1]}"
fi
- fi
-done < <(cat "./map.conf")
+ ((lineno++))
+ done < <(cat "$1")
+}
+
+parse_mapfile "./dotmap.conf"
+
+PARAMS=""
+list_only=0
+while (( "$#" )); do
+ case "$1" in
+ -l|--list)
+ list_only=1
+ shift
+ ;;
+
+ -h|--help)
+ echo "Usage:"
+ echo "-l, --list only list maps"
+ exit 0
+ ;;
+ --) # end arg parsing
+ shift
+ break
+ ;;
+ -*) # unsupported flags
+ echo "Unsupported flag $1" >&2
+ exit 1
+ ;;
+ *) # preserve positional arguments
+ PARAMS="$PARAMS $1"
+ shift
+ ;;
+ esac
+done
+eval set -- "$PARAMS"
+if [ $# -eq 0 ] && [ $list_only -eq 0 ]; then
+ read -srp "Link all dotfiles?" -n 1 -s conf
+ case "$conf" in
+ Y|y)
+ echo
+ for f in "${!mappings[@]}"; do link "$f" "${mappings[$f]}"; done
+ ;;
+ esac
+elif [ $list_only -eq 1 ]; then
+ for f in "${!mappings[@]}"; do echo "$f => ${mappings[$f]}"; done
+else
+ for i in $@; do
+ if ( IFS=$'\n'; echo "${mappings[*]}" ) | grep -vqFx "$i"*; then
+ for f in "${!mappings[@]}"; do
+ if [[ "$f" == "$i"* ]]; then
+ link "$f" "${mappings[$f]}"
+ fi
+ done
+ else
+ die "Error: $i not present in mapfile, don't know how to link."
+ fi
+ done
+fi