#!/bin/bash

APP_DIR=nkode_landing_page
TAR_FILE=nkode_landing_page.tar

# Check if the tar file exists
if [ ! -f $TAR_FILE ]; then
  echo "Error: $TAR_FILE not found."
  exit 1
fi

# Create the target directory if it doesn't exist
mkdir -p $APP_DIR

# Extract the contents of the tar file into the target directory
tar -xvf $TAR_FILE -C $APP_DIR

# Check if extraction was successful
if [ "$(ls -A $APP_DIR)" ]; then
  echo "Extraction successful."
else
  echo "Error: No files extracted into $APP_DIR."
  exit 1
fi

# Remove the existing directory if it exists in /var/www
if [ -d /var/www/$APP_DIR ]; then
  rm -r /var/www/$APP_DIR
fi

# Move the newly extracted directory to /var/www
mv $APP_DIR /var/www

# Restart Nginx to apply changes
systemctl restart nginx

echo "Deployment successful."
