1#!/bin/bash
2
3# SPDX-FileCopyrightText: Copyright 2022 Arm Limited and/or its affiliates <open-source-office@arm.com>
4#
5# SPDX-License-Identifier: Apache-2.0
6#
7# Licensed under the Apache License, Version 2.0 (the License); you may
8# not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an AS IS BASIS, WITHOUT
15# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18
19usage() {
20    echo ""
21    echo "Usage: $(basename $0) <index> <src>"
22    echo " <index>  Index.html file to start link scanning at."
23    echo " <src>    Directory with doxygen source files."
24    echo ""
25}
26
27if [ ! -f "$1" ]; then
28    if [ -z "$1" ]; then
29        echo "No index file provided!" >&2
30    else
31        echo "Index file not found: '$1'" >&2
32    fi
33    usage
34    exit 1
35fi
36
37if [ ! -d "$2" ]; then
38    if [ -z "$2" ]; then
39        echo "No source directory provided!" >&2
40    else
41        echo "Source dir not found: '$2'" >&2
42    fi
43    usage
44    exit 1
45fi
46
47linkchecker -F csv --timeout 3 --check-extern $1
48
49OFS=$IFS
50IFS=$'\n'
51
52for line in $(grep -E '^[^#]' linkchecker-out.csv | tail -n +2); do
53    link=$(echo $line | cut -d';' -f 1)
54    msg=$(echo $line | cut -d';' -f 4)
55    origin=$(grep -Ern "href=['\"]${link}['\"]" $2)
56    for o in $origin; do
57        ofile=$(echo $o | cut -d':' -f 1)
58        oline=$(echo $o | cut -d':' -f 2)
59        match=$(echo $o | cut -d':' -f 3-)
60        rest="${match#*$link}"
61        ocolumn=$((${#match} - ${#rest} - ${#link}))
62        echo "$(readlink -f -n $ofile):${oline}:${ocolumn};${link};${msg};URL '${link}' results to '${msg}'" >&2
63    done
64done
65
66IFS=$OFS
67
68exit 0
69