#!/bin/sh

# Each script - run CLI task2 foreach task's ids
#
# @package     base-app
# @category    CLI
# @version     2.0

PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if [ $1 ]
then
    # Explode the first param by delimiter /
    ARR=(${1//// })
    TASK=${ARR[0]}
    ACTION=${ARR[1]}

    # Run TASK ACTION, it should return IDS eg. 1 5 7 13 
    # Generated by echo implode(' ', array(1, 5, 7, 13));
    IDS=(`/usr/bin/php $PATH/index.php $TASK $ACTION`)
fi

if [ $2 ]
then
    # Explode the second param by delimiter /
    ARR2=(${2//// })
    TASK2=${ARR2[0]}
    ACTION2=${ARR2[1]}
    
    # If count IDS > 0
    if [ ${#IDS[@]} -gt 0 ]
    then
        # Foreach IDS run TASK2 ACTION2 ID
        for ID in "${IDS[@]}"
        do
            OUTPUT=`/usr/bin/php $PATH/index.php $TASK2 $ACTION2 $ID`

            # If debug is specified display OUTPUT
            if [ "$3" == "debug" ]
            then
                echo $OUTPUT
            fi
        done
    fi
fi

# Display failed if error occurs
if [ $? -ne 0 ]
then
    echo "Failed"
    exit 0
fi
