Advertisement
Guest User

p-beautifier

a guest
Oct 25th, 2010
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.71 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # p-beautifier — /etc/portage/package.* files beautifier
  5. #
  6. # With this script you can make a hook while adding new item to package.* file.
  7. # The lines will be alphanumeric sorted and the keys will be indented from the
  8. # packages.
  9. #
  10. # You may prefer use this script through aliases in your ~/.bashrc like
  11. #    alias p-use="/path/to/p-beautifier use "
  12. # And use it from the shell like `p-use app-emulation/wine X alsa -pulseaudio`
  13. # It will be equivalent to
  14. #    `echo "app-emulation/wine X alsa -pulseaudio" >> /etc/portage/package.use`
  15. # But more pretty — indented to 2 columns and sorted.
  16. #
  17. # @autor fargred
  18. # @last_edit 20101025
  19. # @licence GPL v.2
  20. #
  21.  
  22. FILE=$1
  23. PACKAGE=$2
  24. KEYS=${@}
  25. KEYS=${KEYS#${1}\ ${2}}
  26. # right column start from INDENT symbols
  27. INDENT=32
  28. ASK_ME='yes'
  29. MERGE='yes'
  30.  
  31. add_line() {
  32.     if [[ ${#PACKAGE[0]} -lt $INDENT ]]; then {
  33.         num_of_spaces=$(($INDENT-${#PACKAGE[0]}))
  34.         for ((i=0; i < $num_of_spaces; i++)); do
  35.             PACKAGE="${PACKAGE} "
  36.         done
  37.     }; fi
  38.     echo "${PACKAGE}${KEYS}" >> /tmp/package.$FILE.tmp
  39. }
  40.  
  41. check_line() {
  42. # PACKAGE variable can contain perfixes and postfixes like ">=" or
  43. # version numbers, so purifying to section/package
  44.     PKG_SECTION=${PACKAGE%/*}
  45.         PKG_SECTION=${PKG_SECTION//[><=]/}
  46.     PKG_NAME=${PACKAGE#*/}
  47.         PKG_NAME=${PKG_NAME//-[0-9]*/}
  48.     echo ${PKG_SECTION}
  49.     echo ${PKG_NAME}
  50.     search=`grep ${PKG_SECTION}/${PKG_NAME} /etc/portage/package.$FILE`
  51.     if [[ ${search} != '' ]]; then {
  52.         echo -en "Requested package is already in file /etc/portage/package.$FILE\nThe string is: \"${search}\"\nNow, type the exactly package, you prefer. Ctrl+C any time exit. Hit <Enter> to left\ndefault: \"${PACKAGE}\"\nPackage > "
  53.         read input
  54.         if [[ ${input} != '' ]]; then
  55.             PACKAGE=${input}; fi
  56.        
  57.         echo -en "Now, type the exactly keys, you prefer for that package. Ctrl+C any time exit. Hit <Enter> to left\ndefault: \"${KEYS}\"\nKeys > "
  58.         read input
  59.         if [[ ${input} != '' ]]; then
  60.             KEYS=${input}; fi
  61.  
  62.     grep -v ${PKG_SECTION}/${PKG_NAME} /etc/portage/package.$FILE > /tmp/package.$FILE.tmp
  63.     }
  64.     else {
  65.         cp /etc/portage/package.${FILE} /tmp/package.$FILE.tmp
  66.     }; fi
  67. }
  68.  
  69. check_line
  70. add_line
  71.  
  72. if [[ $ASK_ME == 'yes' ]]; then {
  73.     echo -e "\nThis is a new /etc/portage/package.$FILE:\n"
  74.     cat /tmp/package.$FILE.tmp
  75.     echo -en "\nDo you want to merge this file with the already exist? [Y/n] "
  76.     read input
  77.     if [[ ${input} == 'n' || ${input} == 'N' ]]; then
  78.         MERGE='no'; fi
  79. }; fi
  80.  
  81. if [[ $MERGE == 'yes' ]]; then
  82.     cat /tmp/package.$FILE.tmp | sort -bdf > /etc/portage/package.$FILE; fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement