]> arthur.barton.de Git - ax-make.git/blob - scripts/axify
axify: Correctly test if the README.md file already exists
[ax-make.git] / scripts / axify
1 #!/bin/bash
2 # shellcheck disable=SC2250
3 #
4 # ax-make: Alex' Simple Makefile System
5 # Copyright (c)2014-2023 Alexander Barton (alex@barton.de)
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11 #
12
13 NAME=$(basename "$0")
14
15 Usage() {
16         echo "Usage: $NAME [-2|-3|-l <lic>] [<lib-dir>]"
17         echo
18         echo "  -0           Don't create AUTHORS, COPYING and README files."
19         echo '  -2           Use the GNU GPLv2 for the COPYING file.'
20         echo '  -3           Use the GNU GPLv3 for the COPYING file.'
21         echo '  -l <lic>     Specify license to use for the COPYING file:'
22         echo '               <lic> can be "gpl2", "gpl3", "lgpl21", "lgpl3".'
23         echo '               By default an empty COPYING file is created.'
24         echo '  -m           Use Markdown, create AUTHORS.md and README.md.'
25         echo
26         echo "  <lib-dir>    Library directory. Default: current working directory."
27         echo
28         exit 2
29 }
30
31 Download() {
32         URL="$1"
33         FILE="$2"
34
35         echo "Downloading $URL to \"$FILE\" ..."
36
37         if curl --version >/dev/null 2>&1; then
38                 curl -#Lo "$FILE" "$URL" && return 0
39                 echo "Failed to download $URL! [curl]"
40                 return 1
41         fi
42
43         if wget --version >/dev/null 2>&1; then
44                 wget -qO "$FILE" --show-progress "$URL" && return 0
45                 echo "Failed to download $URL! [wget]"
46                 return 1
47         fi
48
49         echo "Can't download $URL, no download tool found!"
50         return 1
51 }
52
53 LIB_D="."
54 LICENSE=""
55 ADD_FILES=1
56 unset SUFFIX
57
58 while true; do
59         case "$1" in
60                 "-0")
61                         unset ADD_FILES
62                         ;;
63                 "-2")
64                         LICENSE="gpl2"
65                         ;;
66                 "-3")
67                         LICENSE="gpl3"
68                         ;;
69                 "-l")
70                         LICENSE="$2"
71                         shift
72                         ;;
73                 "-m")
74                         SUFFIX=".md"
75                         ;;
76                 "-"*)
77                         Usage
78                         ;;
79                 *)
80                         break
81                         ;;
82         esac
83         shift
84 done
85
86 [[ $# -gt 1 ]] && Usage
87
88 [[ -n "$1" ]] && LIB_D="$1"
89
90 if [[ -r "/usr/local/share/ax-make/Makefile.ax" ]]; then
91         MAKEFILE_AX="/usr/local/share/ax-make/Makefile.ax"
92 elif [[ -r "/usr/share/ax-make/Makefile.ax" ]]; then
93         MAKEFILE_AX="/usr/share/ax-make/Makefile.ax"
94 else
95         echo "$NAME: No source \"Makefile.ax\" found!"
96         echo "$NAME: Please check your installation of \"ax-make\" and try again."
97         exit 1
98 fi
99
100 # -- Makefile.ax --
101
102 if [[ ! -d "$LIB_D" ]]; then
103         mkdir -pv "$LIB_D" || exit 1
104 fi
105
106 target="$LIB_D/$(basename "$MAKEFILE_AX")"
107 if [[ ! -e "$target" || "$MAKEFILE_AX" -nt "$target" ]]; then
108         echo "Updating \"$target\" ..."
109         cp -v "$MAKEFILE_AX" "$target" || exit 1
110 else
111         echo "Makefile \"$target\" is up to date."
112 fi
113
114 # -- Project Makefile's ---
115
116 if [[ ! -e "Makefile" ]]; then
117 echo "Creating \"Makefile\" ..."
118 [[ "$LIB_D" != "." ]] && subdirs="$LIB_D" || subdirs=""
119 cat >"Makefile" <<EOF
120 #
121 # Makefile
122 #
123
124 SOURCE_ROOT ?= \$(CURDIR)
125
126 SUBDIRS = $subdirs
127
128 include $LIB_D/Makefile.ax
129 EOF
130 fi
131
132 if [[ "$LIB_D" != "." && ! -e "$LIB_D/Makefile" ]]; then
133 echo "Creating \"$LIB_D/Makefile\" ..."
134 cat >"$LIB_D/Makefile" <<EOF
135 #
136 # Makefile
137 #
138
139 SOURCE_ROOT ?= \$(CURDIR)/..
140
141 include Makefile.ax
142 EOF
143 fi
144
145 # --- Standard project files ---
146
147 if [[ ! -e "AUTHORS$SUFFIX" && -n "$ADD_FILES" ]]; then
148         if git --version >/dev/null 2>&1; then
149                 echo "Creating \"AUTHORS$SUFFIX\" file ..."
150                 echo "$(git config user.name) <$(git config user.email)>" >>"AUTHORS$SUFFIX"
151         fi
152 fi
153
154 if [[ ! -e COPYING ]]; then
155         LICENSE_URL=""
156         case "$LICENSE" in
157                 "")
158                         ;;
159                 "gpl2")
160                         LICENSE_URL="https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt"
161                         ;;
162                 "gpl3")
163                         LICENSE_URL="https://www.gnu.org/licenses/gpl-3.0.txt"
164                         ;;
165                 "lgpl21")
166                         LICENSE_URL="https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt"
167                         ;;
168                 "lgpl3")
169                         LICENSE_URL="https://www.gnu.org/licenses/lgpl-3.0.txt"
170                         ;;
171                 *)
172                         echo "Can't setup unknown \"$LICENSE\" license!"
173                         ;;
174         esac
175         [[ -n "$LICENSE_URL" ]] && Download "$LICENSE_URL" COPYING
176 else
177         [[ -n "$LICENSE" ]] && echo "COPYING file already exists, skipping."
178 fi
179
180 files=()
181
182 if [[ -n "$ADD_FILES" ]]; then
183         files+=(COPYING)
184         files+=("AUTHORS$SUFFIX" "README$SUFFIX")
185 fi
186
187 for f in "${files[@]}"; do
188         if [[ ! -e "$f" ]]; then
189                 echo "Creating empty \"$f\" file ..."
190                 touch "$f"
191         fi
192 done
193
194 exit 0