summaryrefslogtreecommitdiffstats
path: root/lib/core
blob: f6516b5ea1812e33edc7496f397092a1e3362600 (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
#!/bin/sh

# logging
msg() { printf "$@\n" >&2 ;}
info()   { msg "** $@" ;}
error()  { msg "!! $@" ;}
exists(){ type "$1" >/dev/null 2>/dev/null; }

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;
}