#! /bin/bash

function remove_old()
{
    ## Get the previos openFPM installation
    previous_inst=$(cat $PWD/install_dir)
    
    if [ x"$previous_inst" != x"" ]; then
        echo "Found previous installation"
        if [ -d "$previous_inst/openfpm_pdata" ]; then
            echo "Removing the folder: $previous_inst/openfpm_pdata"
            rm -rf "$previous_inst/openfpm_pdata"
            if [ -d "$previous_inst/openfpm_pdata" ]; then
                echo -e "\033[91;5;1m Remove failed, manually remove the folder $previous_inst/openfpm_pdata, I am waiting ... \033[0m"
            fi
            while [ -d "$previous_inst/openfpm_pdata" ]
            do
                sleep 1
            done
            echo "Thanks"
        fi

        if [ -d "$previous_inst/openfpm_data" ]; then
            echo "Removing the folder: $previous_inst/openfpm_data"
            rm -rf "$previous_inst/openfpm_data"
            if [ -d "$previous_inst/openfpm_data" ]; then
                echo -e "\033[91;5;1m Remove failed, manually remove the folder $previous_inst/openfpm_data, I am waiting ... \033[0m"
            fi
            while [ -d "$previous_inst/openfpm_data" ]
            do
                sleep 1
            done
            echo "Thanks"
        fi
    
        if [ -d "$previous_inst/openfpm_devices" ]; then
            echo "Removing the folder: $previous_inst/openfpm_devices"
            rm -rf "$previous_inst/openfpm_devices"
            if [ -d "$previous_inst/openfpm_devices" ]; then
                echo -e "\033[91;5;1m Remove failed, manually remove the folder $previous_inst/openfpm_devices, I am waiting ... \033[0m"
            fi
            while [ -d "$previous_inst/openfpm_devices" ]
            do
                sleep 1
            done
            echo "Thanks"
        fi
    
        if [ -d "$previous_inst/openfpm_io" ]; then
            echo "Removing the folder: $previous_inst/openfpm_io"
            rm -rf "$previous_inst/openfpm_io"
            if [ -d "$previous_inst/openfpm_io" ]; then
                echo -e "\033[91;5;1m Remove failed, manually remove the folder $previous_inst/openfpm_io, I am waiting ... \033[0m"
            fi
            while [ -d "$previous_inst/openfpm_io" ]
            do
                sleep 1
            done
            echo "Thanks"
        fi
    
        if [ -d "$previous_inst/openfpm_vcluster" ]; then
            echo "Removing the folder: $previous_inst/openfpm_vcluster"
            rm -rf "$previous_inst/openfpm_vcluster"
            if [ -d "$previous_inst/openfpm_vcluster" ]; then
                echo -e "\033[91;5;1m Remove failed, manually remove the folder $previous_inst/openfpm_vcluster, I am waiting ... \033[0m"
            fi
            while [ -d "$previous_inst/openfpm_vcluster" ]
            do
                sleep 1
            done
            echo "Thanks"
        fi
    fi
    
    ## Check the installed version of the dependencies
  
    if [ -d $1/BOOST ]; then
    	is_update=$(cat $1/BOOST/include/boost/version.hpp | grep "#define BOOST_VERSION 106000")
    	if [ x"$is_update" == x"" ]; then
                echo -e "\033[1;34;5m  --------------------------------------------------------------------------- \033[0m"
                echo -e "\033[1;34;5m  Boost has been updated to 1.60, the component will be updated automatically \033[0m"
                echo -e "\033[1;34;5m  --------------------------------------------------------------------------- \033[0m"
		sleep 5
        	rm -rf $1/BOOST/include
        	rm -rf $1/BOOST/lib
		rm -rf $1/BOOST
    	fi
    fi

    ## check if the METIS directory exist and the METIS version installed

    if [ -d $1/METIS ]; then
    	version=$(cat $1/METIS/version)
    	if [ x"$version" != x"1"  ]; then
                echo -e "\033[1;34;5m  ------------------------------------------------------------------- \033[0m"
		echo -e "\033[1;34;5m  METIS has been updated, the component will be updated automatically \033[0m"
		echo -e "\033[1;34;5m  ------------------------------------------------------------------- \033[0m"
		sleep 5
		rm -rf $1/METIS/include
        	rm -rf $1/METIS/lib
        	rm -rf $1/METIS
		fi
	fi
	
    if [ -d $1/PARMETIS ]; then
        version=$(cat $1/PARMETIS/version)
        if [ x"$version" != x"1"  ]; then
                echo -e "\033[1;34;5m  ---------------------------------------------------------------------- \033[0m"
                echo -e "\033[1;34;5m  PARMETIS has been updated, the component will be updated automatically \033[0m"
                echo -e "\033[1;34;5m  ---------------------------------------------------------------------- \033[0m"
                sleep 5
                rm -rf $1/PARMETIS/include
                rm -rf $1/PARMETIS/lib
                rm -rf $1/PARMETIS/bin
                rm -rf $1/PARMETIS
        fi
    fi

    if [ -d $1/MPI ]; then
        version=$(cat $1/MPI/version)
        if [ x"$version" != x"1"  ]; then
                echo -e "\033[1;34;5m  ---------------------------------------------------------------------- \033[0m"
                echo -e "\033[1;34;5m  MPI has been updated, the component will be updated automatically      \033[0m"
                echo -e "\033[1;34;5m  ---------------------------------------------------------------------- \033[0m"
                sleep 5
                rm -rf $1/MPI/include
                rm -rf $1/MPI/lib
                rm -rf $1/MPI/bin
                rm -rf $1/MPI/etc
		rm -rf $1/MPI/share
                rm -rf $1/MPI

                rm -rf $1/HDF5
        fi
    fi
}