-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.sh
106 lines (89 loc) · 2.32 KB
/
utils.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/env sh
DOT_DIR=~/dotfiles
USER_SOURCE_FILE=~/.profile
# colors
FAIL="\033[0;31m"
PASS="\033[0;32m"
WARN="\033[0;33m"
INFO="\033[0;34m"
NORM="\033[0m"
# Profile target
if [[ $SHELL == *bash* ]]; then
if [[ -f ~/.bashrc ]]; then USER_SOURCE_FILE=~/.bashrc
elif [[ -f ~/.bash_profile ]]; then USER_SOURCE_FILE=~/.bash_profile
elif [[ -f ~/.bash_login ]]; then USER_SOURCE_FILE=~/.bash_login
elif [[ -f ~/.profile ]]; then USER_SOURCE_FILE=~/.profile
fi
elif [[ $SHELL == *zsh* ]]; then USER_SOURCE_FILE=~/.zshrc
fi
touch $USER_SOURCE_FILE
function install_profile {
echo -e "${INFO}check ${NORM}$1 dependencies..."
$SHELL "$DOT_DIR/$1/setup.sh"
variant=$1
profile_filename="$HOME/.dotfiles_$variant"
hook="[[ -f $profile_filename ]] && source $profile_filename # zeachco-dotfiles $variant"
cp "$DOT_DIR/$variant/profile.sh" "$profile_filename"
echo -e "${INFO}link ${NORM}$profile_filename"
echo "$hook" >> $USER_SOURCE_FILE
}
function clean_imports {
cp -f $USER_SOURCE_FILE "$USER_SOURCE_FILE.backup"
sed '/zeachco-dotfiles/d' "$USER_SOURCE_FILE.backup" > $USER_SOURCE_FILE
}
function print_needs {
echo -e "${WARN}missing ${NORM}$1"
}
function print_exists {
echo -e "${PASS}found ${NORM}$1"
}
function exists {
if command -v "$1" >/dev/null 2>&1
then
print_exists $1
return 0
else
print_needs $1
return 1
fi
}
function needs {
if ! command -v "$1" >/dev/null 2>&1
then
print_needs $1
return 0
else
print_exists $1
return 1
fi
}
function install() {
name="$1"
pkg_name="${2:-$1}"
if needs $name
then
echo -e "${WARN}installing ${NORM}$pkg_name..."
sleep 1
if command -v apt &> /dev/null
then
sudo apt install -y $pkg_name
else
if command -v pacman &> /dev/null
then
sudo pacman -S $pkg_name --noconfirm
else
echo -e "${FAIL} I don't know how to install $pkg_name ${NORM}"
fi
fi
fi
}
function script_install() {
name="$1"
exec="$2"
if needs $name
then
echo -e "${WARN}installing ${NORM}$name..."
echo -e "${INFO}running ${NORM}$exec"
eval "$exec"
fi
}