summaryrefslogtreecommitdiffstats
path: root/bin/populate
blob: fdc4c5de2a25a62e47e15d8f02126371eb5c0e1f (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
#! /bin/sh
set -efu

self=$(readlink -f "$0")
basename=${0##*/}
prefix=${self%/bin/*}
libdir=$prefix/lib

debug=false
force=false
origin_host=${HOSTNAME-cat /proc/sys/kernel/hostname}
origin_user=$LOGNAME
ssh_cmd=ssh
target_spec=


fail=true

error() {
  echo "$basename: error: $1" >&2
  fail=false
}

for arg; do
  case $arg in
    --debug)
      debug=true
      ;;
    --force)
      force=true
      ;;
    --ssh=*)
      ssh_cmd=${arg#--ssh=}
      ;;
    -*)
      error "bad argument: $arg"
      ;;
    *)
      if test -n "$target_spec"; then
        error "bad argument: $arg"
      else
        target_spec=$arg
      fi
      ;;
  esac
done

if test -z "$target_spec"; then
  error 'no target specified'
fi

if test "$fail" != true; then
  exit 11
fi


script=$(jq -e -r \
    --argjson use_force "$force" \
    --arg ssh_cmd "$ssh_cmd" \
    --arg target_spec "$target_spec" \
    --arg origin_host "$origin_host" \
    --arg origin_user "$origin_user" \
    -f "$libdir/populate.jq")

if test -z "$script"; then
  error 'no script produced'
  exit 12
fi


if test "$debug" = true; then
  echo "$script"
else
  eval "$script"
fi