123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. test -f built.list && {
  64. cat built.list | xargs rm -f
  65. rm -f built.list
  66. } || :
  67. }
  68. dist() {
  69. local version=$(get_version)
  70. local dirname=saturnin-$version
  71. mkdir -p $dirname
  72. cp -R bin \
  73. config.mk \
  74. ffoo
  75. Makefile \
  76. README \
  77. LICENSE \
  78. setup \
  79. utils \
  80. $dirname
  81. sed -ie "s/^VERSION = .*/VERSION = $version/" $dirname/config.mk
  82. tar -cf $dirname.tar $dirname
  83. gzip $dirname.tar
  84. rm -rf $dirname
  85. }
  86. expand_includes() {
  87. #
  88. # Expand include directives
  89. #
  90. # Expand e.g. `<!-- include4: foo.sh -->` to include code of foo.sh
  91. #
  92. perl -we '
  93. use strict;
  94. my $text;
  95. while (<>) {
  96. chomp;
  97. if (m/<!-- include4: (\S+) -->/) {
  98. open my $fh, $1 or warn "cannot find: $1";
  99. my $text = do { local($/); <$fh> };
  100. close $fh;
  101. $text =~ s/^(.)/ $1/gm;
  102. chomp $text;
  103. print "$text\n";
  104. } else {
  105. print "$_\n";
  106. }
  107. }
  108. '
  109. }
  110. expand_variables() {
  111. #
  112. # Expand variable values
  113. #
  114. perl -pe "
  115. s|__SATURNIN_CONFIG_LOCAL__|$SATURNIN_CONFIG_PATH_LOCAL|;
  116. s|__SATURNIN_CONFIG_USER__|\\\$HOME/$SATURNIN_CONFIG_PATH_USER|;
  117. s|__SATURNIN_SHARE__|$shrdir|;
  118. s|__SATURNIN_LIBEXEC__|$lexdir|;
  119. s|__VERSION__|$(get_version)|;
  120. "
  121. }
  122. install() {
  123. mkdir -vp $bindir \
  124. $shrdir/ffoo \
  125. $lexdir
  126. list_of_bins | xargs cp -vrt $bindir
  127. list_of_shrs | xargs cp -vrt $shrdir/ffoo
  128. list_of_lexs | xargs cp -vrt $lexdir
  129. list_of_installed_bins | xargs chmod -v 755
  130. list_of_installed_lexs | xargs chmod -v 755
  131. test -f .autoclean && clean || :
  132. }
  133. get_version() {
  134. local version=$VERSION
  135. local stage=$STAGE
  136. if git rev-parse HEAD >&/dev/null;
  137. then # we are in git repo... so we can get smart
  138. local lasttag=$(git tag | grep ^v | sort -V | tail -n1)
  139. if ! git describe --tags --exact-match HEAD >&/dev/null;
  140. then # we are not at later commit than the last tag
  141. local sha=g$(git describe --always HEAD)
  142. fi
  143. if test "$(git diff --shortstat 2>/dev/null)" != "";
  144. then # thr tree is "dirty", i.e. has been edited
  145. local dirty=dirty
  146. fi
  147. version=${lasttag:1}
  148. local suffix=""
  149. case $stage:$sha:$dirty in
  150. ::) suffix="" ;;
  151. ::dirty) suffix="+$dirty" ;;
  152. :g*:) suffix="+$sha" ;;
  153. :g*:dirty) suffix="+$sha.dirty" ;;
  154. *::) suffix="-$stage" ;;
  155. *::dirty) suffix="-$stage+$dirty" ;;
  156. *:g*:) suffix="-$stage+$sha" ;;
  157. *:g*:dirty) suffix="-$stage+$sha.$dirty" ;;
  158. esac
  159. version=$version$suffix
  160. fi
  161. echo $version
  162. }
  163. uninstall() {
  164. list_of_installed_bins | xargs rm -vf
  165. rm -vrf $shrdir
  166. rm -vrf $lexdir
  167. }
  168. case $1 in
  169. build|clean|dist|install|install_manpages|manpages|uninstall)
  170. $1
  171. ;;
  172. *)
  173. echo "usage: $(basename $0) build|clean|dist|install|uninstall" >&2
  174. esac