initial checkin
[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 Sep 30 12:36:24 CEST 2006 [mika]
8 ################################################################################
9
10 if [ $UID != 0 ] ; then
11    echo Error: become root before starting $0 >& 2
12    exit 100
13 fi
14
15 PN='grml-hostname'
16 TMP=$(mktemp)
17 OLDHOSTNAME="$(hostname)"
18
19 if [ -x /usr/bin/random-hostname ] ; then
20    HOSTNAME="$(/usr/bin/random-hostname)"
21 else
22    HOSTNAME="$(hostname)"
23 fi
24
25 bailout() {
26   rm -f $TMP
27   exit 1
28 }
29 trap bailout 1 2 3 15
30
31 # running inside grml2hd? Don't warn because of hostname + X
32 [ -n "$GRML2HD" ] || MSG='\n\nNotice: setting a different hostname while running X is *not* recommend!'
33
34 HOSTNAME="$(dialog --stdout --title "${PN}" --extra-button --extra-label "Propose hostname" \
35 --inputbox "Set hostname (/etc/hostname, /etc/hosts and /etc/postfix/main.cf will be adjusted)\
36 \n\nTip: press 'Propose hostname' for another proposal${MSG}" 14 70 $HOSTNAME)"
37 retval=$?
38
39 case $retval in
40   0)
41      if hostname "$HOSTNAME" 1>/dev/null 2>${TMP} ; then
42        echo "$HOSTNAME" > /etc/hostname
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/^myhostname = .*/myhostname = $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        dialog --stdout --title "${PN}" --msgbox "Setting hostname to $HOSTNAME was successful.$POSTFIX" 0 0
51      else
52        dialog --stdout --title "${PN}" --msgbox "Error when setting hostname to $HOSTNAME: `cat $TMP`" 0 0
53      fi
54      ;;
55   1)
56      echo "Cancel pressed."
57      ;;
58   3)
59      $0 ;;
60   255)
61      echo "ESC pressed."
62      ;;
63 esac
64
65 rm -f $TMP 2>/dev/null
66
67 ## END OF FILE #################################################################