mk.sh 4.3KB

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