#!/bin/bash
# fontlinge_userinstall
# (c) Christian Boltz 2003 / GPL

#
# fontlinge_userinstall:
# install Fontlinge WebGUI to ~/public_html/fontlinge
#

# files that must not be overwritten
webgui_custom="custom.php"

# _new_ files which must not be accessable via webserver
htaccess_deny_files="functions.php config.inc.php config.php"

# obsolete files
webgui_obsolete="config.php _get_config.php"


function getpath {
	perl -e "use fontlinge::Config; print \$fontlinge::Config{'$1'};" || errorexit 250 "
ERROR: Cannot read configuration via perl and fontlinge::Config.
       Please check your Fontlinge installation."
}

function expandtilde {
    perl -e "use fontlinge::Filebasics; print &fontlinge_Expand_Filename('$1')" || errorexit 251 "
ERROR: Cannot expand path via perl and fontlinge::Filebasics
       Please check your Fontlinge installation."
}

function errorexit {
	echo "$2" >&2;
	exit $1;
}

HOMEDIR=~
CONFIGFILE="$HOMEDIR/.fontlinge"

DIRNAME=$HOMEDIR/public_html/ # fontlinge/ will be appended later in this script
HTMLDIR="`getpath htmldir`" || exit $?

INSTALLDIR="install -m 755 -d -v"
INSTALLBIN="install -m 755 -v"
INSTALLDATA="install -m 644 -v"

# get several file lists from fontlinge::Config
webgui_files="`getpath webgui_html`" || exit $?
webgui_binfiles="`getpath webgui_scripts`" || exit $?
webgui_images="`getpath webgui_imgs`" || exit $?


while [ -n "$1" ] ; do
	case "$1" in
		--help)
			echo '
fontlinge_userinstall [ --htmldir /home/me/public_html | --help ]

This script will install the Fontlinge WebGUI into your Apache Userdir
(usually ~/public_html).

--htmldir /home/me/htdocs
    install into /home/me/htdocs instead default dir ~/public_html.
--file /path/to/.fontlinge
    use /path/to/.fontlinge instead default file ~/.fontlinge as configfile
--help
    show this help message.
'
			exit 0
			;;
		--htmldir)
			DIRNAME="$2"
			test "$2" = "" && errorexit 254 "Option --htmldir requires a parameter."
			shift ; shift
			;;
		--file)
			CONFIGFILE="$2"
			test "$2" = "" && errorexit 254 "Option --file requires a parameter."
			shift ; shift
			;;
		*)
			errorexit 255 "ERROR: unknown option: $1
See  fontlinge_userinstall --help  for help."
			;;
	esac
done

( [ -d $DIRNAME ] || [ "$DIRNAME" = "$2" ] ) || {
    echo "$DIRNAME does not exist."
    echo "Please enter the name of your Apache UserDir (i. e. /home/me/htdocs)."
    echo -n "UserDir: "
    read -e DIRNAME
	DIRNAME="`expandtilde \"$DIRNAME\"`" || exit $?
}
DIRNAME="$DIRNAME/fontlinge"

# create WebGUI directory
test -d $DIRNAME/images || $INSTALLDIR $DIRNAME/images || errorexit 1 "ERROR: cannot create directory $DIRNAME/images"

# check for old (from Fontlinge <= 2.0.1) custom.php. Delete it if unchanged
cd "$DIRNAME" || errorexit 9 "ERROR: cannot chdir into $DIRNAME"
test -f custom.php && {
	# from Fontlinge 2.0.0, 2.0.1
	echo "ce01536589ffa3bd90f74fd753000136  custom.php" | md5sum -c &>/dev/null && \
	rm -f custom.php && echo "Removed old (unchanged) custom.php - will be updated."
	# CVS-version between 2.0.1 and 2.1
	echo "c7a882aac8835cba3b0b4cc2c9881c64  custom.php" | md5sum -c &>/dev/null && \
	rm -f custom.php && echo "Removed old (unchanged) custom.php - will be updated."
}

# cd into WebGUI _source_ directory
cd "$HTMLDIR" || errorexit 2 "ERROR: cannot chdir into $HTMLDIR"

# install files
$INSTALLDATA $webgui_files "$DIRNAME" || errorexit 3 "ERROR while installing files"
$INSTALLBIN $webgui_binfiles "$DIRNAME" || errorexit 4 "ERROR while installing files"
( cd images && $INSTALLDATA $webgui_images "$DIRNAME/images" || errorexit 5 "ERROR while installing image files" )

# install custom.php only if it doesn't exist yet
for file in $webgui_custom ; do
	if test -e "$DIRNAME/$file" ; then
		echo "$DIRNAME/$file already exists. Skipped." >&2
	else
		$INSTALLDATA $file "$DIRNAME"
	fi
done

# install/update .htaccess
if test -e "$DIRNAME/.htaccess" ; then
	echo "
-----------------------------------------------------
      $DIRNAME/.htaccess
      already exists.
      Please make sure that it is up to date.
-----------------------------------------------------
"
else
	$INSTALLDATA htaccess $DIRNAME/.htaccess || errorexit 7 "ERROR while installing .htaccess"
fi

# check for obsolete files
for file in $webgui_obsolete ; do
	test -f $DIRNAME/$file && 
	{
		echo
		echo "-----------------------------------------------------"
		echo "Warning: $DIRNAME/$file is obsolete."
		echo "         Please delete it if you don't need it any longer."
		echo "-----------------------------------------------------"
		echo
	}
done



# update .htaccess
destfile="$DIRNAME/.htaccess"
for file in $htaccess_deny_files ; do
	grep " $file>" "$destfile" >/dev/null || \
	{
		echo "Adding $file to .htaccess."
		echo -e "<files $file>\n	Deny from all\n</files>" >> "$destfile" || errorexit 8 "cannot add some lines of text to $destfile"
	}
done

grep "This file will not be overwritten" "$DIRNAME/custom.php" >/dev/null || echo "
-----------------------------------------------------
      $DIRNAME/custom.php
      was not overwritten, but may be out of date.
      Please compare it with
      $HTMLDIR/custom.php

      Make sure your custom.php contains the line
          "This file will not be overwritten"
      to avoid this warning.
-----------------------------------------------------
"

# write WebGUI path to configfile
fontlinge_config --file "$CONFIGFILE" --webgui-path "$DIRNAME" --webgui-conf "$DIRNAME/config.inc.php" || \
	errorexit 9 "Could not write WebGUI path to $CONFIGFILE"

# create empty config.inc.php if it doesn't exist already (needed to avoid
# error message when fontlinge_config calls itsself with --setperms later)
test -e "$DIRNAME/config.inc.php" || touch "$DIRNAME/config.inc.php"

# finished.
echo "
-----------------------------------------------------
      now run 'fontlinge_config' to create or
      update your .fontlinge config file
-----------------------------------------------------
"

