1. #!/bin/bash
  2.  
  3. # apter
  4. # simplify aptosid package installation and updates
  5. # Copyright (c) 2011 Mithat Konar <m i t h a t _ k o n a r (a t e) y a h o o (d o t s) c o m>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  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, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  20. # MA 02110-1301, USA.
  21.  
  22. # 2011-12-15
  23.  
  24. # Requires: gxmessage
  25.  
  26. ### Constants
  27. # UI strings associated with "user mode"
  28. uiENTER_PACKAGES="Enter list of new packages."
  29. uiENTER_PACKAGES_TITLE="Apter"
  30.  
  31. uiNEXT_STEPS="To install your new packages and/or upgrade existing packages:
  32.  
  33. (1) Type 'Ctl-Alt-F1'. (This screen will disappear, so note the following!)
  34. (2) Login as root.
  35. (3) Enter 'apter' and follow the prompts."
  36. uiNEXT_STEPS_TITLE="Apter: next steps"
  37.  
  38. # misc. GUI constants
  39. uiDEFAULT_HEIGHT=0
  40. uiDEFAULT_WIDTH=600
  41.  
  42. # UI strings associated with "root mode"
  43. # TODO (after debugging and checking with the godz)
  44.  
  45. # File where list of pending packages will be stored. Should be writable
  46. # by user.
  47. PACKAGE_PATH=/tmp/apter_pending_packages
  48.  
  49. ### Functions
  50.  
  51. function do_user()
  52. # prompt user to enter package names to be installed and write them to
  53. # PACKAGE_PATH.
  54. {
  55.     local package_list
  56.     local result
  57.    
  58.     touch $PACKAGE_PATH                 # create it if it doesn't exist    
  59.     package_list=`cat $PACKAGE_PATH`    # get what's already there
  60.     # prompt user for additions
  61.     package_list=`gxmessage -entrytext  "$package_list" \
  62.               -title "$uiENTER_PACKAGES_TITLE" \
  63.               -geometry ${uiDEFAULT_WIDTH}x${uiDEFAULT_HEIGHT} \
  64.               -buttons GTK_STOCK_CANCEL:1,GTK_STOCK_OK:101 \
  65.               -default GTK_STOCK_OK \
  66.               "$uiENTER_PACKAGES"`
  67.     result=$?
  68.     if [[ "x$result" == "x1" ]] ; then
  69.         #~ echo "user cancelled" 1>&2
  70.         exit 1
  71.     fi
  72.     echo $package_list > $PACKAGE_PATH  # write the new list
  73.     # let user know what to do next
  74.     gxmessage -title "$uiNEXT_STEPS_TITLE" \
  75.               -geometry ${uiDEFAULT_WIDTH}x${uiDEFAULT_HEIGHT} \
  76.               -buttons GTK_STOCK_OK:101 \
  77.               -default GTK_STOCK_OK \
  78.               "$uiNEXT_STEPS"
  79. }
  80.  
  81. function do_root()
  82. # hold root's hand while killing X, upgrading and installing packages.
  83. {
  84.     # TODO: Put the ui strings into Constants after consulting the godz.
  85.    
  86.     local resp
  87.     local package_list
  88.  
  89.     # Go to runlevel 3 if runlevel > 3
  90.     # TODO: and if we got here via 'C-A-F1'.
  91.     rl=(`runlevel`)
  92.     rl=${rl[1]}
  93.     if [[ $rl > 3 ]] ; then
  94.         echo -n "Shutting down X server ... "
  95.         init 3
  96.         sleep 3
  97.         echo
  98.     fi
  99.    
  100.     read -p "Update repositories? [Y/n]: " resp
  101.     if is_affirmative $resp ; then
  102.         apt-get upgrade
  103.         echo
  104.     fi
  105.  
  106.     read -p "Upgrade existing packages? [Y/n]: " resp
  107.     if is_affirmative $resp ; then
  108.         apt-get dist-upgrade
  109.         echo
  110.     fi
  111.    
  112.     read -p "Install new packages? [Y/n]: " resp
  113.     if is_affirmative $resp ; then
  114.         package_list=`cat $PACKAGE_PATH`
  115.         #~ echo "DEBUG: package_list=$package_list"
  116.         apt-get install $package_list
  117.         echo
  118.     fi
  119.  
  120.     echo -n "Cleaning up downloaded packages ... "
  121.     apt-get clean
  122.     echo "Done."
  123.     echo
  124.  
  125.     #~ read -p "Restart X server? [Y/n]: " resp
  126.     #~ if is_affirmative $resp ; then
  127.        #~ echo "Restarting X server ..."
  128.        #~ init 5 && exit
  129.     #~ fi
  130.     echo "To restart X, enter 'init 5 && exit'"
  131. }
  132.  
  133. function is_affirmative()
  134. # return 0 if $1 is an affirmative response string; 1 otherwise
  135. {
  136.     if [[ "x$1" == "x" ||  "x$1" == "xy" || "x$1" == "xY" ]] ; then
  137.         return 0
  138.     fi
  139.     return 1
  140.  
  141. }
  142.  
  143. ### "MAIN"
  144.  
  145. # if user is not root...
  146. if [[ $EUID -ne 0 ]]; then
  147.     do_user
  148. else
  149.     do_root
  150. fi
  151.  

Posted by mithat at 15 Dec 2011, 20:11:05 Europe/Berlin
Language: bash