1#!/bin/bash
2
3#
4#    Copyright 2015-2018 Nest Labs Inc. All Rights Reserved.
5#
6#    Licensed under the Apache License, Version 2.0 (the "License");
7#    you may not use this file except in compliance with the License.
8#    You may obtain a copy of the License at
9#
10#    http://www.apache.org/licenses/LICENSE-2.0
11#
12#    Unless required by applicable law or agreed to in writing, software
13#    distributed under the License is distributed on an "AS IS" BASIS,
14#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15#    See the License for the specific language governing permissions and
16#    limitations under the License.
17#
18
19#
20#    Description:
21#      This file is a convenience script that will create a skeleton
22#      build layout using the nlbuild-autotools package.
23#
24
25YEAR=`date "+%Y"`
26NAME=""
27DESCRIPTION=""
28DIR=""
29VERSION="1.0"
30verbose=1
31
32#
33# usage
34#
35# Display program usage.
36#
37usage() {
38    name=`basename $0`
39
40    echo "Usage: ${name} [ options ]"
41
42    if [ $1 -ne 0 ]; then
43        echo "Try '${name} -h' for more information."
44    fi
45
46    if [ $1 -ne 1 ]; then
47        echo ""
48        echo "  -h, --help                  Print this help, then exit."
49        echo "  -I DIR                      Specify directory DIR as the root of the "
50        echo "                              nlbuild-autotools repository."
51        echo "  -v, --verbose               Verbosely report mkskeleton progress (default: yes)."
52        echo "  --package-description DESC  Specify description DESC as a the "
53        echo "                              package description."
54        echo "  --package-name NAME         Specify name NAME as the package name."
55        echo "  --package-version VERSION   Specify version VERSION as the initial "
56        echo "                              version for the package (default: ${VERSION})."
57        echo "  --package-year YEAR         Specify year YEAR as the creation "
58        echo "                              year for the package (default: ${YEAR})."
59        echo "  -q, --quiet                 Do not verbosely report mkskeleton progress (default: no)."
60
61        echo ""
62    fi
63
64    exit $1
65}
66
67#
68# log
69#
70log() {
71    if [ ! -z ${verbose} ] && [ ${verbose} -eq 1 ]; then
72        echo $*
73    fi
74}
75
76transform() {
77    local from="${1}"
78    local to="${2}"
79
80    sed                                                   \
81        -e "s,\@NLBUILD_AUTOTOOLS_STEM\@,${DIR},g"        \
82        -e "s,\@PACKAGE_DESCRIPTION\@,${DESCRIPTION},g"   \
83        -e "s,\@PACKAGE_SHORT_LOWER\@,${LOWER},g"         \
84        -e "s,\@PACKAGE_SHORT_UPPER\@,${UPPER},g"         \
85        -e "s,\@PACKAGE_YEAR\@,${YEAR},g"                 \
86        < "${from}" > "${to}"
87}
88
89populate() {
90    local nlbuild="${1}"
91    local perm=${2}
92    local file="${3}"
93    local directory="${4}"
94    local stem="${file%.*}"
95    local extension="${file##*.}"
96    local from
97    local to
98
99    if [ -r "${nlbuild}/examples/${file}" ]; then
100        from="${nlbuild}/examples/${file}"
101
102    elif [ -r "${nlbuild}/examples/${stem}-${directory}.${extension}" ]; then
103        from="${nlbuild}/examples/${stem}-${directory}.${extension}"
104
105    elif [ "${directory}" = "." ] && [ -r "${nlbuild}/examples/${stem}-toplevel.${extension}" ]; then
106        from="${nlbuild}/examples/${stem}-toplevel.${extension}"
107
108    else
109        echo "Cannot find an example file \"${file}\" for directory \"${directory}\"."
110        exit 1
111
112    fi
113
114    to="${directory}/${file}"
115
116    log "Creating \"${to}\"..."
117
118    transform "${from}" "${to}"
119
120    if [ $? = 0 ]; then
121        chmod ${perm} "${to}"
122    fi
123}
124
125#
126# link
127#
128link() {
129    local nlbuild="${1}"
130    local perm=${2}
131    local source="${3}"
132    local directory="${4}"
133    local from="${nlbuild}/scripts/${source}"
134    local to="${directory}/${source}"
135
136    log "Creating \"${to}\"..."
137
138    ln -sf "${from}" "${to}"
139}
140
141# Parse out any command line options
142
143while [ ${#} -gt 0 ]; do
144    if [ ${1} == "-h" ] || [ ${1} == "--help" ]; then
145        usage 0
146
147    elif [ ${1} == "-I" ]; then
148        DIR="${2}"
149        shift 2
150
151    elif [ ${1} == "--package-description" ]; then
152        DESCRIPTION="${2}"
153        shift 2
154
155    elif [ ${1} == "--package-name" ]; then
156        NAME="${2}"
157        shift 2
158
159        UPPER="`echo ${NAME} | tr '[[:lower:]]' '[[:upper:]]'`"
160        LOWER="`echo ${NAME} | tr '[[:upper:]]' '[[:lower:]]'`"
161
162    elif [ ${1} == "--package-version" ]; then
163        VERSION="${2}"
164        shift 2
165
166    elif [ ${1} == "--package-year" ]; then
167        YEAR="${2}"
168        shift 2
169
170    elif [ ${1} == "-q" ] || [ ${1} == "--quiet" ]; then
171        verbose=0
172        shift 1
173
174    elif [ ${1} == "-v" ] || [ ${1} == "--verbose" ]; then
175        verbose=1
176        shift 1
177
178    else
179        usage 1
180
181    fi
182done
183
184# Sanity check the command line arguments
185
186if [ -z "${DIR}" ]; then
187    echo "$0: No -I option specified. Please provide the location of the nlbuild-autotools directory."
188    exit 1
189
190fi
191
192if [ -z "${DESCRIPTION}" ]; then
193    echo "$0: Please provide a package description via --package-description."
194    exit 1
195fi
196
197if [ -z "${NAME}" ]; then
198    echo "$0: Please provide a package name via --package-name."
199    exit 1
200fi
201
202if [ -z "${VERSION}" ]; then
203    echo "$0: Please provide a package default version via --package-version."
204    exit 1
205fi
206
207if [ -z "${YEAR}" ]; then
208    echo "$0: Please provide a package creation year via --package-year."
209    exit 1
210fi
211
212# Create the skeleton directories
213
214DIRS="doc third_party include src tests"
215
216for dir in ${DIRS}; do
217    log "Creating \"${dir}\"..."
218    mkdir -p "${dir}"
219done
220
221# Populate the skeleton directories
222
223populate "${DIR}" 664 configure.ac        .
224populate "${DIR}" 775 bootstrap           .
225populate "${DIR}" 664 repos.conf          .
226populate "${DIR}" 664 Makefile.am         .
227populate "${DIR}" 664 Makefile-bootstrap  .
228populate "${DIR}" 664 Makefile.am         doc
229populate "${DIR}" 664 Doxyfile.in         doc
230populate "${DIR}" 664 Makefile.am         third_party
231populate "${DIR}" 664 Makefile.am         src
232populate "${DIR}" 664 Makefile.am         tests
233link     "${DIR}" 775 bootstrap-configure .
234
235# Create the default package version
236
237log "Creating default version ${VERSION}..."
238
239echo "${VERSION}" > ".default-version"
240