#!/usr/bin/env ruby

require 'optparse'
require 'fileutils'

optparse = OptionParser.new do |opts|
  opts.banner = "This script is used to remove all katello related packages and services. \nThis should only be used if you are sure you are wanting to completly remove Katello from this machine and lose all of your settings and synced content."

  opts.parse!
end

def confirmed?
  puts "\nARE YOU SURE?: This script permanently deletes data and configuration."
  print "Read the source for a list of what is removed.  Type [remove] to continue? "
  'remove' == (gets.chomp)
end

CONFIG_FILES=[
  '/etc/pulp',
  '/etc/candlepin',
  '/etc/katello',
  '/etc/httpd',
  '/etc/tomcat6',
  '/etc/foreman',
  '/etc/tomcat',
  '/etc/foreman-installer',
  '/etc/foreman-proxy',
  '/etc/pki/katello-certs-tools',
  '/etc/sudoers.d/foreman-proxy',
  '/etc/hammer',
  '/etc/tomcat',
  '/etc/squid',
  '/etc/puppet',
  '/etc/puppetlabs',
  '/etc/qpid',
  '/etc/qpid-dispatch',
  '/etc/sysconfig/foreman.rpmsave'
]

LOG_FILES=[
  '/var/log/katello',
  '/var/log/tomcat6',
  '/var/log/pulp',
  '/var/log/candlepin',
  '/var/log/httpd',
  '/var/log/mongodb',
  '/var/log/foreman',
  '/var/log/foreman-proxy',
  '/var/log/foreman-installer',
  '/var/log/tomcat',
  '/var/log/squid',
  '/var/log/capsule-certs-generate*'
]

RPMS=[
  'puppetlabs-release',
  'foreman-release',
  'foreman-client',
  'foreman-proxy',
  'candlepin',
  'katello',
  '^pulp',
  '^python-pulp',
  '^pulp-',
  'mongo',
  'postgre',
  '^mod_',
  '^rubygem',
  '^ruby193',
  '^tfm',
  '^foreman',
  '^qpid',
  '^python-crane',
  '^python-celery',
  '^python-gofer',
  '^python-qpid',
  '^python-kombu',
  '^python-webpy',
  '^python-nectar',
  '^python-saslwrapper',
  '^python-amqp',
  '^python-billiard',
  '^python-semantic_version',
  '^python-requests',
  '^python-isodate',
  '$HOSTNAME',
  'saslwrapper',
  'ruby',
  'rubygems',
  'httpd',
  'puppet',
  'tomcat',
  'squid'
]

CERT_FILES=[
  '/etc/pki/pulp',
  '/etc/pki/content/*',
  '/etc/pki/katello',
  '/root/ssl-build',
  '/etc/pki/tls/certs/katello-node.crt',
  '/etc/pki/tls/private/katello-node.key',
  '/etc/pki/tls/certs/pulp_consumers_ca.crt',
  '/etc/pki/tls/certs/pulp_ssl_cert.crt',
  '/var/www/html/pub/katello-ca*.rpm',
  '/etc/pki/ca-trust/source/anchors/katello_server-host-cert.crt'
]

CONTENT=[
  '/usr/share/foreman-proxy',
  '/usr/share/foreman-installer-katello',
  '/var/www/html/pub/katello-server-ca.crt',
  '/usr/share/foreman',
  '/var/lib/candlepin',
  '/usr/share/katello',
  '/var/lib/puppet',
  '/opt/puppetlabs/puppet',
  '/var/lib/pgsql',
  '/var/lib/mongodb',
  '/var/lib/katello',
  '/var/lib/pulp',
  '/var/lib/foreman',
  '/usr/share/pulp',
  '/var/lib/tomcat',
  '/var/lib/qpidd',
  '/usr/share/candlepin',
  '/usr/share/tomcat',
  '/usr/share/katello-installer-base',
  '/usr/share/qpid',
  '/usr/share/qpid-tools',
  '/var/cache/',
  '/opt/theforeman',
  '/var/www/html/pub/katello-rhsm-consumer',
  '/var/tmp/foreman-proxy'
]

CONTENT.map! do |dir|
  if File.readlines('/proc/mounts').any?{ |line| line.split(" ")[1] =~ /#{dir}$/ }
    dir + '/*'
  else
    dir
  end
end

def remove
  `katello-service stop`

  puts "Removing RPMs"
  packages = []
  RPMS.each do | rpm |
    rpmpackages = `rpm -qa | grep "#{rpm}"`
    packages += rpmpackages.lines.map(&:chomp)
  end
  `yum erase -y #{packages.join(" ")} > /dev/null 2>&1`

  puts "Cleaning up configuration files"
  FileUtils.rm_rf CONFIG_FILES

  puts "Cleaning up log files"
  # logs
  FileUtils.rm_rf LOG_FILES

  puts "Cleaning up Certs"
  # pulp cert stuff
  FileUtils.rm_rf CERT_FILES

  puts "Cleaning up content"
  #content
  FileUtils.rm_rf CONTENT
end

puts "\nWARNING: This script will erase many packages and config files."
puts "Important packages such as the following will be removed:\n"
puts "  * httpd (apache)"
puts "  * mongodb"
puts "  * tomcat"
puts "  * puppet"
puts "  * ruby"
puts "  * rubygems"
puts "  * All Katello and Foreman Packages\n"
puts "Once these packages and configuration files are removed there is no going back."
puts "If you use this system for anything other than Katello and Foreman you probably"
puts "do not want to execute this script.\n"

print "Read the source for a list of what is removed.  Are you sure(Y/N)? "

response = gets.chomp
if /[Y]/i.match(response)
  confirmed? ? remove : (puts "**** cancelled ****")
else
  puts "**** cancelled ****"
end
