-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·92 lines (80 loc) · 2.52 KB
/
install.sh
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
#!/bin/sh
set -e # -e: exit on error
usage="Usage: `basename $0` [-c|-l] [-h]
Install source:
Default: decided automatically
-c, --clone: clone from GitHub
-l, --local: install from the script's directory\n"
# Get args
# Compounded options (i.e. -abc) would require a special case
for i in $@; do
arg=`echo $i | tr "[:upper:]" "[:lower:]"`
case $arg in
-c | --clone )
if [ ! -z $arg_source ]; then
echo "error: multiple install source statements" >&2
exit 1
fi
arg_source=clone
;;
-l | --local )
if [ ! -z $arg_source ]; then
echo "error: multiple install source statements" >&2
exit 1
fi
arg_source=local
;;
-h )
printf "POSIX install script for logicer's dotfiles.\nLearn more: https://github.com/Logicer16/dotfiles\n\n"
printf "$usage"
exit 0
;;
* )
printf "$usage" >&2
exit 1
;;
esac
done
# Download chezmoi for bootstraping if required
if [ ! "`command -v chezmoi`" ]; then
bin_dir="$HOME/.local/bin"
chezmoi="$bin_dir/chezmoi"
if [ "`command -v curl`" ]; then
sh -c "`curl -fsSL https://git.io/chezmoi`" -- -b "$bin_dir"
elif [ "`command -v wget`" ]; then
sh -c "`wget -qO- https://git.io/chezmoi`" -- -b "$bin_dir"
else
echo "error: cannot install chezmoi. You must have curl or wget installed." >&2
exit 1
fi
else
chezmoi=chezmoi
fi
# Decide source
if [ "$arg_source" != "clone" ]; then
# POSIX way to get script's dir: https://stackoverflow.com/a/29834779/12156188
# Looks like garbage but blame the bourne shell's syntax for not having $()
script_dir="`cd -P -- \"\`dirname -- \\\"\\\`command -v -- \\\\\"$0\\\\\"\\\`\\\"\`\" && pwd -P`"
# Check if in a git repo and if it's the right git repo
git_url="`$chezmoi git -S $script_dir -- config --get remote.origin.url 2> /dev/null | tr "[:upper:]" "[:lower:]" || true`"
# Remove the ".git" suffix, if present
if echo $git_url | grep -q "\.git"; then
git_url=`echo $git_url | sed 's/\(.*\)\(\.git\)/\1/'`
fi
if [ "${git_url%.git}" = "https://github.com/logicer16/dotfiles" ]; then
# update git repo
$chezmoi git -S $script_dir -- pull
source="--source=$script_dir"
else
if [ "$arg_source" = "local" ]; then
echo "error: not in valid git repository" >&2
exit 1
fi
fi
fi
# Pass unattended status
if [ ! -t 0 ] || [ ! -t 1 ]; then
UNATTENDED=true
fi
# exec: replace current process with chezmoi init
exec env UNATTENDED=$UNATTENDED "$chezmoi" init --apply ${source:-"logicer16"}