mk.sh 4.7KB

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