|
| 1 | +#!/bin/bash -e |
| 2 | + |
| 3 | +fail() { |
| 4 | + >&2 cat |
| 5 | + exit 1 |
| 6 | +} |
| 7 | + |
| 8 | +if [ $# -ne 0 ]; then |
| 9 | + fail << EOF |
| 10 | +Usage: ${0##*/} |
| 11 | +
|
| 12 | +Options: |
| 13 | +
|
| 14 | + HTTPS=0|1 |
| 15 | + AUTO_COMPLETION=1|0 |
| 16 | + CONFIRM=0|1 |
| 17 | +
|
| 18 | +Installs cppsm command by cloning its repository and appending the necessary |
| 19 | +changes to the user's Bash startup script. |
| 20 | +EOF |
| 21 | +fi |
| 22 | + |
| 23 | +if [ -e "$HOME/.cppsm" ]; then |
| 24 | + fail << EOF |
| 25 | +ERROR: .cppsm already exists in your \$HOME. |
| 26 | +EOF |
| 27 | +fi |
| 28 | + |
| 29 | +contains() { |
| 30 | + local PATTERN="$1" |
| 31 | + local FILE="$2" |
| 32 | + [ -e "$FILE" ] && grep -q -e "$PATTERN" "$FILE" |
| 33 | +} |
| 34 | + |
| 35 | +for SCRIPT in .bash_profile .bashrc; do |
| 36 | + if contains CPPSM "$HOME/$SCRIPT"; then |
| 37 | + fail << EOF |
| 38 | +ERROR: CPPSM already appears in your $SCRIPT. |
| 39 | +EOF |
| 40 | + fi |
| 41 | + if [ -e "$HOME/$SCRIPT" ]; then |
| 42 | + TARGET_SCRIPT="$SCRIPT" |
| 43 | + fi |
| 44 | +done |
| 45 | + |
| 46 | +if [ -z "$TARGET_SCRIPT" ]; then |
| 47 | + fail << EOF |
| 48 | +ERROR: Neither .bash_profile nor .bashrc exists. |
| 49 | +EOF |
| 50 | +fi |
| 51 | + |
| 52 | +COMMANDS="$(cat << EOF |
| 53 | +CPPSM="\$HOME/.cppsm" |
| 54 | +export PATH="\$CPPSM/bin:\$PATH" |
| 55 | +EOF |
| 56 | +)" |
| 57 | + |
| 58 | +if [ "$AUTO_COMPLETION" != 0 ]; then |
| 59 | + COMMANDS="$(cat << EOF |
| 60 | +$COMMANDS |
| 61 | +. "\$CPPSM/bash_completion" |
| 62 | +EOF |
| 63 | +)" |
| 64 | +fi |
| 65 | + |
| 66 | +SNIPPET="$(cat << EOF |
| 67 | +# CPPSM lines added by the C++ submodule manager 'install' script. |
| 68 | +# Uninstall by deleting the CPPSM lines and the \$CPPSM directory. |
| 69 | +$COMMANDS |
| 70 | +EOF |
| 71 | +)" |
| 72 | + |
| 73 | +if [ "$HTTPS" = 1 ] || [ "$TRAVIS" = true ]; then |
| 74 | + URL="https://github.com/cppsm/cppsm-cli.git" |
| 75 | +else |
| 76 | + URL= "[email protected]:cppsm/cppsm-cli.git" |
| 77 | +fi |
| 78 | + |
| 79 | +# shellcheck disable=2001 |
| 80 | +cat << EOF |
| 81 | +This will install the C++ submodule manager 'cppsm' command by running |
| 82 | +
|
| 83 | + git clone --quiet $URL "\$HOME/.cppsm" |
| 84 | +
|
| 85 | +and appending |
| 86 | +
|
| 87 | +$(echo "$SNIPPET" | sed 's#^# #') |
| 88 | +
|
| 89 | +to \$HOME/$TARGET_SCRIPT. |
| 90 | +
|
| 91 | +EOF |
| 92 | + |
| 93 | +if [ "$CONFIRM" = 1 ]; then |
| 94 | + read -p "Continue (y/n)? " -n 1 -r && echo |
| 95 | + if ! [[ $REPLY =~ ^[Yy]$ ]]; then |
| 96 | + fail <<EOF |
| 97 | +Installation cancelled. |
| 98 | +EOF |
| 99 | + fi |
| 100 | + echo |
| 101 | +fi |
| 102 | + |
| 103 | +if ! git clone --quiet $URL "$HOME/.cppsm"; then |
| 104 | + fail << EOF |
| 105 | +ERROR: Failed to git clone the cppsm-cli. |
| 106 | +EOF |
| 107 | +fi |
| 108 | + |
| 109 | +augment-script() { |
| 110 | + cat << EOF >> "$HOME/$TARGET_SCRIPT" |
| 111 | +
|
| 112 | +$SNIPPET |
| 113 | +EOF |
| 114 | +} |
| 115 | + |
| 116 | +if ! augment-script; then |
| 117 | + fail <<EOF |
| 118 | +ERROR: Failed to append to \$HOME/$TARGET_SCRIPT. |
| 119 | +EOF |
| 120 | +fi |
| 121 | + |
| 122 | +cat <<EOF |
| 123 | +C++ submodule manager 'cppsm' command has been installed! |
| 124 | +
|
| 125 | +To use the 'cppsm' command you now need to restart the shell or execute the |
| 126 | +following commands in this shell: |
| 127 | +
|
| 128 | +$COMMANDS |
| 129 | +EOF |
0 commit comments