### 本文件为OpenFOAM-6的安装脚本
### 本脚本默认将OpenFoam-6和ThirdParty放在同一目录

# 请指定OpenFOAM目录 和 ThirdParty目录的上层路径（该路径可以不存在）
OPENFOAM_ROOT=/online1/support/qiql/app/openfoam/6.0/amd/intel2022u1_gcc7.3.0
THIRDPARTY_ROOT=$OPENFOAM_ROOT

# 请指定OpenFOAM 和 ThirdParty 的版本，默认为6
OPENFOAM_VERSION=6
THIRDPARTY_VERSION=6

# 请指定OpenFOAM-6 源文件（压缩包）的路径(默认与本脚本在同一目录）
OPENFOAM_SRC=`pwd`
THIRDPARTY_SRC=$OPENFOAM_SRC

## 根据需求加载环境
your_dev="amd/intel_compiler/2022u1 amd/gcc_compiler/7.3.0"
module load $your_dev

# 指定配置完毕
################################################################################################
# 下面开始操作
 
## 创建目录
mkdir -p $OPENFOAM_ROOT
mkdir -p $THIRDPARTY_ROOT

## 解压
tar xvf $OPENFOAM_SRC/openfoam-${OPENFOAM_VERSION}.tar.gz -C $OPENFOAM_ROOT
tar xvf $THIRDPARTY_SRC/thirdparty-${THIRDPARTY_VERSION}.tar.gz -C $THIRDPARTY_ROOT

# 重命名文件夹
cd $THIRDPARTY_ROOT
mv ThirdParty-$THIRDPARTY_VERSION-version-$THIRDPARTY_VERSION ThirdParty-$THIRDPARTY_VERSION

cd $OPENFOAM_ROOT
mv OpenFOAM-$OPENFOAM_VERSION-version-$OPENFOAM_VERSION OpenFOAM-$OPENFOAM_VERSION

cd ${OPENFOAM_ROOT}/OpenFOAM-$OPENFOAM_VERSION
## 开始修改 OpenFOAM/etc/bashrc文件
sed -i "50a\export FOAM_INST_DIR=$OPENFOAM_ROOT" etc/bashrc
sed -i 's/WM_COMPILER=Gcc/WM_COMPILER=Icc/g' etc/bashrc
sed -i 's/WM_MPLIB=SYSTEMMPI/WM_MPILIB=INTELMPI/g' etc/bahrc
sed -i "118a\export WM_THIRD_PARTY_DIR=$THIRDPARTY_ROOT/ThirdParty-$THIRDPARTY_VERSION" etc/bashrc
## 修改 OpenFOAM/etc/bashrc文件完毕

## 开始修改 OpenFOAM/wmake/rules/linux64Icc目录下的c和c++ 文件
sed -i 's/cc          = icc/cc          = mpiicc/g' wmake/rules/linux64Icc/c
sed -i 's/CC          = icpc/CC          = mpiicc/g' wmake/rules/linux64Icc/c++
## 修改完毕

## source etc/bashrc文件
source etc/bashrc

## 进入ThirdParty的目录，开始编译
cd $THIRDPARTY_ROOT/ThirdParty-$THIRDPARTY_VERSION
./Allclean
./Allwmake

## 返回OpenFoam的目录，执行编译
cd $OPENFOAM_ROOT/OpenFOAM-$OPENFOAM_VERSION
./Allwmake -j 8
## -j 8 指的是采用8个核心进行编译


### END
