Portable environments files and scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
2.4KB

  1. #!/bin/sh
  2. # mblaze.env
  3. # Written in 2020 by Lucas
  4. # CC0 1.0 Universal/Public domain - No rights reserved
  5. #
  6. # To the extent possible under law, the author(s) have dedicated all
  7. # copyright and related and neighboring rights to this software to the
  8. # public domain worldwide. This software is distributed without any
  9. # warranty. You should have received a copy of the CC0 Public Domain
  10. # Dedication along with this software. If not, see
  11. # <http://creativecommons.org/publicdomain/zero/1.0/>.
  12. usage()
  13. {
  14. cat - <<. >&2
  15. Usage:
  16. ${0##*/} f|fetch
  17. ${0##*/} l|list [dirs...]
  18. ${0##*/} r|read dir [dirs...]
  19. ${0##*/} s|scan [-a] dir [dirs...]
  20. .
  21. exit 1
  22. }
  23. _mlist_i_rec_ln()
  24. {
  25. if [ $# -eq 4 ]; then
  26. printf "%6d unseen %3d flagged %6d msg %s\n" "$@"
  27. else
  28. printf "%6d unseen %3d flagged %6d msg\n" "$@"
  29. fi
  30. }
  31. _mlist_i_rec()
  32. {
  33. tuc=0
  34. tfc=0
  35. tmc=0
  36. entries=0
  37. while IFS= read -r dir; do
  38. IFS=" " read -r uc ut fc ft mc mt dummy <<EOF
  39. $(mdirs "$dir" | mlist -i | tail -n 1)
  40. EOF
  41. _mlist_i_rec_ln "$uc" "$fc" "$mc" "$dir"
  42. tuc=$((tuc + uc))
  43. tfc=$((tfc + fc))
  44. tmc=$((tmc + mc))
  45. entries=$((entries + 1))
  46. done
  47. if [ $entries -gt 1 ]; then
  48. _mlist_i_rec_ln "$tuc" "$tfc" "$tmc"
  49. fi
  50. }
  51. m_fetch_usage()
  52. {
  53. printf "Usage: %s f|fetch\n" "${0##*/}" >&2
  54. exit 1
  55. }
  56. m_read_usage()
  57. {
  58. printf "Usage: %s r|read dir [dirs...]\n" "${0##*/}" >&2
  59. exit 1
  60. }
  61. m_scan_usage()
  62. {
  63. printf "Usage: %s s|scan [-a] dir [dirs...]\n" "${0##*/}" >&2
  64. exit 1
  65. }
  66. m_fetch()
  67. {
  68. if [ $# -ne 0 ]; then
  69. m_fetch_usage
  70. fi
  71. printf "%s: fetch TBD.\n" >&2
  72. exit 1
  73. }
  74. m_list()
  75. {
  76. pwd=$(pwd)
  77. # cut(1) lists are 1-based, and +1 for leading slash
  78. pwd_offset=$((${#pwd} + 2))
  79. if [ $# -eq 0 ] && [ -n "$M_LIST_PATH" ]; then
  80. oldIFS=$IFS
  81. IFS=:
  82. set -- $M_LIST_PATH
  83. IFS=$oldIFS
  84. printf "%s\n" "$@" | sort -t / | cut -c $pwd_offset- |
  85. _mlist_i_rec
  86. else
  87. mdirs "${@:-.}" | sort -t / | cut -c $pwd_offset- | mlist -i
  88. fi
  89. }
  90. m_read()
  91. {
  92. if [ $# -eq 0 ]; then
  93. m_read_usage
  94. fi
  95. mdirs "$@" | mlist -s | msort -d | mthread | mless
  96. }
  97. m_scan()
  98. {
  99. mlist_flags=-s
  100. while getopts a flag; do
  101. case $flag in
  102. a)
  103. mlist_flags=
  104. ;;
  105. *)
  106. m_scan_usage
  107. ;;
  108. esac
  109. done
  110. shift $(($OPTIND - 1))
  111. if [ $# -eq 0 ]; then
  112. m_scan_usage
  113. fi
  114. mdirs "$@" | mlist $mlist_flags | msort -d | mthread | mseq -S | mscan
  115. }
  116. if [ $# -lt 1 ]; then
  117. usage
  118. fi
  119. cmd=$1
  120. shift
  121. case $cmd in
  122. f|fetch)
  123. m_fetch "$@"
  124. ;;
  125. l|list)
  126. m_list "$@"
  127. ;;
  128. r|read)
  129. m_read "$@"
  130. ;;
  131. s|scan)
  132. m_scan "$@"
  133. ;;
  134. *)
  135. usage
  136. ;;
  137. esac