gitconfig, cproj, xbps_base, bluetooth_connect
[dotfiles_afify.git] / .scripts / create_c_proj.sh
blob86f5a44308ccf2eb888392ebb7be85bd33fd2fad
1 #!/bin/sh
3 # Create a C project:
4 # git init + files
5 # license
6 # README
7 # Makefile
8 # config.mk
9 # man page
10 # gitignore
11 # config.def.h
12 # c file
13 # util.c
14 # util.h
15 # todo.txt
17 proj=$1
18 year=$(date +'%Y')
19 mkdir "$proj"
20 cd "$proj" && git init > /dev/null
21 git remote add github git@github.com:afify/"$proj".git > /dev/null
22 git remote add repo ssh://repo.or.cz/"$proj".git > /dev/null
24 # Create gitignore
25 #==============================================================================
26 printf "%s\\n*.db\\n*.txt\\n*.log\\n*.o\\n.gitignore" "$proj" > ./.gitignore
28 # Create License
29 #==============================================================================
30 echo "BSD 3-Clause License
32 Copyright (c) $year, Hassan Afify
33 All rights reserved.
35 Redistribution and use in source and binary forms, with or without
36 modification, are permitted provided that the following conditions are met:
38 1. Redistributions of source code must retain the above copyright notice, this
39 list of conditions and the following disclaimer.
41 2. Redistributions in binary form must reproduce the above copyright notice,
42 this list of conditions and the following disclaimer in the documentation
43 and/or other materials provided with the distribution.
45 3. Neither the name of the copyright holder nor the names of its
46 contributors may be used to endorse or promote products derived from
47 this software without specific prior written permission.
49 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\"
50 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
53 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
55 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
56 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
57 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
58 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." > ./LICENSE
60 # Create Makefile
61 #==============================================================================
62 echo "# $proj
63 # See LICENSE file for copyright and license details.
65 include config.mk
67 SRC = $proj.c util.c
68 OBJ = \${SRC:.c=.o}
70 all: options $proj
72 options:
73 @echo $proj build options:
74 @echo \"CFLAGS = \${CFLAGS}\"
75 @echo \"LDFLAGS = \${LDFLAGS}\"
76 @echo \"CC = \${CC}\"
78 .c.o:
79 \${CC} -c \${CFLAGS} \$<
81 \${OBJ}: config.h config.mk
83 config.h:
84 cp config.def.h \$@
86 $proj: \${OBJ}
87 \${CC} -o \$@ \${OBJ} \${LDFLAGS}
89 clean:
90 rm -f $proj \${OBJ} $proj-\${VERSION}.tar.gz
92 dist: clean
93 mkdir -p $proj-\${VERSION}
94 cp -R LICENSE Makefile README.md config.def.h config.mk\\
95 $proj.1 util.h \${SRC} $proj-\${VERSION}
96 tar -cf $proj-\${VERSION}.tar $proj-\${VERSION}
97 gzip $proj-\${VERSION}.tar
98 rm -rf $proj-\${VERSION}
100 install: $proj
101 mkdir -p \${DESTDIR}\${PREFIX}/bin
102 cp -f $proj \${DESTDIR}\${PREFIX}/bin
103 chmod 755 \${DESTDIR}\${PREFIX}/bin/$proj
104 mkdir -p \${DESTDIR}\${MANPREFIX}/man1
105 sed \"s/VERSION/\${VERSION}/g\" < $proj.1 > \${DESTDIR}\${MANPREFIX}/man1/$proj.1
106 chmod 644 \${DESTDIR}\${MANPREFIX}/man1/$proj.1
108 uninstall:
109 rm -f \${DESTDIR}\${PREFIX}/bin/$proj\\
110 \${DESTDIR}\${MANPREFIX}/man1/$proj.1
112 .PHONY: all options clean dist install uninstall"> ./Makefile
114 # Create Readme
115 #==============================================================================
116 echo "$proj
117 ====
118 [![Language grade: C/C++]()]()
119 [![Build status]()]()
120 [![License]()]()
122 $proj is a simple {description} for unix-like systems, follows
123 the suckless [philosophy](https://suckless.org/philosophy/) and
124 [code style](https://suckless.org/coding_style/).
126 **current**
127 \`\`\`sh
128 git clone https://github.com/afify/$proj.git
129 cd $proj/
130 make
131 make install
132 \`\`\`
133 **latest release**
134 \`\`\`sh
135 wget --content-disposition \$(curl -s https://api.github.com/repos/afify/$proj/releases/latest | tr -d '\",' | awk '/tag_name/ {print \"https://github.com/afify/$proj/archive/\"\$2\".tar.gz\"}')
136 tar -xzf $proj-*.tar.gz && cd $proj-*/
137 make
138 make install
139 \`\`\`
142 \`\`\`sh
143 \$ $proj
144 \`\`\`
145 Options
146 -------
147 \`\`\`sh
148 $ $proj [-v]
149 $ man $proj
150 \`\`\`
151 | option | description |
152 |:------:|:---------------------------------------------|
153 | \`-v\` | print version. |
155 Configuration
156 -------------
157 The configuration of $proj is done by creating a custom config.h
158 and (re)compiling the source code." > ./README.md
160 # Create config.def
161 #==============================================================================
162 printf "/* See LICENSE file for copyright and license details.*/\\n
163 #ifndef CONFIG_H\\n#define CONFIG_H\\n\\n\\n\\n#endif /* CONFIG_H */\\n"> ./config.def.h
165 # Create config.mk
166 #==============================================================================
167 printf "# %s version
168 VERSION = 0.1
170 # Customize below to fit your system
172 # paths
173 PREFIX = /usr/local
174 MANPREFIX = \${PREFIX}/share/man
176 #includes and libs
177 STD = -std=c99
178 WARN = -pedantic -Wextra -Wall
179 LIBS =
181 # flags
182 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_POSIX_C_SOURCE=200809L -DVERSION=\\\\\"\${VERSION}\\\\\"
183 CFLAGS = \${STD} \${WARN} -Os \${CPPFLAGS}
184 LDFLAGS = \${LIBS}
186 # compiler and linker
187 CC = cc" "$proj" > ./config.mk
189 # Create man page
190 #==============================================================================
191 echo ".TH $proj 1 $proj\-VERSION
192 .SH NAME
193 $proj \- simple
194 .SH SYNOPSIS
195 .B $proj
196 .RB [ \-hv ]
197 .SH DESCRIPTION
198 $proj is a simple
200 .SH OPTIONS
202 .B \-h
203 prints usage help.
205 .B \-v
206 print version.
207 .SH USAGE
209 .B $proj
210 .SH CUSTOMIZATION
211 $proj is customized by creating a custom config.h and (re)compiling the source
212 code. This keeps it fast, secure and simple.
213 .SH AUTHORS
214 Hassan Afify <hassan at afify dot dev>"> ./"$proj".1
216 # Create c file
217 #==============================================================================
218 printf "/* See LICENSE file for copyright and license details. */
219 #include <stdio.h>
220 #include <stdlib.h>
222 #include \"config.h\"
223 #include \"util.h\"
225 /* macros */
226 /* typedef */
227 /* function declarations */
228 static void usage(void);
230 /* global variables */
232 /* function implementations */
233 static void
234 usage(void)
236 die(\"usage: %s -[Aacv]\\\noptions:\\\n \\\\
237 -A print all tasks, 12-hour clock format.\\\n \\\\
238 -a print all tasks.\\\n \\\\
239 -N print all upcoming tasks, 12-hour clock format.\\\n \\\\
240 -n print all upcoming tasks.\\\n \\\\
241 -c use cache file.\\\n \\\\
242 -v print version.\");
246 main(int argc, char *argv[])
249 return 0;
250 }\\n" "$proj" > ./"$proj".c
252 # Create util.c file
253 #==============================================================================
254 printf "/* See LICENSE file for copyright and license details. */
255 #include <stdarg.h>
256 #include <stdio.h>
257 #include <stdlib.h>
258 #include <string.h>
260 #include \"util.h\"
262 void *
263 ecalloc(size_t nmemb, size_t size)
265 void *p;
266 p = calloc(nmemb, size);
267 FAIL_IF(p == NULL, \"calloc\");
268 return p;
271 void
272 die(const char *fmt, ...)
274 va_list ap;
276 va_start(ap, fmt);
277 (void)vfprintf(stderr, fmt, ap);
278 va_end(ap);
280 if (fmt[0] != '\\\0' && fmt[strlen(fmt)-1] == ':') {
281 (void)fputc(' ', stderr);
282 perror(NULL);
283 } else {
284 (void)fputc('\\\n', stderr);
287 exit(EXIT_FAILURE);
288 }\\n">./util.c
290 # Create util.h file
291 #==============================================================================
292 printf "/* See LICENSE file for copyright and license details. */
294 #ifndef UTIL_H
295 #define UTIL_H
297 #define MAX(A, B) ((A) > (B) ? (A) : (B))
298 #define MIN(A, B) ((A) < (B) ? (A) : (B))
299 #define BETWEEN(X, A, B) ((A) <= (X) && (X) <= (B))
300 #define FAIL_IF(EXP, MSG) {if(EXP){fprintf(stderr, \"[\\\033[31mFAILED \\%d\\\033[0m] \\%s\\\n\", __LINE__, MSG);exit(EXIT_FAILURE);}}
302 void *ecalloc(size_t, size_t);
303 void die(const char *fmt, ...);
305 #endif /* UTIL_H */\\n">./util.h
307 # Create todo.txt
308 #==============================================================================
309 echo "### phase 1
310 - [ ] edit documentation
311 - [ ] push
312 -> # - #/#
314 ### phase 2
315 - [ ] push
316 -> # - #/#
318 ### Documentation
319 - [ ] man page
320 - [ ] README
321 - [ ] splanner.c
322 -> #/#
324 ### testing
325 - [ ] linux
326 - [ ] bsd
327 - [ ] osx
328 -> #/#
330 ### linting, code style
331 - [ ] valgrind
332 - [ ] splint checks
333 -> #/#
335 ### final review
336 -> #/#
338 ### end v0.1
339 -> #/#
341 ### Next version ideas" > ./todo.txt