#!/bin/sh
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT

if [ "$1" = "upgrade" ]; then
     /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a prep-restart
     /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -a stop
fi

if ! grep "^cwagent:" /etc/group >/dev/null 2>&1; then
     groupadd -r cwagent >/dev/null 2>&1
     echo "create group cwagent, result: $?"
fi

if ! id cwagent >/dev/null 2>&1; then
     useradd -r -M cwagent -d /home/cwagent -g cwagent -c "Cloudwatch Agent" -s $(test -x /sbin/nologin && echo /sbin/nologin || (test -x /usr/sbin/nologin && echo /usr/sbin/nologin || (test -x /bin/false && echo /bin/false || echo /bin/sh))) >/dev/null 2>&1
     echo "create user cwagent, result: $?"
fi
