#!/bin/bash

# Deployment script

# Set variables
destination_server="sdman@maint-data.com:/var/www/html/handlingdevices.net/.deployments/"
destination_port=20121

destination_dir="deployments"
date_stamp=$(date +"%Y.%m.%d_%H:%M:%S")
output_file="${destination_dir}/handling_${date_stamp}.tgz"
exclude_file="deployment_exclude.conf"

# Read the exclude list and prepare tar exclude arguments
exclude_args=()
while IFS= read -r line || [[ -n "$line" ]]; do
  # Skip empty lines and comments
  [[ -z "$line" || "$line" =~ ^#.*$ ]] && continue
  exclude_args+=("--exclude=$line")
done < "$exclude_file"

# Create the tarball with excluded files and folders
tar czvf "$output_file" \
    --exclude-vcs \
    "${exclude_args[@]}" \
    ./generated

if [[ $? -ne 0 ]]; then
  echo "Error creating the tarball."
  exit 2
fi

# Transfer the tarball to the destination server
scp -P "$destination_port" "$output_file" "$destination_server"

if [[ $? -ne 0 ]]; then
  echo "Error transferring the tarball to the destination server."
  exit 3
fi

echo "Deployment successful! File transferred: $output_file"
