#! /bin/bash

SOURCE="$1"
DESTINATION="$2"
YOU_CAN_CLOSE="*** You can now close the window ***";

if [ ! -e "${SOURCE}" ]
then
  echo "Source directory does not exist"
  echo "${YOU_CAN_CLOSE}"
  exit 1
fi

if [ ! -e "${DESTINATION}" ]
then
  mkdir -p "${DESTINATION}";
  if [ $? -ne 0 ];
  then
    echo "Unable to create destination directory"
    echo "${YOU_CAN_CLOSE}"
    exit 2
  fi
fi

if [ "${SOURCE}" != "${DESTINATION}" ]
then
  cp -nv "${SOURCE}"/*vtk.gz "${DESTINATION}"
  cp -nv "${SOURCE}"/*vtk "${DESTINATION}"
fi

if [ $(ls -l "${DESTINATION}"/*gz 2>/dev/null | wc -l) -eq 0 ]
then
  echo "No files to uncompress"
  echo "${YOU_CAN_CLOSE}"
  exit 0
fi

for file in "${DESTINATION}"/*gz
do
  if [ -e "${file%.*}" ]
  then
    echo "Removing ${file} because ${file%.*} already exists";
    rm "${file}"
  else
    echo "Uncompressing ${file}"
    gzip -dv "${file}"
  fi
done

echo "${YOU_CAN_CLOSE}"
exit 0

