#!/bin/bash

slept="no"

RED=''
GREEN=''
YELLOW=''
ORANGE=''
BOLD=''
NC=''
if [ -t 1 ]; then
    RED='\033[0;31m'    # \e[31m
    GREEN='\033[0;32m'
    YELLOW='\033[0;33m'
    ORANGE='\033[38;5;214m'
    BOLD='\e[1m'
    NC='\033[0m' # No Color
fi


clear_stdin()
(
    old_tty_settings=$(stty -g)
    stty -icanon min 0 time 0

    while read none; do :; done

    stty $old_tty_settings
)

if egrep "^flags\s*:.*\bhypervisor\b" /proc/cpuinfo >/dev/null; then
    # running inside a VM
    HOSTCOLOR=${ORANGE}
else
    # running on metal
    HOSTCOLOR=${GREEN}
fi

do_gc() {
 if [ -d /etc/.git ]; then
   cd /etc/
   git gc
 fi
}

do_rkhunter() {
  if which rkhunter >/dev/null; then
    rkhunter --propupd
  fi
}

do_tripwire() {
  if which tripwire >/dev/null; then
    tripwire -m c 
    tripwire -m u -r $(/bin/ls -t /var/lib/tripwire/report/*.twr | head -1)
    slept="yes"
  fi
}

do_apt() {
  if which aptitude >/dev/null; then
    aptitude clean
  fi
}

do_push() {
 if [ -d /etc/.git ]; then
   cd /etc/
   git gc
   if [ "x${slept}" != "xyes" ]; then
       clear_stdin
       echo -e -n "Press ${BOLD}[Enter]${NC} to continue on ${HOSTCOLOR}${USER}@${BOLD}$(hostname)${NC}... "
       read -s -p "" _
       echo
   fi
   git push
 fi
}

do_checkkernel() {
  if which needrestart >/dev/null; then
    needrestart -k
  fi
}


do_apt
do_rkhunter
do_gc
do_tripwire
do_push
do_checkkernel
