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