#!/bin/bash

SALPACKDIR=`dirname $0`
. $SALPACKDIR/salpack-functions

SSHD_CONFIG=/etc/ssh/sshd_config
ALIASES=/etc/aliases

ssh_restrict_root() {
  if get_yes_no RESTRICT_ROOT "Restrict root login with keyboard password?"; then
    grep -q '^Match User root' $SSHD_CONFIG \
      || (
           echo "" >> $SSHD_CONFIG
           echo "Match User root" >> $SSHD_CONFIG
           echo -e "\tPasswordAuthentication no" >> $SSHD_CONFIG
         )
    sed -i 's/#\(auth\s*required\s*pam_wheel.so use_uid\)/\1/' \
      /etc/pam.d/su
  fi
}

ssh_allow_editor() {
  if get_yes_no SSH_EDITOR "Allow EDITOR variable for ssh connections?"; then
    grep -q '^AcceptEnv EDITOR' $SSHD_CONFIG || \
      sed -i '/^AcceptEnv XMODIFIERS/aAcceptEnv EDITOR\n' \
        $SSHD_CONFIG
  fi
}

configure_console() {
  if get_yes_no SERIAL_CONSOLE "Reconfigure console to serial /dev/ttyS0?"; then
    $SALPACKDIR/salpack-configure-console update-grub2
  fi
}

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

increase_grub_timeout() {
  # GRUB1
  if [ -f /boot/grub/grub.conf ]; then
    sed -i 's/^timeout=1$/timeout=5/' /boot/grub/grub.conf
  fi
  # GRUB2
  if [ -f /boot/grub2/grub.cfg ]; then
    sed -i 's/^set timeout=1$/set timeout=5/' /boot/grub2/grub.cfg
    sed -i 's/^GRUB_TIMEOUT=1$/GRUB_TIMEOUT=5/' /etc/default/grub
  fi
}

disable_nfs() {
  if get_yes_no DISABLE_NFS "Disable NFS support?"; then
    ssrv disable nfs nfslock rpcbind rpcgssd rpcidmapd rpcsvcgssd
  fi
}

remove_mdadm_num_devices() {
  if [ -r /etc/mdadm.conf ]; then
    if grep -q num-devices /etc/mdadm.conf; then
      if get_yes_no REMOVE_NUM_DEV "Remove num-devices=# from mdadm.conf?"; then
        sed -i 's/ num-devices=[0-9] / /' /etc/mdadm.conf
      fi
    fi
  fi
}

disable_raid_check() {
  if get_yes_no DISABLE_RAID_CHECK \
       "Disable raid-check?"; then
    sed -i 's/^ENABLED=yes/ENABLED=no/' /etc/sysconfig/raid-check
  fi
}

yum_cron() {
  if get_yes_no YUM_CRON \
       "Reconfigure yum-cron to be silent and run automatic update?"; then
    if [ -r /etc/yum/yum-cron.conf ]; then
      sed -i~ \
        -e 's/^update_messages = yes/update_messages = no/' \
        -e 's/^apply_updates = no/apply_updates = yes/' \
        -e 's/^debuglevel = -2/debuglevel = -3/' \
        /etc/yum/yum-cron.conf
    fi
    #if [ -r /etc/yum/yum-cron-hourly.conf ]; then
    #  sed -i~ \
    #    -e 's/^debuglevel = -2/debuglevel = -4/' \
    #    /etc/yum/yum-cron-hourly.conf
    #fi
    # disable yum-cron-hourly
    if [ -r /etc/cron.hourly/0yum-hourly.cron ]; then
      sed -i~ \
        's|^exec /usr/sbin/yum-cron|#exec /usr/sbin/yum-cron|' \
        /etc/cron.hourly/0yum-hourly.cron
    fi
  fi
}

if [ "$1" ]; then
  if [ "$1" = "-f" ]; then
    export SALPACK_FORCE="ON"
    shift 1
  fi
  $@ # run this command
  exit 0
fi

# Update hostname & static IP
get_text "Hostname:" `hostname`
HOSTNAME="$RET"
echo "HOSTNAME=$HOSTNAME"

# Update aliases
grep -q ^root: $ALIASES || update_aliases

get_multi Settings \
  RESTRICT_ROOT "Restrict root login with keyboard password?" ON \
  SSH_EDITOR "Allow EDITOR variable for ssh connections?" ON \
  SERIAL_CONSOLE "Reconfigure console to serial /dev/ttyS0?" OFF \
  DISABLE_NFS "Disable NFS support?" ON \
  REMOVE_NUM_DEV "Remove num-devices=# from mdadm.conf?" ON \
  DISABLE_RAID_CHECK "Disable raid-check?" ON \
  DEL_LOCALHOST "Remove IPv6 localhost from /etc/hosts?" ON

# reconfigure network
. $SALPACKDIR/salpack-static-network all "$HOSTNAME"

# Restrict root login only with key
ssh_restrict_root

# Allow EDITOR
ssh_allow_editor

# Configure serial console
configure_console
increase_grub_timeout

# check mdadm.conf
remove_mdadm_num_devices

# disable raid-check
disable_raid_check

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

# Update mysql
if [ -x $SALPACKDIR/salpack-configure-mysql ]; then
  $SALPACKDIR/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
  hwclock -w
fi
if [ -x /bin/systemctl ]; then
  systemctl enable ntpdate.service && systemctl start ntpdate.service
  hwclock -w
fi
chkconfig ntpd on
service ntpd start
