summaryrefslogtreecommitdiff
path: root/man2pdf
blob: 6e443b54e695baaeb6070804630f7e8a2fb04492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash

# Autor: Matias Linares <mlm0111@famaf.unc.edu.ar>

VIEWER=evince
PARAMS=("$@")
re='^[0-9]+$'

function usage {
    printf "$0 [CHAPTER] [MANPAGE] [FILE]\n\n"
    printf "Converts a man page to pdf. If the file to save is not defined, it will open\n"
    printf "with the PDF viewer of your choice.\n\n"

    printf "[CHAPTER] and [FILE] can be omitted.\n"
}

function isnumber {
    num=$1
    if ! [[ $num =~ $re ]]; then
        echo 0
    else
        echo 1
    fi
}

assert_manpage() {
    man -t $1 $2 > /dev/null || exit 1
}


if [ ${#PARAMS[*]} -eq 0 ] || [ ${#PARAMS[*]} -gt 3 ] ||
   [ "${PARAMS[0]}" = "-h" ] || [ "${PARAMS[0]}" = "--help" ]
then
    # Fallan los parametros o queremos ver la ayuda :p
    usage
elif [ ${#PARAMS[*]} -eq 1 ]
then
    # Un solo parametro
    assert_manpage ${PARAMS[0]}
    name="/tmp/${PARAMS[0]}.pdf"
    manpage=${PARAMS[0]}
    man -t $manpage | ps2pdf - > "$name"
    $VIEWER $name 2> /dev/null &
elif [ ${#PARAMS[*]} -eq 2 ]
then
    # Son dos parametros.
    if [ $(isnumber ${PARAMS[0]}) -eq 0 ]
    then
        # Tenemos un archivo al que escribir
        assert_manpage ${PARAMS[0]}
        manpage=${PARAMS[0]}
        name="${PARAMS[1]}"
        man -t ${PARAMS[0]} | ps2pdf - > "${PARAMS[1]}" 
    else
        # Vemos el archivo con su respectiva pagina de manual
        assert_manpage ${PARAMS[0]} ${PARAMS[1]}
        name="/tmp/${PARAMS[1]}${PARAMS[0]}.pdf"
        chapter=${PARAMS[0]}
        manpage=${PARAMS[1]}
        man -t $chapter $manpage | ps2pdf - > "$name"
        $VIEWER $name 2> /dev/null &
    fi
else
    name="${PARAMS[2]}"
    chapter=${PARAMS[0]}
    manpage=${PARAMS[1]}
    man -t $chapter $manpage | ps2pdf - > "$name"
fi

exit 0