#!/bin/bash
Script="${0##*/}"                   # Need 'Script' for wmakeFunctions messages
scriptsDir="${0%/*}"/scripts        # wmake/scripts directory
. "$scriptsDir"/wmakeFunctions      # Source wmake functions


printInfo() {
    if [ -f "$WM_DIR"/makefiles/info ]
    then
        make --no-print-directory -f "$WM_DIR"/makefiles/info "$@"
    else
        echo "OpenFOAM environment not set?" 1>&2
        return 1
    fi
}

if [ -z "$objectsDir" ]
then
    objectsDir="$MakeDir/$WM_OPTIONS"
fi
mkdir -p "$objectsDir"

make -s -f "$WM_DIR"/makefiles/files MAKE_DIR="$MakeDir" OBJECTS_DIR="$objectsDir" "$objectsDir"/options
make -s -f "$WM_DIR"/makefiles/files MAKE_DIR="$MakeDir" OBJECTS_DIR="$objectsDir"
s=`make --dry-run -f "$WM_DIR"/makefiles/general MAKE_DIR="$MakeDir" OBJECTS_DIR="$objectsDir" $targetType`

if [ $WM_COMPILE_OPTION = "Debug" ]; then
    echo $s
fi

link_flags_extra=`echo $s | grep -oP '(?<=Xlinker)(.*?)(?=Make)'`
link_flags_extra="-Xlinker $link_flags_extra"

OF_compile_flags=`printInfo "cxxflags"`
OF_compile_flags="$OF_compile_flags -iquote."
OF_link_flags="$OF_compile_flags $link_flags_extra"

OF_project_name=`cat Make/files | grep EXE | grep -oP '(?<=/).*$'`
OF_typestr="add_executable"
if [ -z $OF_project_name ] 
then
    OF_project_name=`cat Make/files | grep LIB | grep -oP '(?<=/).*$'`
    OF_typestr="add_library"
fi

OF_sources=`cat Make/files | grep -oP '.*\.C'`
if [ $OF_typestr = "add_executable" ]
then
    OF_typestr="$OF_typestr(\${project_name} \${OF_sources})"
else
    OF_typestr="$OF_typestr(\${project_name} SHARED \${OF_sources})"
fi

str_inc=`echo $s | sed 's/ /\n/g' | grep -E '^-I' | sort | uniq |grep -oP '(?<=-I).*'`
OF_includeDir_tmp=${str_inc/%lnInclude/}
OF_includeDir=""
for inc in $OF_includeDir_tmp
do
    if [ "${inc:0:1}" != "/" ]
    then
        inc="\${CMAKE_SOURCE_DIR}/$inc"
    fi
    OF_includeDir="$OF_includeDir $inc"
done
OF_includeDir="$OF_includeDir \${CMAKE_SOURCE_DIR}/lnInclude"
OF_includeDir=`echo $OF_includeDir | sed 's/ /\n/g'`

OF_linkLib=`echo $s | sed 's/ /\n/g' | grep -E '^-l' | sort | uniq |grep -oP '(?<=-l).*'`
OF_linkDir=$WM_PROJECT_DIR/platforms/$WM_OPTIONS/lib
OF_linkDir=`echo "$OF_linkDir $FOAM_USER_LIBBIN" | sed 's/ /\n/g'`

# 写入文件 CMakeLists.txt
cat > CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 3.12)
#=== OF related variables ===
set(project_name $OF_project_name)
set(WM_COMPILE_OPTION $WM_COMPILE_OPTION)
set(OF_compile_flags "$OF_compile_flags")
set(OF_link_flags "$OF_link_flags")
set(OF_testCaseDir \${CMAKE_SOURCE_DIR}/testCase)

set(OF_sources 
$OF_sources
)
set(OF_includeDir
$OF_includeDir
)
set(OF_linkLib     
$OF_linkLib
)
set(OF_linkDir 
$OF_linkDir
)
#===         End          ===

project(\${project_name} LANGUAGES CXX C)
set(ENV{LD_LIBRARY_PATH} "\$ENV{LD_LIBRARY_PATH}:\${OF_linkDir}")


if(\${WM_COMPILE_OPTION} STREQUAL "Opt")
    set(CMAKE_BUILD_TYPE Release CACHE STRING "Release mode" FORCE)
endif()
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED)

link_directories(
    \${OF_linkDir}
)

include_directories(
    \${OF_includeDir}
    \${CMAKE_SOURCE_DIR}/lnInclude
    \${CMAKE_SOURCE_DIR}
)

${OF_typestr}

target_link_libraries( 
    \${project_name}
    PRIVATE
    \${OF_linkLib}
)
set_target_properties( \${project_name} PROPERTIES
    COMPILE_FLAGS \${OF_compile_flags}
    LINK_FLAGS \${OF_link_flags}
    WORKING_DIRECTORY \${OF_testCaseDir})
EOF