#!/bin/bash
#CLOUD MOD_FCGID INSTALL
currentpath=$(pwd)
cd /tmp
echo -n "Getting mod_fcgid..."
wget "$FTPSERVERSRC/$MODULESRC"
if [ $? != 0 ]; then
{
    echo $FAILED
    exit 1
} 
fi
echo $OK

pathtosrc=$(ls | grep $archname1)
if [ -z "$pathtosrc" ]; then 
    echo $FAILED
    echo "Can't find module's sources"
    exit 1    
fi

CMAKE_SRC=tmpcmakesrc
mkdir -p $CMAKE_SRC 

tar -zxvf $archname1 -C $CMAKE_SRC
curr_path=$(pwd)
cd $CMAKE_SRC/$archname2

pt=$(pwd)                                                                                                                                                    
cd /home/cpeasyapache/src                                                                                                                                  

ls -1a | while read filelist;                                                                                                                                
do                                                                                                                                                           
res=$(echo "$filelist" | grep fcgid)                                                                                                                     
if [ -n "$res" ]; then                                                                                                                                   
   if [ -d "$res" ]; then                                                                                                                                  
      rm -rf /home/cpeasyapache/src/$filelist/*                                                                                                            
      cp -r $pt/* /home/cpeasyapache/src/$filelist/                                                                                                        
   fi                                                                                                                                                      
fi                                                                                                                                                       
done                                                                                                                                                         
cd "$pt" 

cd $pwd

rm -rf $CMAKE_SRC
rm -f $archname1
                                                                                       
echo "mod_sucgid build ok..."

#modify cpanel's apache config file
pathtoconfig1="/var/cpanel/templates/apache2"

if [ -e "$pathtoconfig1/main.default" ]; then
 sed 's/Include "\/usr\/local\/apache\/conf\/conf\.d\/\*"/ /g' "$pathtoconfig1/main.default" > main2.default
 sed 's/<Location \/whm-server-status>/Include "\/usr\/local\/apache\/conf\/conf\.d\/\*"\n<Location \/whm-server-status>/g' main2.default > main.default     
 cp main.default $pathtoconfig1
 rm main.default
 rm main2.default
 echo "Conf modified ok..."
      
else 
      
 echo "Cann't find template file"
        
fi

rebuildscript="/scripts/rebuildhttpdconf"
if [ -e "$rebuildscript" ]; then
 "$rebuildscript"
  echo "Rebuilding conf ok..."
else
  echo "Rubuilding faild..."
fi
   
        
cd $currentpath
                                                       



                          