merged grml-quickconfig nonblocking branch
[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 # Latest change: Sam Okt 27 23:43:06 CEST 2007 [mika]
8 ################################################################################
9
10 source /etc/grml/script-functions
11
12 check4root || exit 1
13
14 PN='grml-hostname'
15 OLDHOSTNAME="$(hostname)"
16
17 case "$1" in -h|--help) echo "Usage: $0 [hostname]">&2; exit 1 ;; esac
18
19 if [ -n "$1" ] ; then
20    HOSTNAME="$1"
21    NONINTERACTIVE=1
22 else
23    if [ -x /usr/bin/random-hostname ] ; then
24       HOSTNAME="$(/usr/bin/random-hostname)"
25    else
26       HOSTNAME="$(hostname)"
27    fi
28 fi
29
30 if [ -z "$NONINTERACTIVE" ] ; then
31    HOSTNAME="$(dialog --stdout --title "${PN}" --extra-button --extra-label "Propose hostname" \
32    --inputbox "Set hostname (/etc/hostname, /etc/hosts and /etc/postfix/main.cf will be adjusted)\
33 \n\nTip: press 'Propose hostname' for another proposal" 14 70 $HOSTNAME)"
34   retval=$?
35 else
36   retval=0
37 fi
38
39 case $retval in
40   0)
41      echo "$HOSTNAME" > /etc/hostname
42      echo "$HOSTNAME" > /etc/mailname
43      sed -i "s/^127.0.0.1[ \t]*$OLDHOSTNAME[ \t]*localhost/127.0.0.1       $HOSTNAME localhost/" /etc/hosts
44      sed -i "s/^::1[ \t]*ip6-localhost ip6-loopback[ \t]*$OLDHOSTNAME/::1     ip6-localhost ip6-loopback $HOSTNAME/" /etc/hosts
45      POSTFIX=''
46      if [ -r /etc/postfix/main.cf ] ; then
47        sed -i "s/^mydestination = .*/mydestination = $HOSTNAME, localhost, localhost.localdomain/" /etc/postfix/main.cf && \
48        sed -i "s/^myhostname = .*/myhostname = $HOSTNAME/" /etc/postfix/main.cf && POSTFIX='
49 Configuration of myhostname in /etc/postfix/main.cf has been adjusted as well. Do not forget to restart postfix if necessary.'
50      fi
51      if [ -z "$NONINTERACTIVE" ] ; then
52         dialog --stdout --title "${PN}" --msgbox "Setting hostname to $HOSTNAME was successful.$POSTFIX" 0 0
53      else
54         echo "Setting hostname to $HOSTNAME: done"
55      fi
56      exit 0
57      ;;
58   1)
59      echo "Cancel pressed."
60      ;;
61   3)
62      $0 ;;
63   255)
64      echo "ESC pressed."
65      ;;
66 esac
67
68 ## END OF FILE #################################################################