#!/usr/bin/env bash
#
# Copyright 2013-2024 Axel Huebl, Rene Widera, Richard Pausch
#
# This file is part of PIConGPU.
#
# PIConGPU is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# PIConGPU is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PIConGPU.
# If not, see <http://www.gnu.org/licenses/>.
#

this_dir=$(cd $(dirname $0) && pwd)

# PIConGPU prefix path
picongpu_prefix=$(cd $this_dir/.. && pwd)
# All files in the folder (no recusion or subfolders) will be copied from PIConGPU source to the input set.
default_folder_to_copy="etc/picongpu"

# Folder which get recusivly copied from the source input set into the destination input set
folder_to_clone="etc/picongpu bin include/picongpu lib validation"
files_to_copy="cmakeFlags executeOnClone"

if [ -n "$PIC_SYSTEM_TEMPLATE_PATH" ] ; then
    default_folder_to_copy="$default_folder_to_copy $PIC_SYSTEM_TEMPLATE_PATH"
else
    # Fallback to be backward compatible to the old behaviour that all content of picongpu/etc is copied into the
    # destination input set.
    # Fallback is only aktive if the environment variable 'PIC_SYSTEM_TEMPLATE_PATH' not exists.
    # All subfolder will be copied.
    full_folder_to_copy="etc/picongpu"
fi

help()
{
    echo "pic-create create a new parameter set for simulation input"
    echo "merge default picongpu parameters and a given example's input"
    echo ""
    echo "usage: pic-create [OPTION] [src_dir] dest_dir"
    echo "If no src_dir is set picongpu a default case is cloned"
    echo "If src_dir is not in the current directory, pic-create will"
    echo 'look for it in $PIC_EXAMPLES'
    echo ""
    echo "-f | --force         - merge data if destination already exists"
    echo "-h | --help          - show this help message"
    echo ""
    echo "Dependencies: rsync"
}

force_param=0

# options may be followed by one colon to indicate they have a required argument
OPTS=`getopt -o fh -l force,help -- "$@"`
if [ $? != 0 ] ; then
    # something went wrong, getopt will put out an error message for us
    exit 1
fi

eval set -- "$OPTS"

while true ; do
    case "$1" in
        -f|--force)
            force_param=1
            ;;
        -h|--help)
            echo -e "$(help)"
            exit 0
            ;;
        --) shift; break;;
    esac
    shift
done

cmake_path="$*"

if [ $# -eq 0 ] || [ $# -gt 2 ] ; then
    echo "Missing destination directory or too many directories were given." >&2
    exit 2
fi

if [ $# -eq 2 ] ; then
    src_path=$1
    dst_path=$2
    selected_default_folder_to_copy=$folder_to_clone
    # Use example directory if no source directory available at current directory
    if [[ -d $PIC_EXAMPLES/$src_path && ! -d ./$src_path ]]; then
        src_path=$PIC_EXAMPLES/$src_path
    fi
else
    src_path=$this_dir/..
    dst_path=$1
    selected_default_folder_to_copy=$default_folder_to_copy
fi

if [ ! -d "$src_path" ] ; then
    echo "$src_path not exists" >&2
fi

if [ -d "$src_path/include/picongpu" ] ; then
    src_path="$src_path"
fi

# legacy check: we removed simulation_defines/ after PIConGPU 0.3.X
if [ -d "$src_path/include/picongpu/simulation_defines" ]; then
    echo "ERROR: simulation_defines/ directory found in source!" >&2
    echo "       Please update your input directory to the new structure!" >&2
    exit 3
fi
if [ -d "$dst_path/include/picongpu/simulation_defines" ]; then
    echo "ERROR: simulation_defines/ directory found in destination!" >&2
    echo "       Please update your input directory to the new structure!" >&2
    exit 4
fi

if [ -d "$dst_path" ] && [ "$force_param" -eq 0 ] ; then
    echo "$dst_path already exists!" >&2
    read -e -p "Merge data into $dst_path [yes|NO]? : " input
    if [ -z "$input" ] ; then
        input="NO"
    fi
    input=`echo "$input" | tr '[:upper:]' '[:lower:]'`
    if [ $input != "yes" ] ; then
        echo "Abort!" >&2
        exit 5
    fi
else
    if [ ! -d "$dst_path" ] ; then
        mkdir -p "$dst_path"
    fi
fi

echo "Cloning from $src_path to $dst_path"

# first copy from PIConGPU source into the destination input set
for d in $default_folder_to_copy
do
    dir_dst="$dst_path/$d"
    dir_src="$picongpu_prefix/$d"
    if [ ! -d "$dir_dst" ] ; then
        mkdir -p "$dir_dst"
    fi
    rsync --inplace -q -avc --exclude=".*" --exclude="*/" $dir_src/ $dir_dst
done

# copy subfolder of PIConGPU source etc/picongpu into the destination input set
for copy_folder in $full_folder_to_copy
do
    dir_to_copy="$picongpu_prefix/$copy_folder"
    for d in $(ls -w 1 -d "$dir_to_copy/"*/)
    do

        dir_dst="$dst_path/$copy_folder/$(basename $d)"
        dir_src="$d"
        if [ -d "$dir_src" ] ; then
            mkdir -p "$dir_dst"
            rsync --inplace -r -q -avc --exclude=".*" "$dir_src" $dir_dst
        fi
    done
done

# copy all data from src_path if path is not picongpu default param path
if [ "$src_path" != "$picongpu_prefix" ] ; then
    for d in $selected_default_folder_to_copy
    do
        dir_dst="$dst_path/$d"
        dir_src="$src_path/$d"
        if [ -d "$dir_src" ] ; then
            mkdir -p "$dir_dst"
            rsync --inplace -r -q -avc --exclude=".*" $dir_src/ $dir_dst
        fi
    done
fi

# copy files from root of the source input set to the destination input set
for d in $files_to_copy
do
    file_dst="$dst_path/$d"
    file_src="$src_path/$d"
    if [ -f "$src_path/$d" ] ; then
       rsync --inplace -q -avc --exclude=".*" $file_src $file_dst
    fi
done

if [ -f "$src_path/executeOnClone" ] ; then
   `sh $src_path/executeOnClone $dst_path`
fi
