#!/bin/bash

# Current Hostname
CHOST=$(hostname)

# Search parameters
DEFNAME=template
DEFDOM=local

errormsg() {
 echo "Bitte Hostnamen und Domain angeben: $0 <hostname> <domain>"
 exit 0
}

status() {
# Status:
echo "------------------------------------------------------"
echo "Aktueller Hostname (hostname -f): $(hostname -f)"
echo "             (cat /etc/hostname): $(cat /etc/hostname)"
echo "Ersetzungs-Suchmuster           : ${DEFNAME}.${DEFDOM}"
echo "neuer Hostname                  : ${FQDN}"
echo "------------------------------------------------------"
}

renamehost(){
sed -i -e "s/${DEFNAME}.${DEFDOM}/${FQDN}/g" /etc/exim4/update-exim4.conf.conf
sed -i -e "s/${DEFNAME}.${DEFDOM}/${FQDN}/g" /etc/hosts
sed -i -e "s/${DEFNAME}/${HNAME}/g"          /etc/hosts
sed -i -e "s/${DEFNAME}.${DEFDOM}/${FQDN}/g" /etc/mailname
sed -i -e "s/${DEFNAME}/${HNAME}/g"          /etc/hostname
}

renamehostdebug(){
echo "sed -i -e \"s/${DEFNAME}.${DEFDOM}/${FQDN}/g\" /etc/exim4/update-exim4.conf.conf"
echo "sed -i -e \"s/${DEFNAME}.${DEFDOM}/${FQDN}/g\" /etc/hosts"
echo "sed -i -e \"s/${DEFNAME}/${HNAME}/g\"       /etc/hosts"
echo "sed -i -e \"s/${DEFNAME}.${DEFDOM}/${FQDN}/g\" /etc/mailname"
echo "sed -i -e \"s/${DEFNAME}/${HNAME}/g\"       /etc/hostname"
}

# check hostname
if [[ -z "$1" ]]; then 
 status
 renamehostdebug
 echo
 errormsg
else
 HNAME=$1
fi

# check domain
if [[ -z "$2" ]]; then 
 errormsg
else
 DOMNAME=$2
fi

# Domain
FQDN=${HNAME}.${DOMNAME}

# Zeige Status
status
echo 
echo "<Enter>: Umbenennen <Ctrl-C>: Abbrechen"
read

# Umbenennen:
renamehost
