summaryrefslogtreecommitdiffstats
path: root/ship/lib/core
blob: 6252481fa787c6e6bcc3ad1ed5d0d967533e6e4b (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
# logging
msg() { echo "$*" >&2; }
info()  { msg "** $*"; }
error() { msg "!! $*"; }
## usage: die [REASON...]
die() {
  test $# -gt 0 && error "$*"
  error 'Bailing out.'
  exit 1
}
exists(){ type "$1" >/dev/null 2>/dev/null; }
is_root(){
  test $(id -u) -eq 0
} 
internet(){
  ping -w 1 google.de >/dev/null 2>&1
}
defer(){
  #close enough
  trapstr="$1;${trapstr:-exit}"
  trap "$trapstr" INT TERM EXIT KILL
}

esudo(){
  # weaksauce esudo (expect sudo)
  if ! is_root; then
    # for the record:
    # exec sudo -E "$0" "$@"
    error "You are not root enough for this script"
    exit 23 # go to hell
  fi
}

get_hostname(){
  # finds the current hostname
  #   if ENV HOSTN is set echo $HOSTN
  #   We try the following:
  #      $HOSTN
  #      $HOSTNAME
  #      hostname
  #      uci system.hostname
  #      /etc/hostname
  #   if everything fails, it returns 1 and prints 'unknown'

  if [ -n "${HOSTN:-}" ] ;     then printf "${HOSTN:-}" 
  elif [ -n "${HOSTNAME:-}" ] ;then printf "$HOSTNAME"
  elif exists hostname ;       then printf "$(hostname)"
  elif exists uci    ;         then printf "$(uci get system.@system[0].hostname)"
  elif [ -e /etc/hostname ]   ;then printf "$(cat /etc/hostname)"
  else                              printf "unknown"; return 1
  fi
  return 0
}

line_to_dot(){ 
  while read line; do printf .; done;
}

get_os()
{
  # TODO: find all the release files
  #if grep -q 'Linux' /etc/*release 2>/dev/null || grep -qe 'Linux' /etc/issue 2>/dev/null; then
  if grep -q 'Linux' /etc/lsb-release 2>/dev/null || grep -q 'Linux' /etc/issue 2>/dev/null; then
    echo 'linux'
  elif exists getprop ; then
    echo 'android'
  elif test -e /etc/openwrt_release; then
    echo 'openwrt'
  elif uname -s | grep -qi 'darwin'; then
    echo 'osx'
  else
    warn "Cannot determine your operating system, falling back to Linux"
    echo 'linux'
  fi
}
# user management
has_user(){
    egrep "^$1:" /etc/passwd >/dev/null
}