#!/bin/sh
# Install BFW script
#
# version : 3.0.0
# author : bulton-fr <bulton.fr@gmail.com>

forceInstall=false
while getopts ":f" options; do
    case "${options}" in
        f)
            forceInstall=true
            ;;
    esac
done

taskFail=false
bfwPath=""

# Function to run actions if it is a forced installation
# Copy config directory
# Remove bfw directories
# This script is stopped if actions failed
#
# @return integer : 0 if not to execute
forceInstallActions()
{
    if [ "$forceInstall" = false ]; then
        return 0
    fi

    echo -n '\033[0;33mForce option : Create directory reinstallBackup\033[0m ... '
    if mkdir -p reinstallBackup ;then
        echo '\033[1;32mDone\033[0m'
    else
        echo '\033[1;31mFail\033[0m'
        exit 1
    fi

    echo -n '\033[0;33mForce option : Copy current config directory to reinstallBackup/config\033[0m ... '
    if cp -R app/config reinstallBackup/config ;then
        echo '\033[1;32mDone\033[0m'
    else
        echo '\033[1;31mFail\033[0m'
        exit 1
    fi

    echo -n '\033[0;33mForce option : Remove bfw directories\033[0m ... '
    if rm -rf app src web cli.php ;then
        echo '\033[1;32mDone\033[0m'
    else
        echo '\033[1;31mFail\033[0m'
        exit 1
    fi

    echo ''
}

# Function to create a directory
# 
# @param string : The directory name
# 
# @return integer : 0 if taskFail is true
createDirectory()
{
    if [ "$taskFail" = true ] ;then
        return 0
    fi
    
    echo -n "> Create $1 directory ..."
    
    if [ -d $1 ] && [ "$forceInstall" = false ]; then
        echo '\033[1;33m Directory exist\033[0m'
        return 0
    fi

    if mkdir -p $1 ;then
        echo '\033[1;32m Done\033[0m'
    else
        echo '\033[1;31m Fail\033[0m'
        taskFail=true
    fi
}

# Function to find the project directory path
# 
# @return integer : 0 if taskFail is true or if php is not found
searchBfwPath()
{
    if [ "$taskFail" = true ] ;then
        return 0
    fi
    
    echo -n '> Search BFW vendor directory path ...'
    
    PHP=`which php`
    if [ "$PHP" = "" ] ;then
        echo '\033[1;31m Fail\033[0m'
        taskFail=true
        return 0
    fi
    
    if bfwPath=`$PHP -r "echo dirname(dirname(realpath('$0')));"` ;then
        echo '\033[1;32m Found\033[0m'
        echo "\033[0;33mBFW path : $bfwPath\033[0m"
    else
        echo '\033[1;31m Fail\033[0m'
        taskFail=true
    fi
}

# Function to copy a file
# 
# @param string : The source file path into the bfw path
# @param string : The destination path
# 
# @return integer : 0 if taskFail is true
copyFile()
{
    if [ "$taskFail" = true ] ;then
        return 0
    fi
    
    echo -n "> Copy $1 file to $2 ..."

    if [ -f $2 ] && [ "$forceInstall" = false ]; then
        echo '\033[1;33m File exist\033[0m'
        return 0
    fi

    if cp "$bfwPath/$1" "$2" ;then
        echo '\033[1;32m Done\033[0m'
    else
        echo '\033[1;31m Fail\033[0m'
        taskFail=true
    fi
}

echo '\033[0;33mRun BFW Install\033[0m'
echo ''

## Force option
forceInstallActions

## app Directories
createDirectory "app"
createDirectory "app/config"
createDirectory "app/config/bfw"
createDirectory "app/modules"
createDirectory "app/modules/available"
createDirectory "app/modules/enabled"

## src Directories
createDirectory "src"

## web Directory
createDirectory "web"

# Define bfwPath variable to know bfw directory into the vendor
echo ''
searchBfwPath

# Copy skeleton files
echo ''
copyFile "skel/app/config/bfw/errors.php" "app/config/bfw/errors.php"
copyFile "skel/app/config/bfw/global.php" "app/config/bfw/global.php"
copyFile "skel/app/config/bfw/manifest.json" "app/config/bfw/manifest.json"
copyFile "skel/app/config/bfw/memcached.php" "app/config/bfw/memcached.php"
copyFile "skel/app/config/bfw/modules.php" "app/config/bfw/modules.php"
copyFile "skel/app/config/bfw/monolog.php" "app/config/bfw/monolog.php"
copyFile "skel/web/.htaccess" "web/.htaccess"
copyFile "skel/web/index.php" "web/index.php"

# Install status
echo ''
echo -n '\033[0;33mBFW install status : '
if [ "$taskFail" = false ] ;then
    echo '\033[1;32mSuccess\033[0m'
else
    echo '\033[1;31mFail\033[0m'
fi
