#!/bin/sh

# PROVIDE: ec2_bootmail
# REQUIRE: mail
# BEFORE: ec2_firstboot

# Define ec2_bootmail_enable=YES in /etc/rc.conf and create /root/firstboot
# to enable sending an instance-launched email when the system next boots.
#
: ${ec2_bootmail_enable=NO}

. /etc/rc.subr

name="ec2_bootmail"
start_cmd="ec2_bootmail_run"
stop_cmd=":"

ec2_bootmail_run()
{

	# If this is the first boot, send an email.
	if [ -f /root/firstboot ]; then
		(
			echo "To: ${ec2_bootmail_addr}"
			echo "From: EC2 Instance <root@localhost>"
			echo "Subject: Instance launch"
			echo
			uname -a
			echo "Instance type: `fetch -qo - http://169.254.169.254/latest/meta-data/instance-type`"
		) | sendmail -t
	fi
}

load_rc_config $name
run_rc_command "$1"
