#! /bin/sh
#+
#
# .. program:: linkchecker.sh
#
# ==============
# linkchecker.sh
# ==============
#
# ---------------------------------------
# check links of a given directory or url
# ---------------------------------------
#
# SYNOPSIS
# ========
#
# .. code-block:: sh
#
#    linkchecker.sh -d dircheck -u url
#
# DESCRIPTION
# ===========
#
# .. option:: -d <dircheck>
# .. option:: -u <url>
#
# check links of a given directory or url
# cf. install.sh
#
# EXAMPLES
# ========
#
# TODO
# ====
#
# ++ linkchecker ne voit pas les erreurs !!
#
# ++ dirpublish forme fplod@cerbere.locean-ipsl.upmc.fr:./WWW/ par example
# donc pas http
#
# + ajouter la possibilité de faire une carte du site avec graphviz
#
# exemple syntaxe
#
# .. code-block:: sh
#
#    linkchecker -odot -v http://www.lodyc.jussieu.fr/NEMO/general/biblio_new/   | dot -Tps > sitemap.ps
#
# remove "set -u" because I don't know how to test if there is at least
# one directory AND one url to be checked without this option
#
# !! ++ must be restore ASAP
#
# EVOLUTIONS
# ==========
#
# $Id$
#
# - fplod 2008-12-19T10:05:59Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * bug fix for relative directory (thanks to Sébastien Masson)
#
# - fplod 2008-09-16T15:28:34Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * comment in reStructuredText
#
# - fplod 2007-10-12T07:32:08Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * add -u pour url
#   * add multiple -d and suppression of interactivity
#   * replace -w by -d (more generic)
#   * use rather checklink than linkchecker because the first one exist either
#     on Mac and Unix, and because the second one exists only on Mac and
#     does'nt seem to detect every problem
#
# - fplod 2007-10-11T15:31:25Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * parametrization
#   * merge with checklink.sh ++ choisir entre les deux
#
# - fplod 2007-06-19T09:26:04Z aedon.locean-ipsl.upmc.fr (Darwin)
#
#   * création
#
#-
system=$(uname)
case "${system}" in
    AIX|IRIX64)
        echo " www : no specific posix checking"
    ;;
    *)
        set -o posix
    ;;
esac
unset system
command=$(basename ${0})
log_date=$(date -u +"%Y%m%dT%H%M%SZ")
log=/tmp/$(basename ${command} .sh).log.${log_date}
#
usage=" Usage : ${command} -d dircheck -u url"
#
minargcount=2
#echo " narg ${#}"
if [ ${#} -lt ${minargcount} ]
then
    echo "eee : not enough arguments"
    echo "${usage}"
    exit 1
fi
unset minargcount
#
idircheck=0
iurl=0
while [ ${#} -gt 0 ]
do
    case ${1} in
        -d)
            idircheck=$(( ${idircheck} + 1 ))
            dircheck[${idircheck}]=$(cd ${2};pwd)
            shift
        ;;
        -u)
            iurlcheck=$(( ${iurlcheck} + 1 ))
            urlcheck[${iurlcheck}]=${2}
            shift
        ;;
    esac
    # next flag
    shift
done
#
# +++ remove temporarily
# ++ check directories or URL
#
# choose the command to be used
#
commandcheck=checklink
#
if [ ${commandcheck} = "linkchecker" ]
then
    # test if linkchecker is available
    type ${commandcheck} 1> /dev/null 2>&1
    status=${?}
    if [ ${status} -ne 0 ]
    then
        echo "${command} : eee : ${commandcheck} unavailable"
        exit 1
    fi
    optcheck="--anchors --recursion-level=-1"
fi
#
if [ ${commandcheck} = "checklink" ]
then
    # test if checklink is available
    type ${commandcheck} 1> /dev/null 2>&1
    status=${?}
    if [ ${status} -ne 0 ]
    then
        echo "${command} : eee : ${commandcheck} unavailable"
        exit 1
    fi
#
    optcheck="--summary --recursive"
fi
#
# loop on directories to be checked
dirchecksize=${#dircheck[@]} # ++ pb set -u
if [ ${dirchecksize} -gt 0 ]
then
    idircheck=1
    while [ ${idircheck} -le ${dirchecksize} ]
    do
        echo "iii : beginning of check of ${dircheck[${idircheck}]}" 1>>${log}
        fverif="file://"${dircheck[${idircheck}]}
        echo "iii : check of ${fverif}"
        ${commandcheck} ${optcheck} ${fverif} 1>>${log} 2>&1
        idircheck=$(( ${idircheck} + 1 ))
    done
fi
#
# loop on urls to be checked
urlchecksize=${#urlcheck[@]} # ++ pb set -u
if [ ${urlchecksize} -gt 0 ]
then
    iurlcheck=1
    while [ ${iurlcheck} -le ${urlchecksize} ]
    do
        echo "iii : beginning of check of ${urlcheck[${iurlcheck}]}" 1>>${log}
        # ++ test si urlcheck commence par http ou pas
        fverif=${urlcheck[${iurlcheck}]}
        echo "iii : check of ${fverif}"
        ${commandcheck} ${optcheck} ${fverif} 1>>${log} 2>&1
        iurlcheck=$(( ${iurlcheck} + 1 ))
    done
fi
#
echo "iii : log in ${log}"
# end
unset log
exit
