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