aboutsummaryrefslogtreecommitdiffstats
path: root/bin/process
blob: 4effb170e838c250c7ef7d45b34c53311194ac93 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/env ruby

require 'erb'
require 'optparse'

def v6rev(input)
	# Parse the input to separate address and netmask
	address, netmask = input.split('/')
	netmask = netmask.to_i

	# Calculate how many bits we actually have (128 - missing bits)
	remaining_bits = 128 - netmask
	remaining_groups = remaining_bits / 16

	# Expand the partial address within the context of just the remaining bits
	expanded_suffix = expand_ipv6_suffix(address, remaining_groups)

	# Convert to hex string and get nibbles
	hex_string = expanded_suffix.gsub(':', '')

	# Split into individual nibbles and reverse for PTR format
	nibbles = hex_string.chars.reverse

	# Join with dots
	nibbles.join('.')
end

def expand_ipv6_suffix(address, total_groups)
	# Handle :: expansion within the context of just our suffix
	if address.include?('::')
		parts = address.split('::')
		left_parts = parts[0].empty? ? [] : parts[0].split(':')
		right_parts = parts[1].empty? ? [] : parts[1].split(':')

		# Calculate how many zero groups we need
		missing_groups = total_groups - left_parts.length - right_parts.length
		zero_groups = ['0000'] * missing_groups

		all_parts = left_parts + zero_groups + right_parts
	else
		all_parts = address.split(':')
	end

	# Pad each part to 4 digits
	all_parts.map { |part| part.rjust(4, '0') }.join(':')
end

# Hash to store our -D variables
template_vars = {}

# Parse command line options
OptionParser.new do |opts|
	opts.banner = "Usage: #{$0} [options] <input> <output>"

	opts.on('-DVAR=VALUE', 'Define template variable') do |definition|
		var, value = definition.split('=', 2)
		if var && value
			template_vars[var] = value
		else
			puts "Invalid -D format: #{definition}"
			exit 1
		end
	end

	opts.on('-h', '--help', 'Show this help') do
		puts opts
		exit
	end
end.parse!

# Check we have the right number of remaining args
if ARGV.length != 2
	puts "Usage: #{$0} [options] <input> <output>"
	puts "Use -h for help"
	exit 1
end

input_file = ARGV[0]
output_file = ARGV[1]

# Read the template
template = File.read(input_file)

# Create a binding with our variables
binding_context = binding
template_vars.each do |var, value|
	binding_context.local_variable_set(var.to_sym, value)
end

# Process with ERB
erb = ERB.new(template)
result = erb.result(binding_context)

# Write output
File.write(output_file, result)