-
Notifications
You must be signed in to change notification settings - Fork 0
/
fd-install.sh
142 lines (109 loc) · 3.21 KB
/
fd-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
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
#!/bin/bash
#
# fd install script
#
# To install:
#
# $ source fd-install.sh [--bashrc | --bash_profile | --profile]
#
# By default, this script will install to ~/.bash_profile, unless it looks like
# ~/.bash_profile sources ~/.bashrc (recommended), in which case we install to ~/.bashrc.
# You may stipulate the exact install location by providing the appropriate flag.
#
WITH_CDD=false
FDRC="$HOME/.fdrc"
# Get the path to the directory this script was ran from
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
# Presume that we will install to .bash_profile. If it looks like
# .bash_profile is including .bashrc, then we will install to .bashrc.
INSTALL_TO=".bash_profile"
if [ ! -f "$INSTALL_TO" ] || grep -q bashrc "$HOME/$INSTALL_TO" ; then
INSTALL_TO=".bashrc"
fi
# Parse options
while [ $# -gt 0 ] ; do
option=$1
shift
case "$option" in
--with-cdd )
WITH_CDD=true
;;
--no-cdd )
WITH_CDD=false
;;
--bashrc )
INSTALL_TO=".bashrc"
;;
--bash_profile )
INSTALL_TO=".bash_profile"
;;
--profile )
INSTALL_TO=".profile"
;;
esac
done
# Disable requested install of cdd if it is already present
if $WITH_CDD && grep -q cddrc "$HOME/$INSTALL_TO" ; then
echo 'Skipping install of cdd, as it is already present.'
fi
# Install cdd first, if requested
if $WITH_CDD ; then
CDDRC="$HOME/.cddrc"
CDD_PATH="$(dirname -- $SCRIPT_DIR)/cdd"
git clone https://github.com/scriptworld/cdd.git "$CDD_PATH"
# Write the sourcing of the cdd scripts to our .cddrc file
cat <<- __EOF__ > $CDDRC
# Source the cdd script
source "$CDD_PATH/cdd.sh"
source "$CDD_PATH/cdd-completion.bash"
__EOF__
echo 'Created new ~/.cddrc configuration file.'
cat <<- __EOF__ >> "$HOME/$INSTALL_TO"
# Source the cdd configuration file.
# See: https://github.com/scriptworld/cdd
source "$HOME/.cddrc"
__EOF__
echo "Installed 'source ~/.cddrc' in ~/$INSTALL_TO"
# Source cdd so that it is available in this shell
source ~/.cddrc
fi
# Do not overwrite existing installations
if [ -f "$FDRC" ] ; then
echo "fd is already installed (~/.fdrc file exists)"
return
fi
# Source the fd-suggest.sh file so that function is available
source "$SCRIPT_DIR/fd-suggest.sh"
# Write the header of our .fdrc file
cat <<- __EOF__ > $FDRC
#!/bin/bash
#
# Configuration file for fd script
# See: https://github.com/g1a/fd
#
# Use fd-suggest to refresh the FDPATH setting below.
source "$SCRIPT_DIR/fd-suggest.sh"
# List directory search location.
# Customize to suit
__EOF__
# Add the initial `fd-suggest` list to the configuration file
fd-suggest >> $FDRC
# Write the sourcing of the fd scripts to our .fdrc file
cat <<- __EOF__ >> $FDRC
# Source the fd script
source "$SCRIPT_DIR/fd.sh"
__EOF__
echo 'Created new ~/.fdrc configuration file.'
# If it looks like the fdrc file is already being sourced, then exit.
if grep -q fdrc "$HOME/$INSTALL_TO" ; then
echo "~/.fdrc configuration file is already sourced from ~/$INSTALL_TO)"
return
fi
cat <<- __EOF__ >> "$HOME/$INSTALL_TO"
# Source the fd configuration file.
# See: https://github.com/g1a/fd
source "$HOME/.fdrc"
__EOF__
echo "Installed 'source ~/.fdrc' in ~/$INSTALL_TO"
# Source fd so that it is available in this shell.
source ~/.fdrc