if [ $EUID -ne 0 ]
then
    echo Aborted: Only root can add a site.
    exit -1
fi
#--------------------------------------------------------------------------------

# Source global settings
if [ -f /etc/opals/opalsrc ]
then
    . /etc/opals/opalsrc
else
    echo ERROR: /etc/opals/opalsrc not found.
    exit -2
fi

UPDATE_LOG=/var/log/opals/update.log
#--------------------------------------------------------------------------------

function checkSqlAdminLogin() {
    testFile=$opals_www/$opals_link/install/sql/test.sql

    echo -n 'Check SQL login... '
    mysql -h$sql_server_host -u$sql_addsite_user -p$sql_addsite_pass < $testFile
    if [ $? -ne 0 ]
    then
        echo $abort_msg
        exit $ERROR_INVALID_SQL_ROOT_PASSWD
    fi

    echo OK.
}
#--------------------------------------------------------------------------------

function getUpdateLog() {
    IFS=$'\r\n' GLOBIGNORE='*' command eval  'uLog=($(cat $UPDATE_LOG))'
    for ((i=0; i < ${#uLog[@]}; i++))
        do  
            uLog[$i]=$(echo ${uLog[$i]}|awk -F ' ' '{print $2}')
        done
 
}



#--------------------------------------------------------------------------------

function updateMysql() {
    sqlDir=$1
   echo "updating MySQL tables....";
   for site  in  `ls /etc/opals/conf`  ;
    do 
        uSite=$(grep "type=union" /etc/opals/conf/$site) ;
        if [ -z $uSite ] ;then 
             echo "    site:  $site"; 
             for sql in `ls $sqlDir`
                 do 
                     mysql -h$sql_server_host -u$sql_addsite_user -p$sql_addsite_pass $site< $sqlDir/$sql
                 done
                     
        fi;
    done
}
#--------------------------------------------------------------------------------

function runUpdateScript() {
    scriptDir=$1
   echo "updating data ....";
   for site  in  `ls /etc/opals/conf`  ;
        do 
            uSite=$(grep "type=union" /etc/opals/conf/$site) ;
            if [ -z $uSite ] ;then 
                echo "    site:  $site"; 
                for SCRIPT in `ls $scriptDir`
                    do 
                       PERL5LIB=/www/opals/module OPALS_CONF=/etc/opals/conf/$site $scriptDir/$SCRIPT 
                    done
            fi;
    done
}

#--------------------------------------------------------------------------------
getUpdateLog
checkSqlAdminLogin
patchUrl=$1;
tmpDir=$(mktemp -d);
tmpFile=$(mktemp);

v=$(ls -l /www/opals);
v=$(echo $v|awk -F '->' '{print $2}');
v=$(echo $v|sed "s/opals-//")
url="http://dione.scoolaid.net/src/update-$v.tbz";


if [[ $url =~ ^http:// ]]; then
    echo "download update from $url..."
    wget $url -O $tmpFile >&null
    r=$?
    if [ $r != 0 ]; then 
        echo "Invalid update file URL"
        echo "Nothing to update"
        exit 0
    fi
   
fi

if [ -f $tmpFile ]; then
    tar -xjpf $tmpFile -C $tmpDir;
    uDir="$tmpDir/update-$v"
    UPDATE=0;
    for u in `ls $uDir`;
        do
            if [[ " ${uLog[*]} " != *" $u "* ]]; then
                echo "applying update $u"; 
                UPDATE=1
                for d in `ls $uDir/$u/code`
                    do
                        rsync -a $uDir/$u/code/$d /www/opals
                    done
                if [ "$(ls -A $uDir/$u/sql)" ]; then    
                    updateMysql "$uDir/$u/sql"
                fi    
                if [ "$(ls -A $uDir/$u/script)" ]; then    
                    runUpdateScript "$uDir/$u/script"
                fi    
                echo `date +%Y%m%d-%H%M%S` $u >> $UPDATE_LOG
            fi
            
        done
   if [ $UPDATE == 0 ]; 
        then 
            echo "Opals is already up-to-date.";
   else echo "Update Completed."         
   fi

fi;

#--------------------------------------------------------------------------------

 


