Add README and some comments
[mapsector.git] / Makefile
index a66e17c..7a8fec5 100644 (file)
--- a/Makefile
+++ b/Makefile
+default: mapsectorsh
 
+###########################################################################
+# Standard debian rules
+
+clean::
+       fakeroot debian/rules clean
+.PHONY: clean
+
+prepare::
+.PHONY: prepare
+
+deb: prepare
+       dpkg-buildpackage -us -uc -rfakeroot -I.git -I.gitignore -I.gitmodules
+.PHONY: deb
+
+###########################################################################
+# Generate debian/changelog from git log
+
+NAME       := $(shell awk '/^Source:/{print $$2}' debian/control)
+CLOGDIR    := .git/changelogs
+CHANGELOGS := $(shell lasttag=HEAD;                                            \
+                      (git tag -l 'v*'; echo)                                  \
+                          | while read tag; do                                 \
+                             echo $(CLOGDIR)/$${tag}$${tag:+-}$$lasttag;       \
+                              lasttag=$$tag;                                   \
+                          done)
+
+# Author/Date formating could as simple as  git log --format=' -- %an <%ae>  %cD'
+# however, we want the tagger not the author and  git log  doesn't show tags, only commits ...
+$(CHANGELOGS): %:
+       @echo "generating $@"
+       @mkdir -p "$(CLOGDIR)"
+       @(                                                                                       \
+            version="$@"; version="$${version#$(CLOGDIR)/}";                                    \
+           from="$${version%-*}"; version="$${version#*-}"; to="$$version";                     \
+           if [ "$$from" = "$$to" ]; then from=""; fi;                                  \
+           release="unstable";                                                                  \
+           if [ "$$version" = "HEAD" ]; then                                                    \
+               version="`git describe --tag --match='v*' | sed -e 's/-/~/' -e 's/-/./g'`";      \
+               release="UNRELEASED";                                                            \
+           fi;                                                                                  \
+           version="$${version#v}";                                                             \
+           echo "$(NAME) ($$version) $$release; urgency=low";                                   \
+           echo;                                                                                \
+           git log --format='  * [%h] %s' $$from$${from:+..}$$to;                               \
+           date="`echo $$to | git cat-file --batch                                              \
+                      | sed -n                                                                  \
+                             -e 's/^\(tagger\|committer\) \(.*\)00$$/\2/'                       \
+                             -eT -e 'y/+-/-+/' -ep`";                                           \
+           author="$${date% * *}"; date="$${date#$$author }"; zone="$${date#* }";               \
+            date="`TZ="UTC$$zone" date -d "1/1/1970 0:0 UTC + $${date% *} seconds" --rfc-2822`"; \
+           echo; echo " -- $$author  $$date"; echo;                                             \
+       ) >$@ || (rm -f $@; false)
+.PHONY: $(word 1,$(CHANGELOGS)) # The first changelog entry must be recreated on each call
+
+debian/changelog: $(CHANGELOGS)
+       cat $^ > $@ || (rm -f $@; false)
+
+clean::
+       rm -f debian/changelog
+       rm -rf $(CLOGDIR)
+
+prepare:: debian/changelog
+
+###########################################################################
+###########################################################################
+
+prepare:: lib/00_version.sh
 lib/00_version.sh:
-       echo "version='`git describe --tag`'" >"`dirname "$0"`"/lib/00_version.sh
+       echo "version='`git describe --tag`'" >lib/00_version.sh
 .PHONY: lib/00_version.sh
+clean::
+       rm -f lib/00_version.sh
+
 
-mapsectorsh: mapsector lib/[0-9][0-9]_*.sh
+# The $(wildcard ...) return value is sorted and duplicates are
+# removed from the list of dependencies. I need to mention
+# lib/00_version.sh explicitly since it might not yet exist
+mapsectorsh: mapsector lib/00_version.sh $(wildcard lib/[0-9][0-9]_*.sh)
        @echo "generating $@"
        @(                                                      \
-       sed -n -e '1p' $<;                                      \
-       echo "#";                                               \
-       echo "# $@ automatically generated from";               \
-       for script in $^; do                                    \
+           sed -n -e '1p' $<;                                  \
+           echo "#";                                           \
+           echo "# $@ automatically generated from";           \
+           for script in $^; do                                \
                echo "#         $$script";                      \
-       done;                                                   \
-       sed -n -e '2,/^load$$/p' $<;                            \
-       for script in `ls lib/[0-9][0-9]_*.sh | sort`; do       \
+           done;                                               \
+           sed -n -e '2,/^load$$/p' $<;                        \
+           for script in $^; do                                \
+               if [ "$$script" = "$<" ]; then continue; fi;    \
                echo "#### $$script";                           \
                echo;                                           \
                cat $$script;                                   \
                echo;                                           \
-       done;                                                   \
-       sed -n -e '/^load$$/,$$p' $<;                           \
-       ) >$@
-       @sed -i -e 's/^load$$/###########################################################################/' $@
-       @chmod ugo+rx $@
-
-debian/changelog:
-       @echo "generating debian/changelog"
-       @(                                                                                        \
-       lasttag="HEAD";                                                                           \
-       (git tag -l 'v*'; echo) | while read tag; do                                              \
-               case "$$lasttag" in                                                               \
-               HEAD) version="`git describe --tag --match='v*' | sed -e 's/-/~/' -e 's/-/./g'`"; \
-                     release="UNRELEASED" ;;                                                     \
-               v*)   version="$${lasttag#v}";                                                    \
-                     release="unstable" ;;                                                       \
-               esac;                                                                             \
-               echo "mapsector ($$version) $$release; urgency=low";                              \
-               echo;                                                                             \
-               git log --pretty=oneline --abbrev-commit $$tag$${tag:+..}$$lasttag                \
-                       | while read commit description; do                                       \
-                       echo "  * [$$commit] $$description";                                      \
-               done;                                                                             \
-               echo;                                                                             \
-               date="`git log --format='%cD' $$lasttag'^!'`";                                    \
-               author="`git log --format='%an <%ae>' $$lasttag'^!'`";                            \
-               echo " -- $$author  $$date";                                                      \
-               echo;                                                                             \
-               lasttag="$$tag" ;                                                                 \
-       done;                                                                                     \
-       ) >debian/changelog
-.PHONY: debian/changelog
-
-prepare: lib/00_version.sh debian/changelog
-.PHONY: prepare
-
-clean:
+           done;                                               \
+           sed -n -e '/^load$$/,$$p' $<;                       \
+       ) >$@ || (rm -f $@; false)
+       @sed -i -e 's/^load$$/####/' -eT -eh -eG -eH -eG -eH -eG -e'y/\n/#/' $@
+       chmod ugo+rx $@
+clean::
        rm -f mapsectorsh
-       rm -f debian/changelog
-       rm -f lib/00_version.sh
-.PHONY: clean