#!/bin/bash
#
# Copyright (c) 2019-2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 2 as shown at https://oss.oracle.com/licenses/GPL-2.

package="olcne-agent"
subcommand="update"

read version
if [ -n "$version" ]; then
	package="$package"-"$version"
	subcommand="update-to"
fi

# First, stop the agent service.  This is done because some of the
# functions that the olcne-agent process calls can result in the
# executable file itself being locked by the kernel.  Once the
# service is stopped, it is possible to safely overwrite it.
systemctl stop olcne-agent.service

# Now update the package.
yum -y "$subcommand" "$package"

# Finally, start the agent back up again.  This is done regardless
# of whether or not the update succeeded.  Otherwise, the agent
# would stay down and manual intervention would be required
systemctl start olcne-agent.service
