summaryrefslogtreecommitdiff
path: root/new-doc
blob: 5b8e773a606b99369b247714d3b7a6fa66f4b1ce (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/bin/bash 
here="$(cd "$(dirname "$0")";pwd)";
cd "$(dirname "$0")"/src
 
usage="new-doc - create new documents for WebCoso
 
Usage:
 
  new-doc [-k kind] [-f] <document path> <tag>...
 
The <document path> will be taken relative to
$(pwd)
 
Non-existent tags will be created.
 
Kind is a suffix to templates/du2html.
"
 
if [[ $# -eq 0 ]]; then
    >&2 echo "$usage"
    exit 1
fi
 
shopt -s extglob
 
now="$(TZ=UTC date +'%F %T')"
 
force=0
kind=''
while getopts fk: optname; do
    case "$optname" in
        f) force=1 ;;
        k) kind="$OPTARG" ;;
        ?) echo "$usage"; exit 2 ;;
    esac
done
shift $[ $OPTIND - 1 ]
 
docname="$1"
shift
declare -a tags
tags=("$@")
 
if [[ -d "$docname" && "$force" -ne 1 ]]; then
    >&2 echo "\"$docname\" already exists"
    exit 1
fi
 
docname="${docname%/}"
 
mkdir -p "$docname"
mkdir -p ../additions/"$docname"
 
langs="${LANGS:-en it}"
 
for lang in $langs; do
    {
        cat <<EOF
${docname//?/=}
$docname
${docname//?/=}
:CreationDate: $now
:Id: $docname
EOF
        if [[ ${#tags[@]} -gt 0 ]]; then
            i=0
            for tag in "${tags[@]}"; do
                tag="${tag%/}"
                if [[ $i -eq 0 ]]; then
                    echo -n ':tags: '
                else
                    echo -n '       '
                fi
                echo "$tag"
                i=$(( $i + 1 ))
            done
        fi
        echo ''
    }  > "$docname/document.${lang}.rest.txt"
    echo "/document.${lang}.du.xml" >> "${docname}/.gitignore"
done
 
ln -s "${docname//+([^\/])/..}"/../templates/du2html${kind:+"-$kind"}.xsl "$docname"/du2html.xsl
 
cd "$here"
./new-tag "${tags[@]}"