summaryrefslogtreecommitdiff
path: root/install.sh
blob: 9138eff8b9080eee8f9831232e458953e99faf7b (plain)
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
#!/bin/sh
# Copyright Zach Smith, MIT (See LICENSE-MIT)

# adds my other script paths (except volsv)

SSH_BASE_URL="git@git.zachir.xyz:zachir/"
HTTP_BASE_URL="https://git.zachir.xyz/"

SCRIPT_DIRS="$(cat << 'EOF'
blocks
generics
scripts
shortcmds
sp
volsv
EOF
)"

UPDATE=""

printhelp () {
  printf "install.sh | Used to add my other script repos as submodules. Feel free to modfy it to clone yours as well.\n"
  printf "  -s) clone repos using ssh\n"
  printf "  -S) clone repos using https\n"
  printf "  -h) prints this help message\n"
  printf "  -u X )  will clone using X as the URL\n"
  printf "  -U) updates the repos rather than clones\n"
}

while getopts "cdhsSu:U" o; do case "${o}" in
  c) unset SCRIPT_DIRS ;;
  d)
    [ -n "$SCRIPT_DIRS" ] && SCRIPT_DIRS="$SCRIPT_DIRS\n$OPTARG"
    [ -z "$SCRIPT_DIRS" ] && SCRIPT_DIRS="$OPTARG"
    ;;
  s) ___USE_SSH=y ;;
  S) unset ___USE_SSH ;;
  u)
    SSH_BASE_URL="$OPTARG"
    HTTP_BASE_URL="$OPTARG"
    ;;
  U) UPDATE="y" ;;
  *) printhelp ;;
esac done

update_dir () {
  if [ ! -d "$1" ]; then
    printf "%s does not exist; please clone first.\n" "$1"
  elif [ -f "$1" ]; then
    printf "%s already exists, but not as a dir; cannot update.\n" "$1"
  else
    printf "Updating %s...\n" "$1"
    cd "$1"
    git pull
    cd ..
  fi
}

clone_dir () {
  if [ -d "$1" ]; then
    printf "%s already exists as dir; not adding.\n" "$1"
  elif [ -f "$1" ]; then
    printf "%s already exists, but not as a dir; not adding.\n" "$1"
  else
    if [ -n "$2" ]; then
      printf "Cloning %s using ssh; will be read-write\nNote: only my ssh key will clone it\n" "$1"
      git clone "$SSH_BASE_URL$1.git" "$1"
    else
      printf "Cloning %s using http; will be read-only\n" "$1"
      git clone "$HTTP_BASE_URL$1.git" "$1"
    fi
  fi
}

echo "$SCRIPT_DIRS" | while read -r i; do
  [ -z "$i" ] && continue
  if [ "$UPDATE" = "y" ]; then
    update_dir "$i"
  else
    clone_dir "$i"  "$___USE_SSH"
  fi
done