/stdhome

To get this branch, use:
bzr branch http://bzr.ed.am/stdhome
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/bin/bash

# fix up home dir
if [ ! -d ~/.stdhome -a ! -d ~/.stdhome.dev -a ! -d ~/.stdhome.orig ]; then
	mkdir ~/.stdhome
fi
if [ ! -d ~/.stdhome ]; then
	echo "Can't determine current state: no ~/.stdhome"
	exit 1
fi

# current state
CURRENT=
if [ -d ~/.stdhome.orig -a ! -d ~/.stdhome.dev ]; then
	CURRENT=on
elif [ ! -d ~/.stdhome.orig ]; then
	CURRENT=off
fi
if [ -z "$CURRENT" ]; then
	echo "Can't determine current state" >&2
	exit 1
fi

# target
TARGET=
if [ "$1" == "on" ]; then
	TARGET=on
elif [ "$1" == "off" ]; then
	TARGET=off
fi

if [ "$TARGET" == "on" ]; then

	# already on?
	if [ "$CURRENT" == "on" ]; then
		echo already on >& 2
		exit 0
	fi
	echo turning on

	# create/check rc
	if [ ! -f ~/.stdhomerc ]; then
		echo "No ~/.stdhomerc, creating..."
		echo "[stdhome]" >> ~/.stdhomerc
		echo "#home-dir = ~/tmp/stdhome-dev" >> ~/.stdhomerc
		mkdir -p ~/tmp/stdhome-dev
	fi
	if ! grep -q '^#home-dir ?= ?~/tmp/stdhome-dev' ~/.stdhomerc; then
		echo "No commented home-dir in ~/.stdhomerc"
		echo "Please add the following to the [stdhome] section:"
		echo "#home-dir = ~/tmp/stdhome-dev"
		exit 1
	fi

	# turn on
	mv ~/.stdhome{,.orig}
	if [ -d ~/.stdhome.dev ]; then
		mv ~/.stdhome{.dev,}
	else
		mkdir ~/.stdhome
		echo new dev environment, use stdhome init
	fi
 	sed -ie 's/^#\(home-dir\)/\1/' ~/.stdhomerc
	mkdir -p ~/tmp/stdhome-dev

elif [ "$TARGET" == "off" ]; then

	# already off?
	if [ "$CURRENT" == "off" ]; then
		echo already off >& 2
		exit 0
	fi
	echo turning off

	# check rc
	if ! grep -q '^home-dir ?= ?~/tmp/stdhome-dev' ~/.stdhomerc; then
		echo "No uncommented home-dir in ~/.stdhomerc"
		exit 1
	fi

	# turn off
	mv ~/.stdhome{,.dev}
	mv ~/.stdhome{.orig,}
	sed -ie 's/^home-dir/#\0/' ~/.stdhomerc

else
	echo currently $CURRENT
fi