#!/bin/bash

osOutput=`cat /etc/os-release | grep CentOS`
echo $osOutput
if [ "$osOutput" ]
then
    echo "Jenkins is running on CentOS"

    if [[ -n "${JENKINS_GIT_TAG}" ]]; then
        echo "Prestarting test deploy instance"
        aws ec2 run-instances --launch-template LaunchTemplateId=${LAUNCH_TEMPLATE} --instance-type r5a.large --output text --query 'Instances[*].InstanceId' > EC2_ID
        aws ec2 create-tags --resources `cat EC2_ID` --tags Key=Name,Value=ice-console-test-${JENKINS_GIT_TAG}
    fi

	sudo yum -y install nodejs npm
	npm config set spin false
	npm install
	npm prune
	exit 0
else
	echo "Jenkins is not running on CentOS"
	osOutput=`cat /etc/os-release | grep Ubuntu`
fi
echo $osOutput
if [ "$osOutput" ]
then
	echo "Jenkins is running on Ubuntu"
	sudo apt-get update
	sudo curl -sL https://deb.nodesource.com/setup_6.x | sudo bash -
	sudo apt-get install --yes nodejs
	npm config set spin false
	npm install
	npm prune

else
	echo "Jenkins is not running on Ubuntu"
	exit 1
fi
