#!/bin/sh

# This script should be run after the nSite archive is unpacked, in order
# to set up the various program, library, man pages, and html 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`

echo ""
echo ""
echo "***********************************************************************"
echo "       Welcome to the nSite setup script, version $ver    "
echo "***********************************************************************"
echo "nSite generates site maps for a given site. It traverses a site from"
echo "the root URL given as the -url option and generates an HTML page"
echo "consisting of a bulleted list which reflects the structure of the site."
echo "***********************************************************************"
echo ""


# Ask where Perl is installed
echo "***********************************************************************"
echo "nSite is written entirely 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 "failed"
        echo ""
        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.003 ? 1 : 0)'
if [ $? = "1" ]; then
        echo "ERROR: Detected old Perl version. nSite requires"
        echo "Perl 5.003 or greater to run."
        echo ""
        exit
fi
echo "ok"

printf "Getopt::Long .......... "
$perl -e 'use Getopt::Long; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the Getopt::Long module. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly"
        echo ""
        exit
else
        echo "ok"
fi

printf "LWP::UserAgent ........ "
$perl -e 'use LWP::UserAgent; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the LWP::UserAgent. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly, or"
        echo "you do not have the proper module."
        echo ""
        echo "To install the proper module, issue the following command as root:"
        echo "    $perl -MCPAN -e 'install LWP::UserAgent'"
        echo ""
        exit
else
        echo "ok"
fi

printf "LWP::AuthenAgent ...... "
$perl -e 'use LWP::AuthenAgent; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the LWP::AuthenAgent. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly, or"
        echo "you do not have the proper module."
        echo ""
        echo "To install the proper module, issue the following command as root:"
        echo "    $perl -MCPAN -e 'install LWP::AuthenAgent'"
        echo ""
        exit
else
        echo "ok"
fi
printf "HTML::Entities ........ "
$perl -e 'use HTML::Entities; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the HTML::Entities. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly, or"
        echo "you do not have the proper module."
        echo ""
        echo "To install the proper module, issue the following command as root:"
        echo "    $perl -MCPAN -e 'install HTML::Entities'"
        echo ""
        exit
else
        echo "ok"
fi

printf "HTML::TreeBuilder ..... "
$perl -e 'use HTML::TreeBuilder; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the HTML::TreeBuilder. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly, or"
        echo "you do not have the proper module."
        echo ""
        echo "To install the proper module, issue the following command as root:"
        echo "    $perl -MCPAN -e 'install HTML::TreeBuilder'"
        echo ""
        exit
else
        echo "ok"
fi

printf "HTML::Summary ......... "
$perl -e 'use HTML::Summary; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the HTML::Summary. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly, or"
        echo "you do not have the proper module."
        echo ""
        echo "To install the proper module, issue the following command as root:"
        echo "    $perl -MCPAN -e 'install HTML::Summary'"
        echo ""
        exit
else
        echo "ok"
fi

printf "WWW::Robot ............ "
$perl -e 'use WWW::Robot; print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the WWW::Robot. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly, or"
        echo "you do not have the proper module."
        echo ""
        echo "To install the proper module, issue the following command as root:"
        echo "    $perl -MCPAN -e 'install WWW::Robot'"
        echo ""
        exit
else
        echo "ok"
fi

printf "Digest::MD5 ........... "
$perl -e 'use Digest::MD5 qw( md5_hex ); print "foobar\n"' 2>/dev/null | grep foobar >/dev/null
if [ $? != 0 ]; then
        echo "failed"
        echo ""
        echo "ERROR: Unable to use the Digest::MD5. Maybe $perl is"
        echo "not the Perl interpreter, or is not installed properly, or"
        echo "you do not have the proper module."
        echo ""
        echo "To install the proper module, issue the following command as root:"
        echo "    $perl -MCPAN -e 'install Digest::MD5'"
        echo ""
        exit
else
        echo "ok"
fi

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

# Copy files
printf "Creating executable ... "
echo "#!$perl" > nsite
cat src/nsite.pl >> nsite
cp src/nsitemap.pm nsitemap.pm
echo "done"


printf "Changing permissions .. "
chmod 755 nsite
chmod 644 nsitemap.pm
echo "done"
echo ""

echo "***********************************************************************"
echo "nSite has been installed successfully. You can place the man pages in"
echo "your manpage directories (i.e., /usr/man) if you have permission:"
echo "    cp ./docs/nsite.1 /usr/man/man1"
echo "    cp ./docs/nsitemap.3 /usr/man/man3"
echo ""
echo "For more information on nSite consult the man pages or the files in"
echo "the docs directories."
echo ""
echo "To use nSite without reading the docs, you can try:"
echo "   ./nsite -verbose -url http://www.mywebsite.address >./sitemap.html"
echo ""
echo "***********************************************************************"
echo ""
