#!/bin/bash

TMP=`mktemp salpack.XXXXXXXXXX`
trap "rm -rf $TMP; exit" INT TERM EXIT

get_text() {
  # input returned in $RET variable
  whiptail --title salpack --inputbox "$1" 8 70 "$2" 2> $TMP
  RET=`cat $TMP`
}

get_yes_no() {
  whiptail --title salpack --yesno "$1" 8 70
}

restrict_root() {
  if get_yes_no; then
    grep -q '^Match User root' /etc/ssh/sshd_config \
      || (
           echo "Match User root" >> /etc/ssh/sshd_config
           echo -e "\tPasswordAuthentication no" >> /etc/ssh/sshd_config
         )
    sed -i 's/#\(auth\s*required\s*pam_wheel.so use_uid\)/\1/' \
      /etc/pam.d/su
  fi
}

update_aliases() {
  get_text "root's email address:"
  root_email="$RET"
  if [ "$root_email" ]; then
    echo "root: $root_email" >> /etc/aliases
    newaliases
  fi
}

# Update hostname & static IP
get_text "Hostname:" `hostname`
HOSTNAME="$RET"
echo "HOSTNAME=$HOSTNAME"
/usr/sbin/salpack-static-network all "$HOSTNAME"

# Update aliases
grep -q ^root: /etc/aliases || update_aliases

# Update serial console and other things for virtual machine
if [ -x /usr/sbin/salpack-configure-console ]; then
  # only update automatically if virtio disks found (for guests)
  if [ -f /dev/vda ]; then
    /usr/sbin/salpack-configure-console
  fi
fi

# Update NRPE
if rpm -q salpack-nrpe >/dev/null; then
  /usr/sbin/salpack-configure-nrpe
fi

# Update mysql
if [ -x /usr/sbin/salpack-configure-mysql ]; then
  /usr/sbin/salpack-configure-mysql
fi

# stop iscsi (you only need iscsid), if present
chkconfig iscsi off 2>/dev/null || true

# Start ntp if possible
chkconfig ntpd stop >/dev/null 2>/dev/null
if [ -x /etc/init.d/ntpdate ]; then
  chkconfig ntpdate on
  service ntpdate start
fi
chkconfig ntpd on
service ntpd start
