#!/usr/bin/python3

# Copyright (C) 2011 Oracle. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2.  This program is distributed in the hope that it will
# be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
# Public License for more details.  You should have received a copy of the GNU
# General Public License along with this program; if not, write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 021110-1307, USA.

### BEGIN PLUGIN INFO
# name: system
# configure: 60
# cleanup: 60
# description: Script to configure template.
### END PLUGIN INFO

import json
import os

from templateconfig.cli import main
from templateconfig.common import run_cmd, shell_cmd


def cleanup_log(filename):
    if os.path.exists(filename):
        open(filename, 'w').close()
        shell_cmd('rm -f %s.*' % filename)
        shell_cmd('rm -f %s-*' % filename)


def write_rhnuuid(rhnuuid):
    rhnuuidfilename = '/etc/sysconfig/rhn/up2date-uuid'
    if os.path.exists(rhnuuidfilename):
        rhnuuidfile = open(rhnuuidfilename, 'w')
        rhnuuidfile.write('uuid[comment]=Universally Unique ID for this server\n')
        rhnuuidfile.write('rhnuuid=%s\n' % rhnuuid)
        rhnuuidfile.close()


def generate_rhnuuid():
    rhnuuid = run_cmd(['uuidgen']).strip()
    write_rhnuuid(rhnuuid)


def cleanup_rhnuuid():
    write_rhnuuid('UNSPECIFIED')


def do_enumerate(target):
    return json.dumps([])


def do_configure(param):
    param = json.loads(param)
    generate_rhnuuid()
    return json.dumps(param)


def do_cleanup(param):
    param = json.loads(param)
    cleanup_log('/var/log/acpid')
    cleanup_log('/var/log/messages')
    cleanup_log('/var/log/dmesg')
    cleanup_log('/var/log/secure')
    cleanup_log('/var/log/cron')
    cleanup_log('/var/log/wtmp')
    cleanup_log('/var/log/boot.log')
    cleanup_log('/var/log/dracut.log')
    cleanup_log('/var/log/scrollkeeper.log')
    cleanup_log('/var/log/maillog')
    cleanup_log('/var/log/lastlog')
    cleanup_log('/var/log/yum.log')
    cleanup_log('/var/log/audit/audit.log')
    cleanup_log('/var/log/cups/error_log')
    cleanup_log('/var/log/setroubleshoot/setroubleshootd.log')
    cleanup_log('/var/log/spooler')
    shell_cmd('rm -fr /root/.vnc')
    shell_cmd('rm -f /root/.bash_history')
    shell_cmd('rm -f /root/install.log')
    shell_cmd('rm -f /root/install.log.syslog')
    shell_cmd('rm -f /root/anaconda-ks.cfg')
    shell_cmd('rm -f /root/.viminfo')
    shell_cmd('rm -fr /var/log/anaconda.*')
    shell_cmd('rm -f /var/log/oraclevm-template.log*')
    shell_cmd('rm -fr /var/log/sa/*')
    shell_cmd('rm -fr /var/cache/yum/*')
    shell_cmd('rm -f /.autorelabel')
    shell_cmd('rm -f /poweroff')
    shell_cmd('rm -fr /tmp/*')
    shell_cmd('rm -f /var/log/dnf.log')
    shell_cmd('rm -f /var/log/dnf.librepo.log*')
    shell_cmd('rm -f /var/log/hawkey.log')
    shell_cmd('rm -f /var/log/dnf.rpm.log')
    cleanup_rhnuuid()
    return json.dumps(param)


if __name__ == '__main__':
    main(do_enumerate, {'configure': do_configure, 'cleanup': do_cleanup})
