Don't like ads? PRO users don't see any ads ;-)
Guest

AUR Byobu updates_available

By: a guest on Nov 18th, 2010  |  syntax: Bash  |  size: 2.93 KB  |  hits: 120  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/bin/sh -e
  2. #
  3. #    updates_available: calculate and cache the number of updates available
  4. #    Copyright (C) 2008 Canonical Ltd.
  5. #
  6. #    Authors: Dustin Kirkland <kirkland@canonical.com>
  7. #
  8. #    This program is free software: you can redistribute it and/or modify
  9. #    it under the terms of the GNU General Public License as published by
  10. #    the Free Software Foundation, version 3 of the License.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19.  
  20. PKG="byobu"
  21. color 2>/dev/null || color() { true; }
  22.  
  23. if [ "$1" = "--detail" -o "$1" = "--short" ]; then
  24.         if which yaourt >/dev/null; then
  25.                 detail=`yaourt -Qu`
  26.                 if [ "$1" = "--detail" ]; then
  27.                         printf "$detail"
  28.                 else  
  29.                         short=`printf "$detail" | wc -l`
  30.                         printf "$short"
  31.                 fi
  32.         fi
  33.         exit 0
  34. fi
  35.  
  36. print_updates() {
  37.         u=$1
  38.         s=$2
  39.         if [ -n "$u" ]; then
  40.                 if [ "$u" -gt 0 ]; then
  41.                         printf "$(color b r W)%d$(color -)$(color r W)!" "$u"
  42.                         if [ -n "$s" ]; then
  43.                                 if [ "$s" -gt 0 ]; then
  44.                                         printf "!"
  45.                                 fi
  46.                         fi
  47.                         printf "$(color -) "
  48.                 fi
  49.         fi
  50. }
  51.  
  52. update_cache() {
  53.         mycache=$1
  54.         # Now we actually have to do hard computational work to calculate updates.
  55.         # Let's try to be "nice" about it:
  56.         renice 10 $$ >/dev/null 2>&1 || true
  57.         ionice -c3 -p $$ >/dev/null 2>&1 || true
  58.         # These are very computationally intensive processes.
  59.         # Background this work, have it write to the cache files,
  60.         # and let the next cache check pick up the results.
  61.         if which yaourt >/dev/null; then
  62.                 yaourt -Qu | wc -l > $mycache &
  63.         fi
  64. }
  65.  
  66. PKG="byobu"
  67.  
  68. # The following is somewhat Ubuntu and Debian specific (apt).
  69. # I would welcome contributions from other distros to make this
  70. # more distro-agnostic.
  71.  
  72. [ -d "/var/run/screen/S-$USER" ] && DIR="/var/run/screen/S-$USER" || DIR="$HOME/.byobu"
  73. mycache="$DIR/$PKG.updates-available"
  74.  
  75. # If mycache is present, use it
  76. [ -r $mycache ] && print_updates `grep "^[0-9]" $mycache | sed "s/ .*$//"`
  77.  
  78. # Update the cache if necessary
  79. if [ ! -e "$mycache" ] || [ "/var/lib/pacman" -nt "$mycache" ] || [ "/var/lib/pacman/sync" -nt "$mycache" ]; then
  80.         # If apt is newer than mycache, background an update now
  81.         update_cache "$mycache"
  82. fi