#!/bin/bash

OLD_BASE=$(realpath $1)
NEW_BASE=$(realpath $2)

function get_all_filepaths() {
	local result=`find ${OLD_BASE} -type f`
	echo "${result}"
}

function handle_path() {

	local filepath=$(realpath $1)
	local old_basename=$(dirname $filepath)

	local new_basename=${old_basename/${OLD_BASE}/${NEW_BASE}}
	local new_filepath=${filepath/${OLD_BASE}/${NEW_BASE}}

	if ! [[ -d $new_basename ]];
	then
		mkdir -p $new_basename
	fi
	echo "Writing file: From ${filepath} to ${new_filepath}"
	gzip -c $filepath > $new_filepath
}

all_files=$(get_all_filepaths)

echo "Migrate from ${OLD_BASE} to ${NEW_BASE}"

for f in $all_files; do
	handle_path $f
done
