-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathinstall.sh
executable file
·66 lines (56 loc) · 1.76 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
#!/usr/bin/env bash
# Get install paths.
DEFAULT_INSTALL_DIR=${XDG_DATA_HOME:-$HOME/.local/share}/shunpo
read -p "Enter the installation directory [default: $DEFAULT_INSTALL_DIR]: " user_input
INSTALL_DIR=${user_input:-"$DEFAULT_INSTALL_DIR"}
SCRIPT_DIR=${INSTALL_DIR}/scripts/
BASHRC="$HOME/.bashrc"
# File containing command definitions.
SHUNPO_CMD="$INSTALL_DIR/shunpo_cmd"
setup() {
mkdir -p $INSTALL_DIR
mkdir -p $SCRIPT_DIR
if [ -f $SHUNPO_CMD ]; then
rm $SHUNPO_CMD
fi
touch $SHUNPO_CMD
}
add_commands() {
# Define command set.
SCRIPT_DIR="$(realpath "$SCRIPT_DIR")"
cat >"$SHUNPO_CMD" <<EOF
#!/usr/bin/env bash
sj() { source "$SCRIPT_DIR/jump_to_parent.sh" "\$@"; }
sd() { source "$SCRIPT_DIR/jump_to_child.sh"; }
sb() { "$SCRIPT_DIR/add_bookmark.sh"; }
sr() { "$SCRIPT_DIR/remove_bookmark.sh" "\$@"; }
sg() { source "$SCRIPT_DIR/go_to_bookmark.sh" "\$@"; }
sl() { "$SCRIPT_DIR/list_bookmarks.sh"; }
sc() { "$SCRIPT_DIR/clear_bookmarks.sh"; }
EOF
}
install() {
# Store scripts in SCRIPTS_DIR.
cp src/* $SCRIPT_DIR
# Add sourcing for shunpo_cmd (overwrite).
source_rc_line="source $SHUNPO_CMD"
temp_file=$(mktemp)
sed '/^source.*\shunpo_cmd/d' "$BASHRC" >"$temp_file"
mv "$temp_file" "$BASHRC"
echo "$source_rc_line" >>"$BASHRC"
echo "Added to BASHRC: $source_rc_line"
# Record SHUNPO_DIR for uninstallation (overwrite).
install_dir_line="export SHUNPO_DIR=$INSTALL_DIR" >>"$BASHRC$"
temp_file=$(mktemp)
grep -v '^export SHUNPO_DIR=' "$BASHRC" >"$temp_file"
mv "$temp_file" "$BASHRC"
echo "$install_dir_line" >>"$BASHRC"
echo "Added to BASHRC: $install_dir_line"
add_commands
}
# Install.
echo "Installing."
setup
install
echo "Done."
echo "(Remember to run source ~/.bashrc.)"