3b06f74cd2ae6cb1d5b41e6c6e95a62853c2acaa
[feisty_meow.git] / scripts / networking / prune_domain.sh
1 #!/bin/bash
2
3 # takes the domain name off of the hostname provided and outputs just the
4 # short name for the host.  the host is the first parameter, and the domain
5 # name is the second, but both are optional.  if missing, this script uses
6 # the local machine's information to print a hostname stripped of domain.
7
8 source "$FEISTY_MEOW_SCRIPTS/core/functions.sh"
9
10 #ha, should look back and see what other cool stuff hecho used to do.
11 function hecho()
12 {
13   echo $* 1>&2
14 }
15
16 export host="$1"; shift
17 if [ -z "$host" ]; then
18   host="$(hostname)"
19 fi
20 export DOMAIN_NAME="$1"; shift
21 if [ -z "$DOMAIN_NAME" ]; then
22   # set the domain name based on what's found in the resolv.conf file, where
23   # dns info is often found.
24   DOMAIN_NAME=$(grep -i search </etc/resolv.conf | tail -1 | sed -n -e 's/domain.\(.*\)$/\1/p')
25   if [ -z "$DOMAIN_NAME" ]; then
26     # second try, searching out the search domain as a suitable replacement
27     # for the actual domain being specified.
28     DOMAIN_NAME=$(grep -i search </etc/resolv.conf | tail -1 | sed -n -e 's/search.\(.*\)$/\1/p')
29   fi
30   # oh well, we don't know what the heck the domain is.
31   if [ -z "$DOMAIN_NAME" ]; then
32     hecho "The domain name cannot be intuited from the system and was not provided"
33     hecho "on the command line, so we have to give up."
34 #hmmm: below starts to look reusable.
35     hecho "   host is '$host', domain is'$DOMAIN_NAME'."
36     exit 1
37   fi
38 fi
39
40 #hecho domain is $DOMAIN_NAME
41 #hecho name before was $host
42
43 domlen=${#DOMAIN_NAME}
44 namelen=${#host}
45
46 if [ $namelen -le $(($domlen+1)) ]; then
47   hecho "The hostname is too short to remove the domain name."
48   hecho "   host is '$host', domain is'$DOMAIN_NAME'."
49   exit 1
50 fi
51
52 #hecho domain name length is $domlen
53
54 # we go back one more character than needed for the domain name, so we can consume the dot.
55 chopname=${host:0:(($namelen - $domlen - 1))}
56
57 #hecho "chopped name is '$chopname'"
58
59 echo $chopname
60