#!/bin/bash

# copy previously cached node modules in current directory
cp -r /home/ec2-user/iarchiv-cached-node_modules/iarchiv-pdf-worker/node_modules /home/ec2-user/node_modules/adams/tmp/iarchiv-pdf-worker/master/

npm install
STATUS=$?
if [ $STATUS -eq 0 ]; then

	# remove previous cached node modules and copy new instead
	rm -rf /home/ec2-user/iarchiv-cached-node_modules/iarchiv-pdf-worker/node_modules
	cp -r /home/ec2-user/node_modules/adams/tmp/iarchiv-pdf-worker/master/node_modules /home/ec2-user/iarchiv-cached-node_modules/iarchiv-pdf-worker/

    rm -rf /var/www/pdf-worker
    mkdir /var/www/pdf-worker
    cp -a . /var/www/pdf-worker/

    sh /var/www/pdf-worker/stopstage.sh
    sh /var/www/pdf-worker/startstage.sh

    echo "Deployment completed. Server restarted"
else
    echo "Deployment failed."
    exit 1
fi