#!/bin/sh
# setup
# This script should be run after the archive is unpacked, in order
# to setup the various program, config, and html files

# Find distribution directory and program version number
cd `dirname $0`
dist_dir=`pwd`;
ver=`cat $dist_dir/version.txt`
irix=0

echo "***********************************************************************"
echo "    Welcome to the ManViewer for LINUX setup script, version $ver"
echo "***********************************************************************"
echo "ManViewer is a web-based 'man-page' reader package for Unix-like"
echo "operating systems."
echo ""
if [ "$doc_root" = "" ]; then
        echo "***********************************************************************"
        echo "ManViewer will place the HTML and Image files into a subdirectory"
        echo "named 'manview' in your web server document root directory. If"
        echo "'manview' already exists, then you will be prompted to supply a"
        echo "different name. Please enter the full path of your web server document"
        echo "root (HTML) directory."
        echo ""
        printf "Document root (html) directory: "
        read doc_root
        if [ "$doc_root" = "" ]; then
                echo "ERROR: You must specify your document root."
                echo ""
                exit
        fi
        doc_root=`echo $doc_root | sed 's?.//*$??'`
        abspath=`echo $doc_root | grep "^/"`
        if [ "$abspath" = "" ]; then
                echo "Document root directory must be an absolute path"
                echo ""
                exit
        fi
        if [ ! -d "$doc_root" ]; then
                echo "ERROR: Unable to find directory: $doc_root"
                echo ""
                exit
        fi
        echo ""
fi
sub_dir=manview
html_dir=$doc_root/$sub_dir
if [ -d "$html_dir" ]; then
        if [ "$forcerm" = "1" ]; then
                rm -fr $html_dir
        else
                echo "Sub-Directory $sub_dir in $html_dir already exists. "
                echo "Please enter another sub-directory name."
                echo ""
                sub_dir=manview$ver
                printf "Subdirectory name [$sub_dir]: "
                read sub_dir
                sub_dir=`echo $sub_dir | sed 's?.//*$??'`
                if [ "$sub_dir" = "" ]; then
                        sub_dir=manview$ver
                fi
                html_dir=$doc_root/$sub_dir
                if [ -d "$html_dir" ]; then
                        echo "ERROR: Directory already exists."
                        echo ""
                        exit
                fi
                echo ""
        fi
fi
html_prefix=`echo $html_dir | sed -e "s#^$doc_root##"`

if [ "$cgi_dir" = "" ]; then
        cgi_def=`echo $doc_root | sed 's?/html$?/cgi-bin?'`
        echo "***********************************************************************"
        echo "ManViewer 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 [$cgi_def]: "
        read cgi_dir
        cgi_dir=`echo $cgi_dir | sed 's?.//*$??'`
        if [ "$cgi_dir" = "" ]; then
                cgi_dir=$cgi_def
        fi
        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
        echo ""
fi

if [ "$cgi_prefix" = "" ]; then
        echo "***********************************************************************"
        echo "ManViewer needs to know the CGI URL prefix of your web server"
        echo "cgi-bin (CGI) directory. If you do not use prefixes, enter a slash (/)."
        echo ""
        printf "URL prefix for cgi-bin directory [/cgi-bin]: "
        read cgi_prefix
        if [ "$cgi_prefix" = "" ]; then
                cgi_prefix=/cgi-bin
        fi
        echo""
fi
cgi_prefix=`echo $cgi_prefix | sed 's?//*$??'`

# Ask where Perl is installed
if [ "$perl" = "" ]; then
        echo "***********************************************************************"
        echo "ManViewer 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=`which perl`
                if [ $? != 0 ]; then
                        perldef=""
                fi
        fi
        printf "Full path to Perl [$perldef] : "
        read perl
        if [ "$perl" = "" ]; then
                perl=$perldef
        fi
        echo ""
fi

# 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.002 ? 1 : 0)'
if [ $? = "1" ]; then
        echo "ERROR: Detected old Perl version. ManViewer requires"
        echo "Perl 5.002 or greater to run"
        echo ""
        exit
fi
echo "ok"
echo ""
echo "Perl seems to be installed ok"
echo ""


# Ask for man2html location if we can't figure it out

if [ -x /usr/local/bin/man2html ]; then
        man2html=/usr/local/bin/man2html
elif [ -x /usr/bin/man2html ]; then
        man2html=/usr/bin/man2html
else
        echo "***********************************************************************"
        echo "Setup cannot find your man2html program. ManViewer requires"
        echo "man2html. Please enter your man2html command."
        echo ""
        printf "man2html command: "
        read man2html
        if [ "$man2html" = "" ]; then
                echo "No command entered, exiting setup."
                echo ""
                exit
        fi
        if [ ! -x $man2html ]; then
                echo "Cannot locate $man2html."
                echo ""
                exit
        fi
        echo ""
fi

# Get manpath command
if [ -x /usr/local/bin/manpath ]; then
        manpath=`/usr/local/bin/manpath`
elif [ -x /usr/bin/manpath ]; then
        manpath=`/usr/bin/manpath`
else
        manpathcmd=`which manpath 2>/dev/null`
        if [ $? != "0" ]; then
                manpath=/usr/share/catman:/usr/share/man:/usr/catman:/usr/local/man:/usr/man:/usr/X11R6/man:/usr/lib/perl5/man
        else
                manpath=`manpathcmd`
        fi
fi
echo "***********************************************************************"
echo "ManViewer will use the following default man-path. If you want to add"
echo "to this list, enter the full path specifications separated by colons"
echo "(:). If you do not want to add any to the list, just press enter."
echo ""
echo "Default man-path"
echo "  $manpath"
echo ""
printf "Additional paths: "
read manpathadd
if [ "$manpathadd" != "" ]; then
        manpathadd=`echo $manpathadd | sed 's/:$//'`
        manpath=$manpath:$manpathadd
        echo "manpath set to $manpath"
fi
echo ""

# Get zcat command
zcatcmd=`which zcat 2>/dev/null`
if [ $? != "0" ]; then
        echo "***********************************************************************"
        echo "ManViewer cannot find your zcat command. Please enter the full"
        echo "path specification to your zcat command."
        echo ""
        printf "zcat command: "
        read zcatcmd
        if [ "$zcatcmd" = "" ]; then
                echo "No command entered, exiting setup."
                exit 6
        fi
        echo ""
fi

# Get the 'man -w' command
defmanw="man -w"
echo "***********************************************************************"
echo "ManViewer requires the command to display a manual page filename."
echo "Please enter the full command. (The default is typically correct.)"
echo ""
printf "Command [$defmanw]: "
read manw
if [ "$manw" = "" ]; then
        manw=$defmanw
fi
echo ""

# Let the user known what we are going to do and ask form permission
cfile=$cgi_dir/manviewer.cgi

echo "***********************************************************************"
echo "Setup will use the following information to build manviewer. If this"
echo "information is incorrect, stop and restart setup."
echo ""
echo "Perl located at  : $perl"
echo "HTML Directory   : $html_dir"
echo "CGI Directory    : $cgi_dir"
echo "CGI prefix       : $cgi_prefix/"
echo "man2html command : $man2html"
echo "Zcat command     : $zcatcmd"
echo "man -w command   : $manw"
echo "Manpath          : $manpath"
echo ""
printf "Is this information correct (y/n) [y]? "
read ok
if [ "$ok" = "" ]; then
        ok=y
fi
isok=`echo $ok | grep "^[yY]"`
if [ "$isok" = "" ]; then
        echo ""
        echo "Exiting setup without continuing at your request."
        echo ""
        exit
fi
echo ""

# Create the HTML subdirectory

printf "Creating HTML sub-directory ...... "
if [ ! -d $html_dir ]; then
        mkdir $html_dir;
        chmod 755 $html_dir
        if [ $? != 0 ]; then
                echo "ERROR: Failed to create directory $html_dir"
                echo ""
                exit
        fi
fi
echo "done"

# Copy and install files

if [ "$dist_dir" != "$html_dir" ]; then
        printf "Copying files to HTML directory .. "
        cp $dist_dir/images/*.gif $html_dir
echo "done"
fi

printf "Building files in CGI directory .. "
echo "#!$perl"                            > $cfile
echo "\$IRIX       = \"$irix\";"         >> $cfile
echo "\$MAN2HTML   = \"$man2html\";"     >> $cfile
echo "\$MAN_W      = \"$manw\";"         >> $cfile
echo "\$CGI        = \"$cgi_dir\";"      >> $cfile
echo "\$IMGDIR     = \"$html_prefix\";"  >> $cfile
echo "\$MANPATH    = \"$manpath\";"      >> $cfile
echo "\$ZCAT       = \"$zcatcmd\";"      >> $cfile
cat $dist_dir/man.pl                     >> $cfile
echo "done"

# Create Uninstall script

printf "Creating uninstaller script ...... "
ufile=$dist_dir/mv_uninstall.sh
echo "#!/bin/sh"                                                                      > $ufile
echo "printf 'Uninstalling ManViewer .. '"                                           >> $ufile
echo "rm -f $html_dir/mv_text.gif $html_dir/mv_dot.gif $html_dir/mv_lines.gif"       >> $ufile
echo "rm -f $html_dir/mv_h_img.gif $html_dir/mv_v_img.gif"                           >> $ufile
echo "rm -f $html_dir/mv_hl_img.gif $html_dir/mv_ll_img.gif $html_dir/mv_lr_img.gif" >> $ufile
echo "rm -f $html_dir/mv_ul_img.gif $html_dir/mv_ur_img.gif $html_dir/mv_vr_img.gif" >> $ufile
echo "rm -f $html_dir/mv_logo.gif $html_dir/mv_find.gif $html_dir/mv_go.gif"         >> $ufile
echo "rm -f $cgi_dir/manviewer.cgi $cfile $cgi_dir/allcommands"                      >> $ufile
echo "rm -f $dist_dir/mv_uninstall.sh"                                               >> $ufile
echo "echo Done"                                                                     >> $ufile

echo "done"

if [ "$nochmod" = "" ]; then
        chmod 755 $cgi_dir/manviewer.cgi
        chmod 700 $dist_dir/mv_uninstall.sh
fi

echo ""
echo "************************************************************************"
echo "ManViewer has been installed successfully. Use your web browser to go to"
echo ""
echo "  $cgi_prefix/manviewer.cgi"
echo ""
echo "************************************************************************"
echo ""

