#!/bin/sh
# setup
# This script should be run after the HTML Tag Analyzer archive is unpacked,
# in order to set up the various html, image, and cgi files

# This program 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.
 
# This program 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

# Find distribution directory
cd `dirname $0`
dist_dir=`pwd`;
ver=`cat $dist_dir/version.txt`
autoos=2

echo ""
echo ""
echo "***********************************************************************"
echo "     Welcome to the HTML Tag Analyzer setup script, version $ver       "
echo "***********************************************************************"
echo "HTML Tag Analyzer is a web-based HTML file analysis CGI program "
echo "for Unix-like operating systems."
echo "***********************************************************************"
echo ""
echo "HTML Tag Analyzer will place the Image files into your web server "
echo "document (html) directory. Please enter the full path of your web server"
echo "document directory."
echo ""
printf "Document (html) directory [$defhtml_dir]: "
read html_dir
if [ "$html_dir" = "" ]; then
        if [ "$defhtml_dir" != "" ]; then
                html_dir=$defhtml_dir
        else
                echo "ERROR: You must specify your document directory."
                echo ""
                exit
        fi
fi
html_dir=`echo $html_dir | sed 's?.//*$??'`
abspath=`echo $html_dir | grep "^/"`
if [ "$abspath" = "" ]; then
        echo "Document directory must be an absolute path"
        echo ""
        exit
fi
if [ ! -d "$html_dir" ]; then
        echo "ERROR: Unable to find directory: $html_dir"
        echo ""
        exit
fi
printf "URL prefix for this directory [/]: "
read html_prefix
html_prefix=`echo $html_prefix | sed 's?//*$??'`
echo ""

if [ "$defcgi_dir" = "" ]; then
        defcgi_dir=`echo $html_dir | sed 's?/html$?/cgi-bin?'`
fi
echo "***********************************************************************"
echo "HTML Tag Analyzer will place the CGI and Perl files into your web server"
echo "cgi-bin (CGI) directory. You can also choose an alternate directory."
echo ""
printf "CGI directory [$defcgi_dir]: "
read cgi_dir
if [ "$cgi_dir" = "" ]; then
        if [ "$defcgi_dir" != "" ]; then
                cgi_dir=$defcgi_dir
        else
                echo "ERROR: You must specify your document root."
                echo ""
                exit
        fi
fi
cgi_dir=`echo $cgi_dir | sed 's?.//*$??'`
abspath=`echo $cgi_dir | grep "^/"`
if [ "$abspath" = "" ]; then
        echo "CGI directory must be an absolute path"
        echo ""
        exit
fi
if [ ! -d "$cgi_dir" ]; then
        echo "ERROR: Unable to find directory: $cgi_dir"
        echo ""
        exit
fi
printf "URL prefix for this directory [/cgi-bin]: "
read cgi_prefix
cgi_prefix=`echo $cgi_prefix | sed 's?//*$??'`
if [ "$cgi_prefix" = "" ]; then
        cgi_prefix=/cgi-bin
fi
echo ""

printf "Checking directories and required files .. "
# Ask for tmp directory
tmp_dir=/tmp
if [ ! -d $tmp_dir ]; then
        echo ""
        echo "***********************************************************************"
        echo "Setup cannot find your system /tmp directory. It is needed for"
        echo "temporary file storage."
        echo ""
        printf "Tmp file directory: "
        read tmp_dir
        tmp_dir=`echo $tmp_dir | sed 's?.//*$??'`
        abspath=`echo $tmp_dir | grep "^/"`
        if [ "$abspath" = "" ]; then
                echo "Tmp directory must be an absolute path"
                echo ""
                exit
        fi
        if [ ! -d $tmp_dir ]; then
                mkdir $tmp_dir
                if [ $? != 0 ]; then
                        echo "ERROR: Failed to create directory $tmp_dir"
                        echo ""
                        exit
                fi
        fi
fi
echo "Done"
echo ""

# Ask where taggrep is found, if we can't find it

if [ -x /usr/local/bin/taggrep ]; then
        taggrep=/usr/local/bin/taggrep
elif [ -x /usr/bin/taggrep ]; then
        taggrep=/usr/bin/taggrep
else
        echo "***********************************************************************"
        echo "Setup cannot find the 'taggrep' command. If you have the taggrep"
        echo "program, enter it's full path specification; otherwise leave it blank"
        echo "and setup will try another option."
        echo ""
        printf "Enter taggrep command: [] "
        read taggrep
        if [ "$taggrep" != "" ]; then
                abspath=`echo $taggrep | grep "^/"`
                if [ "$abspath" = "" ]; then
                        echo "taggrep must be an absolute path."
                        echo ""
                        exit
                fi
                if [ ! -x $taggrep ]; then
                        echo "Invalid taggrep command."
                        echo ""
                        exit
                fi
        fi
        echo ""
fi

# Ask where Perl is installed
echo "***********************************************************************"
echo "HTML Tag Analyzer is written in Perl. Please enter the full path to"
echo "the Perl 5 interpreter on your system."
echo ""
if [ -x /usr/local/bin/perl ]; then
        perldef=/usr/local/bin/perl
elif [ -x /usr/bin/perl ]; then
        perldef=/usr/bin/perl
else
        perldef=""
fi
if [ "$perldef" = "" ]; then
        printf "Full path to Perl: "
        read perl
        if [ "$perl" = "" ]; then
                echo "ERROR: No path entered!"
                echo ""
                exit
        fi
else
        printf "Full path to Perl [$perldef]: "
        read perl
        if [ "$perl" = "" ]; then
                perl=$perldef
        fi
fi
echo ""

# Test Perl 
printf "Testing Perl ...... "
if [ ! -x $perl ]; then
        echo "ERROR: Failed to find Perl at $perl"
        echo ""
        exit
fi
echo "ok"
printf "Test script ....... "
$perl -e 'print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != "0" ]; then
        echo "ERROR: Failed to run test Perl script. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly"
        echo ""
        exit
fi
echo "ok"
printf "Version check ..... "
$perl -e 'exit ($] < 5.005 ? 1 : 0)'
if [ $? = "1" ]; then
        echo "ERROR: Detected old Perl version. HTML Tag Analyzer requires"
        echo "Perl 5.005 or greater to run."
        echo ""
        exit
fi
echo "ok"
if [ "$taggrep" = "" ]; then
        printf "HTML::TokeParser .. "
        $perl -e 'use HTML::TokeParser; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
        if [ $? != "0" ]; then
                echo "failed"
                echo ""
                echo "ERROR: Perl HTML::TokeParser module not installed."
                echo "Ask your system administrator to install HTML::TokeParser;"
                echo "or if you are the sysadmin, as root issue the following command:"
                echo "    $perl -MCPAN -e 'install HTML::TokeParser'"
                echo ""
                exit
        fi
        echo "ok"
fi

echo "Perl seems to be installed ok"
echo ""

# Copy/build files
printf "Building files  ....... "
cp $dist_dir/metalogo.gif $html_dir/metalogo.gif

echo "#!$perl" > $cgi_dir/htaz.cgi
echo "\$Logo = '$html_prefix/metalogo.gif';" >> $cgi_dir/htaz.cgi
if [ "$taggrep" = "" ]; then
        echo "\$TagGrepCmd = '';" >> $cgi_dir/htaz.cgi
        cat $dist_dir/toke.pl >> $cgi_dir/htaz.cgi
else
        echo "\$TagGrepCmd = '$taggrep';" >> $cgi_dir/htaz.cgi
        cat $dist_dir/taggrep.pl >> $cgi_dir/htaz.cgi
fi
cat $dist_dir/meta_subs.pl >> $cgi_dir/htaz.cgi
cat $dist_dir/meta_main.pl >> $cgi_dir/htaz.cgi
echo "done"

printf "Changing permissions .. "
chmod 755 $cgi_dir/htaz.cgi
chmod +r $html_dir/metalogo.gif
echo "done"
echo ""

echo "***********************************************************************"
echo "HTML Tag Analyzer has been installed successfully."
echo ""
echo "Use your web browser to access HTML Tag Analyzer using"
echo "  $cgi_prefix/htaz.cgi"
echo "***********************************************************************"
echo ""
