#!/bin/bash

# Secure entrypoint
chmod 600 /entrypoint.sh

# Wait for RabbitMQ to start
bash -c 'while [[ "$(curl -s -o /dev/null -w ''%{http_code}'' http://guest:guest@127.0.0.1:15672/api/aliveness-test/%2F)" != "200" ]]; do echo "RabbitMQ not up yet" && sleep 1; done'

# Setup admin user
rabbitmqctl add_user admin admin
rabbitmqctl set_user_tags admin administrator
rabbitmqctl set_permissions -p / admin ".*" ".*" ".*"

# Declare message queues
rabbitmqadmin declare queue --vhost=/ name=factory_idle durable=true
rabbitmqadmin declare queue --vhost=/ name=automation durable=true
rabbitmqadmin declare queue --vhost=/ name=batch_process durable=true
rabbitmqadmin declare queue --vhost=/ name=alerts durable=true
rabbitmqadmin declare queue --vhost=/ name=quality_control durable=true

# Populate data
echo 102 | rabbitmqadmin publish exchange=amq.default routing_key=factory_idle
echo process_done | rabbitmqadmin publish exchange=amq.default routing_key=automation
echo process_idle | rabbitmqadmin publish exchange=amq.default routing_key=automation
echo process_halt | rabbitmqadmin publish exchange=amq.default routing_key=automation
echo labelled | rabbitmqadmin publish exchange=amq.default routing_key=batch_process
echo processed | rabbitmqadmin publish exchange=amq.default routing_key=batch_process
echo pending | rabbitmqadmin publish exchange=amq.default routing_key=batch_process
echo 00 | rabbitmqadmin publish exchange=amq.default routing_key=factory_idle
echo "device-halted|err|storage_room_132" | rabbitmqadmin publish exchange=amq.default routing_key=factory_idle
echo "device-failed-to-respond|err|storage_room_132" | rabbitmqadmin publish exchange=amq.default routing_key=factory_idle
echo "device-time-out|err|storage_room_132" | rabbitmqadmin publish exchange=amq.default routing_key=factory_idle

# Add more queues and logs
rabbitmqadmin declare queue --vhost=/ name=production_logs durable=true
rabbitmqadmin declare queue --vhost=/ name=temperature_logs durable=true
rabbitmqadmin declare queue --vhost=/ name=maintenance_logs durable=true

echo "production_started|info|line_1" | rabbitmqadmin publish exchange=amq.default routing_key=production_logs
echo "product_quality_issue|warning|line_1" | rabbitmqadmin publish exchange=amq.default routing_key=quality_control
echo "maintenance_required|info|line_1" | rabbitmqadmin publish exchange=amq.default routing_key=maintenance_logs

echo "HTB{th3_hunt3d_b3c0m3s_th3_hunt3r}" | rabbitmqadmin publish exchange=amq.default routing_key=factory_idle