aboutsummaryrefslogtreecommitdiff
path: root/install.sh
blob: be34753b5c9f51f1dedbe134cc19aa2fd39bb557 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/bin/bash

#['name']='install location'
declare -A CONFIGS
CONFIGS=( 	["sway"]=".config"
		["alacritty"]=".config"
		["polybar"]=".config"
		["powerline"]=".config"
		["nvim"]=".config"
		["termite"]=".config"
		["twmn"]=".config"
		[".i3"]="."
		[".vim"]="."
		[".xinitrc"]="."
		[".compton.conf"]="."
		[".bashrc"]="."
		[".Xresources"]="."
		[".radare2rc"]="."
		[".bash_profile"]="." )

#1: message
yes_no()
{
	read -p "$1 (y/[n])" inp
	case $inp in
		[yY]* ) return 0;;
		* ) 	return 1;;
	esac
}

#interface on sterr
multiselector() {
	local argc=$#
	local argv=($@)
	local cnt=0
	local ret=""

	for entry in "$@"; do
		>&2 echo "$cnt) $entry"
		((cnt=$cnt + 1))
	done

	>&2 echo "Select entry(s)"
	>&2 echo "eg. '1 3' '1-4' 'a'"
	>&2 read -p " > " inp

	for sel in $inp; do
		local reg_range="^[0-9]+\-[0-9]+$"
		local reg_single="^[0-9]+$"
		local reg_all="^[aA]*"

		if [[ $sel =~ $reg_range ]]; then
			range=($(echo $sel | tr "-" " "))
			for (( i=${range[0]}; i<=${range[1]}; i++ )); do
				ret="$ret ${argv[$i]}"
			done
		elif [[ $sel =~ $reg_single ]]; then
			ret="$ret ${argv[$sel]}"
		elif [[ $sel =~ $reg_all ]]; then
			ret=$@
			break;
		else
			>&2 echo "Wrong input at \"$sel\""
			ret=""
			break
		fi
	done
	echo $ret
}

selector()
{
	local regex="^-?[0-9]+\$"
	local cnt=0
	for selection in "$@"
	do
		echo "$cnt) $selection"
		((cnt=$cnt + 1))
	done

	read -p "(default=0) >" inp
	if [[ "$inp" =~ $regex ]] && [ $inp -ge 0 -a $inp -le $# ]
	then
		echo $inp
		return $inp
	elif [ -z $inp ]
	then
		return 0
	else
		return 2
	fi
}

#1: source 2: destination
link()
{
	if [ -e $2 ]; then
		if yes_no "$(basename $2) exists. Overwrite?"; then
			if [ -d $2 ]; then
				rm -R $2
			else
				rm $2
			fi
		else
			return
		fi
	fi

	ln -s "$1" "$2"
}

if [ $# -gt 0 ]
then
	for i in "$@"
	do
		echo "Install $i to"
		if [ -e $i ]
		then
			selector "~" "~/.config" "Custom location" "Abort"
			case $? in
			0)
				export INSTPATH="$HOME";;
			1)
				export INSTPATH="$HOME/.config";;
			2)
				echo custom
				continue;;
			*)
				echo Abort.
				continue;;
			esac
			echo aaa $INSTPATH/$i
			link $(pwd)/$i $INSTPATH/$(basename $i)
			unset INSTPATH
		else
			echo $i does not exist. Skipping.
		fi

	done
	exit 0
fi

WORKDIR=$(dirname $0)
cd $WORKDIR
echo Working in $WORKDIR
echo Homedir is $HOME

git submodule init
git submodule update

selected=( $(multiselector ${!CONFIGS[@]}) )

for cnf in "${selected[@]}"; do
	echo "$(pwd)/$cnf  $HOME/${CONFIGS[$cnf]}/$cnf"
	link "$(pwd)/$cnf" "$HOME/${CONFIGS[$cnf]}/$cnf"
done

#.files is used to tell scripts where to look for the dotfiles
if yes_no "Generate '.files'?"; then
	echo "DOTFILEBASE=\"$(pwd)\"" > $HOME/.files
fi