#!/bin/bash

# https://wiki.debian.org/HowTo/ChangeHostname

READCONF=/opt/algo/bin/readconf

usage() {
   echo "usage : $0 <new hostname>"
   exit 1
}

[ -z $1 ] && usage

old=`hostname`
new=$1

echo $new

# Only allow letters (A-Z), digits (0-9), and hyphens (-) as per RFC952

if [[ $new =~ [^a-zA-Z0-9\-] ]]; then
	# Use the default hostname if user input is invalid
	new=$($READCONF admin.devname -d)
fi

for file in \
   /etc/hostname \
   /etc/hosts \
   /etc/ssh/ssh_host_rsa_key.pub \
   /etc/ssh/ssh_host_dsa_key.pub \
   /etc/motd \
   /etc/ssmtp/ssmtp.conf
do
   [ -f $file ] && sed -i.old -e "s:$old:$new:g" $file
done
