<commit_before>#!/usr/bin/env ruby

require 'open3'
require 'optparse'
require 'ostruct'

options = OpenStruct.new
# settin' some defaults
options.systemd = 0
options.sysvinit = 0
options.systemd_el = 0
options.systemd_sles = 0
options.old_el = 0
options.old_sles = 0
options.sles = 0
options.java = 'java-1.8.0-openjdk-headless'
options.release = 1
options.is_pe = false
options.replaces = {}
options.additional_dependencies = []
options.user = 'puppet'
options.group = 'puppet'
options.additional_dirs = []
options.sources = []
options.debug = false
options.logrotate = false
options.termini = false
options.termini_chdir = 'termini'
options.termini_sources = ['opt']
options.rpm_triggers = []
options.deb_interest_triggers = []
options.deb_activate_triggers = []

OptionParser.new do |opts|
  opts.on('-o', '--operating-system OS', [:fedora, :el, :sles, :debian, :ubuntu], 'Select operating system (fedora, el, sles, debian, ubuntu)') do |o|
    options.operating_system = o
  end
  opts.on('--os-version VERSION', Integer, 'VERSION of the operating system to build for') do |v|
    options.os_version = v
  end
  opts.on('-n', '--name PROJECT', 'Name of the PROJECT to build') do |n|
    options.name = n
  end
  opts.on('--package-version VERSION', 'VERSION of the package to build') do |v|
    options.version = v
  end
  opts.on('--release RELEASE', 'RELEASE of the package') do |r|
    options.release = r
  end
  opts.on('--[no-]enterprise-build', 'Whether or not this is a PE build') do |e|
    options.is_pe = e
  end
  opts.on('--replaces <PKG,VERSION>', Array, 'PKG and VERSION replaced by this package. Can be passed multiple times.') do |pkg,ver|
    options.replaces[pkg] = ver
  end
  opts.on('--additional-dependency DEP', 'Additional dependency this package has. Can be passed multiple times.') do |dep|
    options.additional_dependencies << dep
  end
  opts.on('-u', '--user USER', 'USER that should be added with this package') do |user|
    options.user = user
  end
  opts.on('-g', '--group GROUP', 'GROUP that should be added with this package') do |group|
    options.group = group
  end
  opts.on('--create-dir DIR', 'The package should additionally create DIR') do |dir|
    options.additional_dirs << dir
  end
  opts.on('--realname NAME', 'The realname') do |name|
    options.realname = name
  end
  opts.on('--chdir DIR', 'The dir to chdir to before building') do |dir|
    options.chdir = dir
  end
  opts.on('--source <DIR>', Array, 'comma-separated list of source dirs') do |dir|
    options.sources = dir
  end
  opts.on('--dist NAME', 'the dist tag') do |dist|
    options.dist = dist
  end
  opts.on('--[no-]debug', 'for debugging purposes') do |d|
    options.debug = d
  end
  opts.on('--[no-]logrotate', 'to logrotate or not to logrotate') do |l|
    options.logrotate = l
  end
  opts.on('--[no-]build-termini', 'whether or not we should build a termini package') do |t|
    options.termini =  t
  end
  opts.on('--termini-chdir DIR', 'DIR for the termini build, defaults to "termini"') do |c|
    options.termini_chdir = c
  end
  opts.on('--termini-sources <SOURCES>', Array, 'sources for the termini build, defaults to "opt"') do |c|
    options.termini_chdir = c
  end
  opts.on('--rpm-trigger TRIGGER', 'TRIGGER for the rpm packages, in the format package:file_containing_script') do |t|
    options.rpm_triggers << t
  end
  opts.on('--deb-interest-trigger TRIGGER', 'name of the interest TRIGGER for the deb packages ') do |t|
    options.deb_interest_triggers << t
  end
  opts.on('--deb-activate-trigger TRIGGER', 'name of the activate TRIGGER for the deb packages') do |t|
    options.deb_activate_triggers << t
  end
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!

# validation
fail "--name is required!" unless options.name
options.realname = options.name if options.realname.nil?
fail "--package-version is required!" unless options.version
fail "--operating-system is required!" unless options.operating_system
options.chdir = options.dist if options.chdir.nil?
options.output_type = case options.operating_system
                      when :fedora, :el, :sles
                        'rpm'
                      when :debian, :ubuntu
                        'deb'
                      else
                        fail "Can't figure out the output type for #{options.operating_system}. Teach me?"
                      end
# don't require the os-version for deb, just require dist
fail "--os-version is required!" unless options.os_version or options.output_type == 'deb'
fail "--dist is required!" if options.output_type == 'deb' && options.dist.nil?
# set some default sources
if options.sources.empty?
  options.sources = case options.operating_system
                    when :fedora, :sles, :el
                      if options.operating_system == :el && options.os_version < 7 || options.operating_system == :sles && options.os_version < 12 #sysv rpm platforms
                        ['etc', 'opt', 'var']
                      else
                        ['etc', 'opt', 'usr', 'var']
                      end
                    when :debian, :ubuntu
                      if options.dist == 'trusty' #trusty is the only sysv deb we build for right now
                        ['etc', 'opt', 'var']
                      else
                        ['etc', 'lib', 'opt', 'usr', 'var']
                      end
                    else
                      fail "I don't know what your default sources should be, pass it on the command line!"
                    end
end
options.dist = "#{options.operating_system}#{options.os_version}" if options.dist.nil?

fpm_opts = Array('')
shared_opts = Array('')
termini_opts = Array('')

options.app_logdir = "/var/log/puppetlabs/#{options.realname}"
options.app_rundir = "/var/run/puppetlabs/#{options.realname}"
options.app_prefix = "/opt/puppetlabs/server/apps/#{options.realname}"
options.app_data = "/opt/puppetlabs/server/data/#{options.realname}"

# rpm specific options
if options.output_type == 'rpm'

  shared_opts << "--rpm-rpmbuild-define 'rpmversion #{options.version}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_logdir #{options.app_logdir}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_rundir #{options.app_logdir}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_prefix #{options.app_prefix}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_data #{options.app_data}'"

  if options.operating_system == :fedora # all supported fedoras are systemd
    options.systemd = 1
    options.systemd_el = 1
  elsif options.operating_system == :el && options.os_version >= 7 # systemd el
    options.systemd = 1
    options.systemd_el = 1
  elsif options.operating_system == :el # old el
    options.sysvinit = 1
    options.old_el = 1
  elsif options.operating_system == :sles && options.os_version >= 12 # systemd sles
    options.systemd = 1
    options.systemd_sles = 1
    options.sles = 1
    options.java = 'java-1_8_0-openjdk-headless'
  elsif options.operating_system == :sles #old sles<commit_msg>(maint) Was setting the rundir to the logdir<commit_after>#!/usr/bin/env ruby

require 'open3'
require 'optparse'
require 'ostruct'

options = OpenStruct.new
# settin' some defaults
options.systemd = 0
options.sysvinit = 0
options.systemd_el = 0
options.systemd_sles = 0
options.old_el = 0
options.old_sles = 0
options.sles = 0
options.java = 'java-1.8.0-openjdk-headless'
options.release = 1
options.is_pe = false
options.replaces = {}
options.additional_dependencies = []
options.user = 'puppet'
options.group = 'puppet'
options.additional_dirs = []
options.sources = []
options.debug = false
options.logrotate = false
options.termini = false
options.termini_chdir = 'termini'
options.termini_sources = ['opt']
options.rpm_triggers = []
options.deb_interest_triggers = []
options.deb_activate_triggers = []

OptionParser.new do |opts|
  opts.on('-o', '--operating-system OS', [:fedora, :el, :sles, :debian, :ubuntu], 'Select operating system (fedora, el, sles, debian, ubuntu)') do |o|
    options.operating_system = o
  end
  opts.on('--os-version VERSION', Integer, 'VERSION of the operating system to build for') do |v|
    options.os_version = v
  end
  opts.on('-n', '--name PROJECT', 'Name of the PROJECT to build') do |n|
    options.name = n
  end
  opts.on('--package-version VERSION', 'VERSION of the package to build') do |v|
    options.version = v
  end
  opts.on('--release RELEASE', 'RELEASE of the package') do |r|
    options.release = r
  end
  opts.on('--[no-]enterprise-build', 'Whether or not this is a PE build') do |e|
    options.is_pe = e
  end
  opts.on('--replaces <PKG,VERSION>', Array, 'PKG and VERSION replaced by this package. Can be passed multiple times.') do |pkg,ver|
    options.replaces[pkg] = ver
  end
  opts.on('--additional-dependency DEP', 'Additional dependency this package has. Can be passed multiple times.') do |dep|
    options.additional_dependencies << dep
  end
  opts.on('-u', '--user USER', 'USER that should be added with this package') do |user|
    options.user = user
  end
  opts.on('-g', '--group GROUP', 'GROUP that should be added with this package') do |group|
    options.group = group
  end
  opts.on('--create-dir DIR', 'The package should additionally create DIR') do |dir|
    options.additional_dirs << dir
  end
  opts.on('--realname NAME', 'The realname') do |name|
    options.realname = name
  end
  opts.on('--chdir DIR', 'The dir to chdir to before building') do |dir|
    options.chdir = dir
  end
  opts.on('--source <DIR>', Array, 'comma-separated list of source dirs') do |dir|
    options.sources = dir
  end
  opts.on('--dist NAME', 'the dist tag') do |dist|
    options.dist = dist
  end
  opts.on('--[no-]debug', 'for debugging purposes') do |d|
    options.debug = d
  end
  opts.on('--[no-]logrotate', 'to logrotate or not to logrotate') do |l|
    options.logrotate = l
  end
  opts.on('--[no-]build-termini', 'whether or not we should build a termini package') do |t|
    options.termini =  t
  end
  opts.on('--termini-chdir DIR', 'DIR for the termini build, defaults to "termini"') do |c|
    options.termini_chdir = c
  end
  opts.on('--termini-sources <SOURCES>', Array, 'sources for the termini build, defaults to "opt"') do |c|
    options.termini_chdir = c
  end
  opts.on('--rpm-trigger TRIGGER', 'TRIGGER for the rpm packages, in the format package:file_containing_script') do |t|
    options.rpm_triggers << t
  end
  opts.on('--deb-interest-trigger TRIGGER', 'name of the interest TRIGGER for the deb packages ') do |t|
    options.deb_interest_triggers << t
  end
  opts.on('--deb-activate-trigger TRIGGER', 'name of the activate TRIGGER for the deb packages') do |t|
    options.deb_activate_triggers << t
  end
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
end.parse!

# validation
fail "--name is required!" unless options.name
options.realname = options.name if options.realname.nil?
fail "--package-version is required!" unless options.version
fail "--operating-system is required!" unless options.operating_system
options.chdir = options.dist if options.chdir.nil?
options.output_type = case options.operating_system
                      when :fedora, :el, :sles
                        'rpm'
                      when :debian, :ubuntu
                        'deb'
                      else
                        fail "Can't figure out the output type for #{options.operating_system}. Teach me?"
                      end
# don't require the os-version for deb, just require dist
fail "--os-version is required!" unless options.os_version or options.output_type == 'deb'
fail "--dist is required!" if options.output_type == 'deb' && options.dist.nil?
# set some default sources
if options.sources.empty?
  options.sources = case options.operating_system
                    when :fedora, :sles, :el
                      if options.operating_system == :el && options.os_version < 7 || options.operating_system == :sles && options.os_version < 12 #sysv rpm platforms
                        ['etc', 'opt', 'var']
                      else
                        ['etc', 'opt', 'usr', 'var']
                      end
                    when :debian, :ubuntu
                      if options.dist == 'trusty' #trusty is the only sysv deb we build for right now
                        ['etc', 'opt', 'var']
                      else
                        ['etc', 'lib', 'opt', 'usr', 'var']
                      end
                    else
                      fail "I don't know what your default sources should be, pass it on the command line!"
                    end
end
options.dist = "#{options.operating_system}#{options.os_version}" if options.dist.nil?

fpm_opts = Array('')
shared_opts = Array('')
termini_opts = Array('')

options.app_logdir = "/var/log/puppetlabs/#{options.realname}"
options.app_rundir = "/var/run/puppetlabs/#{options.realname}"
options.app_prefix = "/opt/puppetlabs/server/apps/#{options.realname}"
options.app_data = "/opt/puppetlabs/server/data/#{options.realname}"

# rpm specific options
if options.output_type == 'rpm'

  shared_opts << "--rpm-rpmbuild-define 'rpmversion #{options.version}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_logdir #{options.app_logdir}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_rundir #{options.app_rundir}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_prefix #{options.app_prefix}'"
  fpm_opts << "--rpm-rpmbuild-define '_app_data #{options.app_data}'"

  if options.operating_system == :fedora # all supported fedoras are systemd
    options.systemd = 1
    options.systemd_el = 1
  elsif options.operating_system == :el && options.os_version >= 7 # systemd el
    options.systemd = 1
    options.systemd_el = 1
  elsif options.operating_system == :el # old el
    options.sysvinit = 1
    options.old_el = 1
  elsif options.operating_system == :sles && options.os_version >= 12 # systemd sles
    options.systemd = 1
    options.systemd_sles = 1
    options.sles = 1
    options.java = 'java-1_8_0-openjdk-headless'
  elsif options.operating_system == :sles #old sles
{'fraction_non_alphanumeric': 0.13122395649410268, 'fraction_numerical': 0.00423758740024013, 'mean_word_length': 2.920265780730897, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3832505', 'n_tokens_mistral': 4694, 'n_tokens_neox': 4481, 'n_words': 1314}