aboutsummaryrefslogtreecommitdiff
path: root/make_links
blob: 4b6babbb6d83cb70ec8d4f01f18a5fdecaf89e2f (plain)
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh
# vim: ts=4:sw=4:sts=4
#############################################################################
# Copyright 2021 Christopher Nelson <christopher.nelson@languidnights.com>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#############################################################################

BASEDIR="$(cd "$(dirname "$0")" && pwd )"

mkdir -p $HOME/.config
mkdir -p $HOME/.local/share
mkdir -p $HOME/.local/bin

linker()
{
	local destroy="$1"

	while read -r dotfile
	do
		# Skip blank lines
		[ -z "$dotfile" ] && continue

		initial_char=$(echo $dotfile | cut -c1-1)
		# Skip lines with a '#'
		[ $initial_char = '#' ] && continue
		echo "linking $BASEDIR/$dotfile to $HOME/$dotfile"
		if [ -f "$HOME/$dotfile" ]; then
			if [ $destroy = "true" ]; then
				echo "removing $HOME/$dotfile"
				rm "$HOME/$dotfile"
			elif [ -L $HOME/$dotfile ]; then
				echo "$HOME/$dotfile is already a link, skipping"
				continue
			else
				echo "$HOME/$dotfile is a regular file, skipping"
				continue
			fi
		elif [ -d "$HOME/$dotfile" ]; then
			if [ -L "$HOME/$dotfile" ]; then
				if [ $destroy = "true" ]; then
					echo "removing $HOME/$dotfile"
					rm "$HOME/$dotfile"
				else [ -L "$HOME/$dotfile" ]
					echo "$HOME/$dotfile is already a link, skipping"
					continue
				fi
			elif [ $destroy = "true" ]; then
				echo "removing $HOME/$dotfile"
				rm -r "$HOME/$dotfile"
			else
				echo "$HOME/$dotfile already exists, skipping"
				continue
			fi
		fi

		if [ ! -d $(dirname "$HOME/$dotfile") ]; then
			echo "dir does not exist $(dirname "$HOME/$dotfile")"
			mkdir -p $(dirname "$HOME/$dotfile")
			echo "creating directory"
		fi
		
		ln -s $BASEDIR/$dotfile $HOME/$dotfile
		echo "linked $BASEDIR/$dotfile to $HOME/$dotfile"

	done < $BASEDIR/linked_files
}

convert_readme()
{
	if [ $(command -v rst2html5) != "" ]; then
		rst2html5 $BASEDIR/README.rst > $BASEDIR/README.html
	fi
}

help()
{
	echo "make_links will link out the contents of \"linked_files\" to your home dir"
	echo
	echo "Syntax: make_links [-d|-h|-l|-n|-m]"
	echo "options:"
	echo "  -d, --destructive        Destructively link (removes existing symlinks)"
	echo "  -h, --help               Print this help"
	echo "  -l, --license            Print the license for make_links"
	echo "  -n, --nondestructive     Non-destructively link (existing files and links will be left alone)"
	echo "  -m, --make-documentation Converts documentation READMEs from rst to HTML"
	echo
}

license()
{
	$PAGER $BASEDIR/COPYING
}

for arg in "$@"; do
	shift
	case "$arg" in
		"--help") help; exit 0;;
		"-h") help; exit 0;;
		"--destructive") linker true; exit 0;;
		"-d") linker true; exit 0;;
		"--nondestructive") linker false; exit 0;;
		"-n") linker false; exit 0;;
		"--license") license; exit 0;;
		"-l") license; exit 0;;
		"--make-documentation") convert_readme; exit 0;;
		"-m") convert_readme; exit 0;;
	esac
done

help