#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)
PACKAGE="magento"

usage()
{
	echo "usage: $CMD" >&2
	exit 1
}

cleanup()
{
	rm -f $tmpfile
}

fix_perms()
{
	set -e

	echo -n "  fixing perms ($user:$group) ..."
	$CHOWN -R $user:$group $HERE
	echo -n "."
	$CHMOD -R ug+rw $HERE
	echo -n "."
	find $HERE/. -type d -print0 | xargs -0 $CHMOD 2770
	echo -n "."
	# at least these must be writeable
	for dir in app/etc var media
	do
		[ -d $dir ] && {
			$CHMOD -R ug+rw $dir
		}
	done

	echo " done"
	set +e
}

get_and_install()
{
	local stat=
	local swdir="magento"
	local file=
	local target=

	cd $instdir
	[ -s $software_file ] || {
		wgetfile $software_url/$software_file $software_file || {
			stat=$?
			rm -f $software_file
			return $stat
		}
	}

	echo "  unpacking $software_file ... "
	$software_unpack $software_file || return $?

	[ -d $swdir ] || {
		error "directory '$swdir' not found after unpack"
		return 1
	}

	fix_perms

	echo -n "  moving files ... "
	cd $swdir
	find | cpio -pduml .. >$tmpfile 2>&1 || {
		stat=$?
		tail -10 $tmpfile
		error "during file copy ..."
		return $?
	}
	cd ..
	rm -rf $swdir
	echo "ok"

	fix_perms

	return 0
}



# post install
#
post_install_tasks()
{
	local tag="XXX-DO-NOT-EDIT-XXX"

	##rm -f *htaccess.txt

	##cktag $tag bin/.htaccess	&& installfile bin-htaccess.txt bin/.htaccess
	##cktag $tag pub/.htaccess	&& installfile pub-htaccess.txt pub/.htaccess

	##cktag $tag .htaccess && {
		##if [ "X$redirect_index" == "Xtrue" ]
		##then
			##installfile redirected-htaccess.txt .htaccess
		##else
			##installfile root-htaccess.txt .htaccess
		##fi
	##}

	# this one only if not already present
	#
	##[ -f lib/LocalSite.cfg ] || {
		##installfile "LocalSite.cfg" "lib/"
	##}

	fix_perms
}


cktag()
{
	[ -f $2 ] || return 0
	grep -q "$1" $2
}


save_config()
{
	# records install infos
	#
	echo "# config saved on $(date)
#
software_url='$software_url'
software_release='$software_release'
software_file='$software_file'
software_unpack='$software_unpack'

magento_datadir='$magento_datadir'

config_allowed_ips='$config_allowed_ips'

redirect_index='$redirect_index'

"
}

install_file()
{
	TOOLKIT=kusa jtinstall --sudo "$@" $user:$group
}




# (MAIN)

export VERBOSE=true
export DEBUG=false

tmpfile=$(mktemp /tmp/$CMD-XXXXXXX)
release_file=".release"
install_file=".install"
installused_file=".install_used"

jtescape="_"">""_"	# note the tricky use of " to avoid jtconf parsing

trap "echo '*INTR*'; cleanup; exit 127" 1 2 3
trap "cleanup" EXIT


# get common www install functions
#
. www-install-lib.sh

instdir=$(pwd)

eval $(grep 'APACHE_RUN_USER=' /etc/apache2/envvars)
eval $(grep 'APACHE_RUN_GROUP=' /etc/apache2/envvars)

user=$(stat --format '%U' ..)
group=$(stat --format '%G' ..)

if [ -f XXX_FIXME_FILE_TO_CHECK ]
then
	is_new_install=false
else
	is_new_install=true
fi

# sanity checks
#
check_installdir $instdir magento magento || exit $?

# get install infos
#
for file in $release_file $install_file
do
	[ -f $file ] && {
		echo "  reading config from '$file'"
		. $file
	}
done

# defaults
#
software_url=${software_url:-$(kusa-conf $PACKAGE.repo_url)}
software_release=${software_release:-$(kusa-conf $PACKAGE.release)}
software_file=${software_file:-$(kusa-conf $PACKAGE.file)}
software_unpack=${software_unpack:-$(kusa-conf $PACKAGE.unpack_cmd)}

config_allowed_ips=${config_allowed_ips:-$(kusa-conf $PACKAGE.config_allowed_ips)}

installed_release=${installed_release:-""}
redirect_index=${redirect_index:-"false"}

for val in "$software_url" "$software_release" "$software_file" "$software_unpack"
do
	[ "$val" == "" ] && {
		error "some install infos are missing"
		exit 1
	}
done


# env for parsing
#
host=$(basename $(pwd | sed -e 's#/docs##' -e "s#/$PACKAGE##"))
[ "$host" == "default" ] && host=$(uname -n)

export magento_datadir="$HERE/data"
export magento_pubdir="$HERE/pub"

if [ "$config_allowed_ips" != "" ]
then
	export tmp_config_allowed_ips="	Allow from $config_allowed_ips"
else
	export tmp_config_allowed_ips=""
fi


# install infos
#
[ -f $install_file ] || {
	echo "No installation directives found, I will create"
	echo "the file '$install_file', edit it and relaunch $CMD"
	save_config >$install_file
	exit 0
}


echo "
  installed release: $installed_release
  latest release:    $software_release
"

[ "$installed_release" != $software_release ] && {
	echo "  installation needed"
	get_and_install
	rm -f $software_file
}


post_install_tasks

##install_default_data_main
##install_overwrite_data_main


echo "  saving configs in '$installused_file'"
save_config >$installused_file

echo "  saving release in '$release_file'"
echo "# file saved on $(date)" >$release_file
echo "installed_release=$software_release" >>$release_file

echo "done"
exit 0
