#!/bin/sh

echo "snapshot-ipv4-dhcp-el7" > /etc/hostname

hostname snapshot-ipv4-dhcp-el7

cat > /etc/hosts << EOF
127.0.0.1   snapshot-ipv4-dhcp-el7 snapshot-ipv4-dhcp-el7 localhost localhost.localdomain
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
EOF


apt-get update >/dev/null 2>/dev/null
apt-get -y install puppet >/dev/null 2>/dev/null


cat > /etc/puppet/puppet.conf << EOF
[main]
vardir = /var/lib/puppet
logdir = /var/log/puppet
rundir = /var/run/puppet
ssldir = \$vardir/ssl

[agent]
pluginsync      = true
report          = true
certname        = snapshot-ipv4-dhcp-el7
EOF

/usr/bin/puppet agent --config /etc/puppet/puppet.conf -o --tags no_such_tag --server  --no-daemonize
/sbin/chkconfig puppetd on

if [ -x /usr/bin/curl ]; then
  /usr/bin/curl -o /dev/null -H 'Content-Type: text/plain' --fail --noproxy \* --silent 'http://foreman.example.com/unattended/built'
elif [ -x /usr/bin/wget ]; then
  /usr/bin/wget -q -O /dev/null --method POST --header 'Content-Type: text/plain' --no-proxy 'http://foreman.example.com/unattended/built'
else
  wget -q -O /dev/null --header 'Content-Type: text/plain' 'http://foreman.example.com/unattended/built'
fi
FINAL_STATUS=$?

if [ -x "$(command -v subscription-manager)" ] ; then
  subscription-manager facts --update
  SUB_MAN_STATUS=$?

  if [ $FINAL_STATUS -eq 0 ]; then
    FINAL_STATUS=$SUB_MAN_STATUS
  fi
fi

(exit $FINAL_STATUS);

PATH=/usr/bin:/usr/sbin:/bin:/sbin:$PATH shutdown -r +1

exit 0
