#!/bin/sh

HOMEDIR=/opt/crab
WEBROOTDIR=$HOMEDIR/var/www

usage()
{
	echo "usage :"
	echo "--vhostname  the vhost install root dir, default:<HOME>/var/vhost<HOSTNAME>/www"
	echo "--webrootdir  the web install root dir, default:<HOME>/var/www"
}

while [ "$1" != "" ]
do
	case $1 in
		--webrootdir) 
			if [ ! -z $2 ]; then
				if [ -d $HOMEDIR/var/$2 ] ; then
					WEBROOTDIR=$HOMEDIR/var/$2
				else
					if [ -d $2 ] ; then
						WEBROOTDIR=$2
					fi
				fi
				shift 2
			else
				shift 1
			fi			
			;;
		--vhostname) 
			if [ ! -z $2 ] ; then
				WEBROOTDIR=$HOMEDIR/var/vhost/$2/www
				shift 2
			else
				shift 1
			fi			
			;;
		*)
		if [ ! -z $1 ] && [ -d $HOMEDIR/var/vhost/$1/www ] ; then
			WEBROOTDIR=$HOMEDIR/var/vhost/$1/www
		fi
		shift 1
		;;
	esac
done

if [ ! -d $WEBROOTDIR ] ; then
	echo "no '$WEBROOTDIR' failed!"
	exit 1
fi 

#echo "WEBROOTDIR=$WEBROOTDIR"

CDIR=`dirname $0`
CDIR=$(cd $CDIR;pwd)
CACHEDIR=`dirname $CDIR`

WDIR=$CDIR/web

cp -rf $WDIR/* $WEBROOTDIR/

rm -rf $WEBROOTDIR/Makefile
rm -rf $WEBROOTDIR/test
rm -rf $WEBROOTDIR/build

rm -rf $WEBROOTDIR/cache/*

chown crab.root $WEBROOTDIR -R

