#!/bin/bash

echo '[*] Checking server certificate'
test -e /var/lib/openvas/CA/cacert.pem || openvas-mkcert -q

echo -e '\n\n[*] Checking client certificate'
if ! test -e /var/lib/openvas/CA/clientcert.pem || \
    ! test -e /var/lib/openvas/private/CA/clientkey.pem; then
    openvas-mkcert-client -n -i
fi

echo -e '\n\n[*] Synchronizing "Network Vulnerability Tests (NVT)" repository with an OpenVAS NVT feed   (Note: this may take a while depending on the speed of your Internet connection)'
openvas-nvt-sync
echo -e '\n\n[*] Synchronizing "Security Content Automation Protocol (SCAP)" data   (Note: this may take a while depending on the speed of your Internet connection)'
openvas-scapdata-sync
echo -e '\n\n[*] Synchronizing CERT advisory directory data   (Note: this may take a while depending on the speed of your Internet connection)'
openvas-certdata-sync

echo -e '\n\n[*] Stopping services'
/etc/init.d/openvas-manager stop
/etc/init.d/openvas-scanner stop

echo -e '\n\n[*] Starting OpenVAS'
killall -w openvassd 2>/dev/null
openvassd
openvasmd --migrate

echo -e '\n\n[*] Regenerating database   (Note: this may take a while depending on the speed of your device)'
openvasmd --progress --rebuild

echo -e '\n\n[*] Stopping OpenVAS'
killall -w openvassd 2>/dev/null
sleep 15

echo -e '\n\n[*] Starting services'
/etc/init.d/openvas-scanner start
/etc/init.d/openvas-manager start
/etc/init.d/greenbone-security-assistant restart

echo -e '\n\n[*] Checking admin user'
if ! openvasmd --list-users | grep -q '^admin$'; then
    openvasmd --create-user=admin
fi
