123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. #!/bin/bash
  2. # saturnin - Smart and ready desktop helper
  3. # See LICENSE file for copyright and license details.
  4. tmp=$(mktemp)
  5. sed -e 's/ = /=/' < config.mk > $tmp
  6. . $tmp
  7. rm -f $tmp
  8. bindir=${DESTDIR}${PREFIX}/bin
  9. shrdir=${DESTDIR}${PREFIX}/share/saturnin
  10. lexdir=${DESTDIR}${PREFIX}/libexec/saturnin
  11. list_of_bins() {
  12. echo bin/saturnin
  13. }
  14. list_of_lexs() {
  15. echo libexec/saturnin-czrates
  16. echo libexec/saturnin-dmenu
  17. echo libexec/saturnin-iam
  18. echo libexec/saturnin-conf
  19. echo libexec/saturnin-ip
  20. echo libexec/saturnin-ln
  21. echo libexec/saturnin-push
  22. echo libexec/saturnin-revert
  23. echo libexec/saturnin-watch
  24. echo libexec/saturnin-www
  25. }
  26. list_of_installed_bins() {
  27. list_of_bins | sed -e "s/bin/$(sed -e 's/\//\\\//g' <<<$bindir)/"
  28. }
  29. list_of_installed_lexs() {
  30. list_of_lexs | sed -e "s/libexec/$(sed -e 's/\//\\\//g' <<<$lexdir)/"
  31. }
  32. die() {
  33. for l in "$@"; do echo "$l" >&2; done
  34. exit 9
  35. }
  36. list_of_shrs() {
  37. echo ffoo/saturnin_www.sh
  38. }
  39. build1() {
  40. local srcpath dstpath
  41. srcpath=$1
  42. dstpath=${srcpath%.skel}
  43. case $dstpath in
  44. *.md) cat $srcpath \
  45. | expand_includes \
  46. | expand_variables \
  47. > $dstpath ;;
  48. *) cat $srcpath \
  49. | expand_variables \
  50. > $dstpath ;;
  51. esac
  52. echo $dstpath >> built.list
  53. }
  54. build() {
  55. local srcpath
  56. find -type f -name '*.skel' \
  57. | while read srcpath;
  58. do
  59. build1 "$srcpath"
  60. done
  61. }
  62. clean() {
  63. #
  64. # Clean up tree after building
  65. #
  66. test -f built.list && {
  67. cat built.list | xargs rm -f
  68. rm -f built.list
  69. } || :
  70. }
  71. dist() {
  72. #
  73. # Create distributable tarball
  74. #
  75. local version=$(get_version)
  76. local dirname=saturnin-$version
  77. mkdir -p $dirname
  78. cp -R bin \
  79. config.mk \
  80. ffoo \
  81. Makefile \
  82. README \
  83. LICENSE \
  84. setup \
  85. utils \
  86. $dirname
  87. sed -i -e "s/^VERSION = .*/VERSION = $version/" $dirname/config.mk
  88. tar -cf $dirname.tar $dirname
  89. gzip $dirname.tar
  90. rm -rf $dirname
  91. }
  92. expand_includes() {
  93. #
  94. # Expand include directives
  95. #
  96. # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
  97. #
  98. perl -we '
  99. use strict;
  100. my $text;
  101. while (<>) {
  102. chomp;
  103. if (m/<!-- include4: (\S+) -->/) {
  104. open my $fh, $1 or warn "cannot find: $1";
  105. my $text = do { local($/); <$fh> };
  106. close $fh;
  107. $text =~ s/^(.)/ $1/gm;
  108. chomp $text;
  109. print "$text\n";
  110. } else {
  111. print "$_\n";
  112. }
  113. }
  114. '
  115. }
  116. expand_variables() {
  117. #
  118. # Expand variable values
  119. #
  120. perl -pe "
  121. s|__SATURNIN_CONFIG_LOCAL__|$SATURNIN_CONFIG_PATH_LOCAL|;
  122. s|__SATURNIN_CONFIG_USER__|\\\$HOME/$SATURNIN_CONFIG_PATH_USER|;
  123. s|__SATURNIN_SHARE__|$shrdir|;
  124. s|__SATURNIN_LIBEXEC__|$lexdir|;
  125. s|__VERSION__|$(get_version)|;
  126. "
  127. }
  128. install() {
  129. #
  130. # Install product
  131. #
  132. mkdir -vp $bindir \
  133. $shrdir/ffoo \
  134. $lexdir
  135. list_of_bins | xargs cp -vrt $bindir
  136. list_of_shrs | xargs cp -vrt $shrdir/ffoo
  137. list_of_lexs | xargs cp -vrt $lexdir
  138. list_of_installed_bins | xargs chmod -v 755
  139. list_of_installed_lexs | xargs chmod -v 755
  140. test -f .autoclean && clean || :
  141. }
  142. get_version() {
  143. #
  144. # Build semver version string with build metadata
  145. #
  146. # Build version string from available info using following
  147. # logic:
  148. #
  149. # 1. use VERSION (from config.mk)
  150. # 2. if we are in git, override the version with last tag
  151. # 3. if set, add STAGE (from config.mk) as pre-release ID
  152. # (afer dash)
  153. # 4. if we are at a later commit than the last tag, add commit
  154. # sha1 to build metadata (after plus sign)
  155. # 5. if the tree is "dirty", i.e. has uncommited changes,
  156. # add "dirty" to build metadata
  157. #
  158. # The version is compatible with SemVer 2.0.0.
  159. #
  160. # Examples:
  161. #
  162. # myprog v1.0.7 # all clear
  163. # myprog v1.0.7-alpha # STAGE="alpha"
  164. # myprog v1.0.7-alpha+g1aef811 # ^^ + some commits after
  165. # myprog v1.0.7-alpha+g1aef811.dirty # ^^ + tree edited
  166. # myprog v1.0.7-alpha+dirty # tag OK but tree edited
  167. # myprog v1.0.7+dirty # ^^ but no stage
  168. #
  169. # Note that versions with "dirty" should be perceived as kind of
  170. # dangerous outside developer's own machine. Versions with sha1 are
  171. # safer but must not be released.
  172. #
  173. # I have considered decorating the git commit refs to make them
  174. # sort of sortable (e.g. "r1.g1aef811"), but on second thought,
  175. # I don't think it's good idea to give *any* semantics to meta-data
  176. # at all. First, there is no rule that r1<r2<r3; a commit can be
  177. # removing what other just added and one change can be split to
  178. # multiple commits. Also, the whole thing breaks anyway once you
  179. # rebase your branch (no, it's not a sin). The sole purpose of
  180. # meta-data is to *identify* the code, and provide safe path back
  181. # to tree; commit refs are already perfect for that.
  182. #
  183. local version=$VERSION
  184. local stage=$STAGE
  185. if git rev-parse HEAD >&/dev/null;
  186. then # we are in git repo... so we can get smart
  187. local lasttag=$(git tag | grep ^v | sort -V | tail -n1)
  188. if ! git describe --tags --exact-match HEAD >&/dev/null;
  189. then # we are not at later commit than the last tag
  190. local sha=g$(git log -1 --pretty=format:%h HEAD)
  191. fi
  192. if test "$(git diff --shortstat 2>/dev/null)" != "";
  193. then # thr tree is "dirty", i.e. has been edited
  194. local dirty=dirty
  195. fi
  196. version=${lasttag:1}
  197. local suffix=""
  198. case $stage:$sha:$dirty in
  199. ::) suffix="" ;;
  200. ::dirty) suffix="+$dirty" ;;
  201. :g*:) suffix="+$sha" ;;
  202. :g*:dirty) suffix="+$sha.dirty" ;;
  203. *::) suffix="-$stage" ;;
  204. *::dirty) suffix="-$stage+$dirty" ;;
  205. *:g*:) suffix="-$stage+$sha" ;;
  206. *:g*:dirty) suffix="-$stage+$sha.$dirty" ;;
  207. esac
  208. version=$version$suffix
  209. fi
  210. echo $version
  211. }
  212. uninstall() {
  213. #
  214. # Uninstall product
  215. #
  216. list_of_installed_bins | xargs rm -vf
  217. rm -vrf $shrdir
  218. rm -vrf $lexdir
  219. }
  220. case $1 in
  221. build|clean|dist|install|install_manpages|manpages|uninstall)
  222. $1
  223. ;;
  224. *)
  225. echo "usage: $(basename $0) build|clean|dist|install|uninstall" >&2
  226. esac