aboutsummaryrefslogtreecommitdiffstats
path: root/setupdocs.sh
blob: 6450b3159e1630f245e46d968808c0935ea35880 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash

DEBUG=1
PROGNAME=$(basename $BASH_SOURCE)
DESTINATION="."
GITREF=""
DOCTOOLSDIR="doctools"
docswebtemplates="https://github.com/automotive-grade-linux/docs-webtemplate.git"
doctools="https://github.com/automotive-grade-linux/docs-tools.git"

#default branch
ref_docswebtemplate=""
ref_doctools="master"

pushd() {
    command pushd "$@" &> /dev/null
}
popd() {
     command popd "$@" &> /dev/null
}
debug() {
    [[ $DEBUG -eq 0 ]] && echo "[DEBUG]: $@" >&2
}
error() {
    echo "$@" >&2
}

gitcheckout() {
    command git checkout "$@" &> /dev/null
    if [ ! $? -eq 0 ]; then
        error "Cannot checkout: $@ does not exit"
        exit 4
    fi
}

gitclone() {
    command git clone "$@" &> /dev/null
    if [ ! $? -eq 0 ]; then
        error "Cannot clone $@ "
        exit 5
    fi
}

function usage() {
    cat <<EOF >&2
Usage: $PROGNAME [OPTIONS]... [DIRECTORY]
    --debug                               debug mode
    -d, --directory=[DST]                 directory destination; DST is the destination
    -h, --help                            print this help
    -t, --doctools-ref=[REFERENCE]        doctools reference;
                                          REFERENCE can be a branch, a tag, a commit
    -w, --webtemplate-ref=[REFERENCE]    webtemplates reference;
                                          REFERENCE can be a branch, a tag, a commit
    -b, --baseurl=[DST]                   baseurl of the site, defaut is empty
                                          DST must start with /
EOF
    exit 1
}


SHORTOPTS="w:t:d:h:b:"
LONGOPTS="webtemplate-ref:,doctools-ref:,baseurl:,directory:,debug,help"
ARGS=$(getopt -s bash --options $SHORTOPTS  \
  --longoptions $LONGOPTS --name $PROGNAME -- "$@" )
if [ ! $? -eq 0 ]; then
    exit 1
fi
eval set -- "$ARGS"

while [ "$#" -gt "1" ]; do
    case "$1" in
        -w|--webtemplate-ref)
            ref_docswebtemplate=$2;shift 2;;
        -t|--doctools-ref)
            ref_doctools=$2; shift 2;;
        -d|--directory)
            DESTINATION=$2;shift 2;;
        -b|--baseurl)
            baseurl=$2;shift 2;;
        --debug)
            DEBUG=0;shift 2;;
        -h|--help)
            usage;;
        *)
            usage;;
    esac
done

#make sure nodejs and jekyll are installed
node -v && jekyll -v
if [ ! $? -eq 0 ]; then
    error "please, make sure nodejs and jekyll are installed"
    exit 3
fi

#check writable dir
if [ ! -w $DESTINATION ]; then
    error "$DESTINATION is not a writable directory"
    exit 2
fi


pushd $DESTINATION

#get reference
[[ -d .git ]] && [[ "$(realpath $BASH_SOURCE)" == "$(realpath $(basename $BASH_SOURCE))" ]] && GITREF=$(git rev-parse HEAD)
ref_docswebtemplate=${ref_docswebtemplate:-${GITREF:-master}}
debug "ref_docswebtemplate=$ref_docswebtemplate ref_doctools=$ref_doctools"

[[ -d .git ]] && rev=$(git show-ref -s $ref_docswebtemplate | sort | uniq) && rev=${rev:-$ref_docswebtemplate}

debug "GITREF=$GITREF and rev=$rev"
#check that reference given matching with local repo
[[ "$GITREF" != "$rev" ]]  \
    && { error "Invalid reference between $ref_docswebtemplate and local repo in $DESTINATION"; exit 5; }
#processing cloning or update
if [ -z $GITREF ]; then
    echo "Cloning docwebtemplates and doctools in $DESTINATION with baseurl=${baseurl}"
    gitclone $docswebtemplates .
    gitcheckout $ref_docswebtemplate
    if [ ! -z ${baseurl} ]; then
        sed -i "s|^baseurl:.*$|baseurl: \"${baseurl}\"|g" conf/_dev.yml
    fi
    gitclone $doctools $DOCTOOLSDIR
    pushd $DOCTOOLSDIR
    gitcheckout $ref_doctools
    npm install
    popd
    echo "docwebtemplates and doctools cloned in $DESTINATION"
else
    echo "you are in docs-webtemplate: process $DOCTOOLSDIR"
    echo "so no process will be done in docs-webtemplate"
    if [ -d $DOCTOOLSDIR ]; then
        echo "$DOCTOOLSDIR already exits: process update with reference=$ref_doctools"
        pushd $DOCTOOLSDIR
        gitcheckout $ref_doctools
        git pull $doctools $ref_doctools &> /dev/null
        npm install
        popd
    else
        echo "cloning $DOCTOOLSDIR"
        gitclone $doctools $DOCTOOLSDIR
        pushd $DOCTOOLSDIR
        gitcheckout $ref_doctools
        npm install
        popd
    fi
    echo "doctools updated"
fi
popd