Remove all bashisms; drop grml-muttng
[grml-scripts.git] / usr_sbin / grml-hostname
1 #!/bin/sh
2 # Filename:      grml-hostname
3 # Purpose:       simple frontend to set hostname
4 # Authors:       (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 . /etc/grml/script-functions
10
11 check4root || exit 1
12
13 PN='grml-hostname'
14 OLDHOSTNAME="$(hostname)"
15
16 case "$1" in -h|--help) echo "Usage: $0 [hostname]">&2; exit 1 ;; esac
17
18 if [ -n "$1" ] ; then
19    NEW_HOSTNAME="$1"
20    NONINTERACTIVE=1
21 else
22    if [ -x /usr/bin/random-hostname ] ; then
23       NEW_HOSTNAME="$(/usr/bin/random-hostname)"
24    else
25       NEW_HOSTNAME="$(hostname)"
26    fi
27 fi
28
29 if [ -z "$NONINTERACTIVE" ] ; then
30    NEW_HOSTNAME="$(dialog --stdout --title "${PN}" --extra-button --extra-label "Propose hostname" \
31    --inputbox "Set hostname (/etc/hostname, /etc/hosts and /etc/postfix/main.cf will be adjusted)\
32 \n\nTip: press 'Propose hostname' for another proposal" 14 70 $NEW_HOSTNAME)"
33   retval=$?
34 else
35   retval=0
36 fi
37
38 case $retval in
39   0)
40      echo "$NEW_HOSTNAME" > /etc/hostname
41      echo "$NEW_HOSTNAME" > /etc/mailname
42      sed -i "s/^127.0.0.1[ \t]*$OLDHOSTNAME[ \t]*localhost/127.0.0.1       $NEW_HOSTNAME localhost/" /etc/hosts
43      sed -i "s/^::1[ \t]*ip6-localhost ip6-loopback[ \t]*$OLDHOSTNAME/::1     ip6-localhost ip6-loopback $NEW_HOSTNAME/" /etc/hosts
44      POSTFIX=''
45      if [ -r /etc/postfix/main.cf ] ; then
46        sed -i "s/^mydestination = .*/mydestination = $NEW_HOSTNAME, localhost, localhost.localdomain/" /etc/postfix/main.cf && \
47        sed -i "s/^myhostname = .*/myhostname = $NEW_HOSTNAME/" /etc/postfix/main.cf && POSTFIX='
48 Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. Do not forget to restart postfix if necessary.'
49      fi
50      if [ -z "$NONINTERACTIVE" ] ; then
51         dialog --stdout --title "${PN}" --msgbox "Setting hostname to $NEW_HOSTNAME was successful.$POSTFIX" 0 0
52      else
53         echo "Setting hostname to $NEW_HOSTNAME: done"
54      fi
55      exit 0
56      ;;
57   1)
58      echo "Cancel pressed."
59      ;;
60   3)
61      $0 ;;
62   255)
63      echo "ESC pressed."
64      ;;
65 esac
66
67 ## END OF FILE #################################################################