]> arthur.barton.de Git - ax-make.git/blobdiff - scripts/axify
axify: Update URLs of license files and follow HTTP redirects
[ax-make.git] / scripts / axify
index b40d9182ea2ed02561613c4827e7451f84726715..26ce49660fd886173710f59c70c79f8bf15c3eb0 100755 (executable)
@@ -1,7 +1,8 @@
-#!/bin/sh
+#!/bin/bash
+# shellcheck disable=SC2250
 #
 # ax-make: Alex' Simple Makefile System
-# Copyright (c)2014 Alexander Barton (alex@barton.de)
+# Copyright (c)2014-2023 Alexander Barton (alex@barton.de)
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
 # (at your option) any later version.
 #
 
-NAME=`basename "$0"`
+NAME=$(basename "$0")
 
-[ -n "$1" ] && LIB_D="$1" || LIB_D="."
+Usage() {
+       echo "Usage: $NAME [-2|-3|-l <lic>] [<lib-dir>]"
+       echo
+       echo "  -0           Don't create AUTHORS, COPYING and README files."
+       echo '  -2           Use the GNU GPLv2 for the COPYING file.'
+       echo '  -3           Use the GNU GPLv3 for the COPYING file.'
+       echo '  -l <lic>     Specify license to use for the COPYING file:'
+       echo '               <lic> can be "gpl2", "gpl3", "lgpl21", "lgpl3".'
+       echo '               By default an empty COPYING file is created.'
+       echo '  -m           Use Markdown, create AUTHORS.md and README.md.'
+       echo
+       echo "  <lib-dir>    Library directory. Default: current working directory."
+       echo
+       exit 2
+}
 
-if [ -r "/usr/local/share/ax-make/Makefile.ax" ]; then
+Download() {
+       URL="$1"
+       FILE="$2"
+
+       echo "Downloading $URL to \"$FILE\" ..."
+
+       if curl --version >/dev/null 2>&1; then
+               curl -#Lo "$FILE" "$URL" && return 0
+               echo "Failed to download $URL! [curl]"
+               return 1
+       fi
+
+       if wget --version >/dev/null 2>&1; then
+               wget -qO "$FILE" --show-progress "$URL" && return 0
+               echo "Failed to download $URL! [wget]"
+               return 1
+       fi
+
+       echo "Can't download $URL, no download tool found!"
+       return 1
+}
+
+LIB_D="."
+LICENSE=""
+ADD_FILES=1
+unset SUFFIX
+
+while true; do
+       case "$1" in
+               "-0")
+                       unset ADD_FILES
+                       ;;
+               "-2")
+                       LICENSE="gpl2"
+                       ;;
+               "-3")
+                       LICENSE="gpl3"
+                       ;;
+               "-l")
+                       LICENSE="$2"
+                       shift
+                       ;;
+               "-m")
+                       SUFFIX=".md"
+                       ;;
+               "-"*)
+                       Usage
+                       ;;
+               *)
+                       break
+                       ;;
+       esac
+       shift
+done
+
+[[ $# -gt 1 ]] && Usage
+
+[[ -n "$1" ]] && LIB_D="$1"
+
+if [[ -r "/usr/local/share/ax-make/Makefile.ax" ]]; then
        MAKEFILE_AX="/usr/local/share/ax-make/Makefile.ax"
-elif [ -r "/usr/share/ax-make/Makefile.ax" ]; then
+elif [[ -r "/usr/share/ax-make/Makefile.ax" ]]; then
        MAKEFILE_AX="/usr/share/ax-make/Makefile.ax"
 else
        echo "$NAME: No source \"Makefile.ax\" found!"
@@ -25,46 +99,94 @@ fi
 
 # -- Makefile.ax --
 
-[ ! -d "$LIB_D" ] && mkdir -pv "$LIB_D"
-if [ "$MAKEFILE_AX" -nt "$LIB_D/`basename "$MAKEFILE_AX"`" ]; then
-       echo "Updating \"$LIB_D/`basename "$MAKEFILE_AX"`\" ..."
-       cp -v "$MAKEFILE_AX" "$LIB_D/`basename "$MAKEFILE_AX"`" || exit 1
+if [[ ! -d "$LIB_D" ]]; then
+       mkdir -pv "$LIB_D" || exit 1
+fi
+
+target="$LIB_D/$(basename "$MAKEFILE_AX")"
+if [[ ! -e "$target" || "$MAKEFILE_AX" -nt "$target" ]]; then
+       echo "Updating \"$target\" ..."
+       cp -v "$MAKEFILE_AX" "$target" || exit 1
 else
-       echo "Makefile \"$LIB_D/`basename "$MAKEFILE_AX"`\" is up to date."
+       echo "Makefile \"$target\" is up to date."
 fi
 
 # -- Project Makefile's ---
 
-if [ ! -e "Makefile" ]; then
+if [[ ! -e "Makefile" ]]; then
 echo "Creating \"Makefile\" ..."
-[ "$LIB_D" != "." ] && subdirs="$LIB_D" || subdirs=""
+[[ "$LIB_D" != "." ]] && subdirs="$LIB_D" || subdirs=""
 cat >"Makefile" <<EOF
 #
 # Makefile
 #
 
+SOURCE_ROOT ?= \$(CURDIR)
+
 SUBDIRS = $subdirs
 
 include $LIB_D/Makefile.ax
 EOF
 fi
 
-if [ "$LIB_D" != "." -a ! -e "$LIB_D/Makefile" ]; then
+if [[ "$LIB_D" != "." && ! -e "$LIB_D/Makefile" ]]; then
 echo "Creating \"$LIB_D/Makefile\" ..."
 cat >"$LIB_D/Makefile" <<EOF
 #
 # Makefile
 #
 
+SOURCE_ROOT ?= \$(CURDIR)/..
+
 include Makefile.ax
 EOF
 fi
 
 # --- Standard project files ---
 
-for f in AUTHORS COPYING README; do
-       if [ ! -e "$f" ]; then
-               echo "Creating empty \"$f\" ..."
+if [[ ! -e AUTHORS && -n "$ADD_FILES" ]]; then
+       if git --version >/dev/null 2>&1; then
+               echo "Creating \"AUTHORS$SUFFIX\" file ..."
+               echo "$(git config user.name) <$(git config user.email)>" >>"AUTHORS$SUFFIX"
+       fi
+fi
+
+if [[ ! -e COPYING ]]; then
+       LICENSE_URL=""
+       case "$LICENSE" in
+               "")
+                       ;;
+               "gpl2")
+                       LICENSE_URL="https://www.gnu.org/licenses/old-licenses/gpl-2.0.txt"
+                       ;;
+               "gpl3")
+                       LICENSE_URL="https://www.gnu.org/licenses/gpl-3.0.txt"
+                       ;;
+               "lgpl21")
+                       LICENSE_URL="https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt"
+                       ;;
+               "lgpl3")
+                       LICENSE_URL="https://www.gnu.org/licenses/lgpl-3.0.txt"
+                       ;;
+               *)
+                       echo "Can't setup unknown \"$LICENSE\" license!"
+                       ;;
+       esac
+       [[ -n "$LICENSE_URL" ]] && Download "$LICENSE_URL" COPYING
+else
+       [[ -n "$LICENSE" ]] && echo "COPYING file already exists, skipping."
+fi
+
+files=()
+
+if [[ -n "$ADD_FILES" ]]; then
+       files+=(COPYING)
+       files+=("AUTHORS$SUFFIX" "README$SUFFIX")
+fi
+
+for f in "${files[@]}"; do
+       if [[ ! -e "$f" ]]; then
+               echo "Creating empty \"$f\" file ..."
                touch "$f"
        fi
 done