#! /bin/bash

#
# #############################################################################
# ## Alladdin, the all-add-in web server                                     ##
# #############################################################################
#
# @author Laurent GAERTNER <garthh@bagsbug.net>
# @version $Revision$
# @modifiedby $Author$
# @lastmodified $Date$
#
# #############################################################################
# ## cp-app : Copy application files to destination                          ##
# #############################################################################
#
# LICENSE:
# Alladdin is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Alladdin is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

. /etc/alladdin/alladdin.conf

actuDir=$PWD

## Vérification des entrées

if [ -z $1 ]
then
    echo "NO APPLICATION SELECTED"
    exit 2
fi

if [ -z $2 ]
then
    echo "NO DESTINATION ACCOUNT"
    exit 2
fi

## Copie depuis le modèle

thisDir=$(cat /dev/null)
counter=0
cd $alladdinDir/skel/$1/application
for entry in $(ls -RBl | grep "^[^d|^total]" | awk '{print $NF}' | sed "s/://" | sed "s/.\//<:DIR:>/" | tail -n+2)
do
	entryDir=$(echo $entry|grep "<:DIR:>"|sed "s/<:DIR:>//")
	if [ ! -z $entryDir ] ; then
		if [ ! -d $wwwDir/$1/$2/$entryDir ] ; then
			mkdir $wwwDir/$1/$2/$entryDir
		fi
		thisDir=$entryDir"/"
	else
		counter=$(($counter+1))
		cp $alladdinDir/skel/$1/application/$thisDir$entry $wwwDir/$1/$2/$thisDir
	fi
        echo -n -e "\r$counter files in progress\r"
done
echo "Task done, $counter files copied"

## Vérouillage des fichiers de l'application (444)

thisDir=$(cat /dev/null)
counter=0
for entry in $(ls -RBl | grep "^[^d|^total]" | awk '{print $NF}' | sed "s/://" | sed "s/.\//<:DIR:>/" | tail -n+2)
do
	entryDir=$(echo $entry|grep "<:DIR:>"|sed "s/<:DIR:>//")
	if [ ! -z $entryDir ] ; then
		thisDir=$entryDir"/"
	else
        counter=$(($counter+1))
		chmod 444 $wwwDir/$1/$2/$thisDir$entry
	fi
        echo -n -e "\r$counter files in progress\r"
done
echo "Task done, $counter files protected from writing"

cd $actuDir