Advertisement
s243a

/usr/bin/slim-theme

Mar 14th, 2019
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.64 KB | None | 0 0
  1. #!/bin/sh
  2. # slim-theme: manage SLiM themes
  3. # Aleksej Bobylev <al.bobylev@gmail.com>, 2013-2014
  4.  
  5. ME=$(basename $0)
  6. VERSION=20130525
  7. CONF=/etc/slim.conf
  8.  
  9. help()
  10. {
  11.     cat >&2 << EOT
  12. $ME: Manage SLiM themes
  13.  
  14. Usage: $ME OPTION [NAME]
  15.  
  16. Options:
  17.   -h       Display this short help and exit
  18.   -l       List available themes (comma separated)
  19.   -g       Get the name of current theme (note, it can be comma separated list)
  20.   -s NAME  Set NAME theme as current; name of previous theme will be stored
  21.            (note, you can set comma separated list for random theme behavior)
  22.   -f NAME  Forget about NAME theme and return to previous used theme
  23.   -V       Display information about $ME version
  24.   -t       Translate current theme\'s strings according to global LANG value
  25.            (note, its done auto when -s processed)
  26.   -T LANG  Same as above, but use specified language
  27.  
  28. Set option works like stack: you can "set" theme several times (every time you
  29. install new SLiM theme). Use "forget" option at every theme uninstall to revert
  30. to previous theme, if it was current.
  31. EOT
  32.     exit 0
  33. }
  34.  
  35. get_current() { awk '/current_theme/ { print $2 }' $CONF ; } #Braces and trailing ";" added by s243a
  36. get_prev()    { awk '/previous_theme/ { print $2 }' $CONF ; } #Braces and trailing ";" added by s243a
  37. set_current() { sed -i 's|^current_theme .*$|current_theme '$1'|' $CONF ; } #Braces and trailing ";" added by s243a
  38. set_prev()
  39. {
  40.     sed -i '/^previous_theme/d' $CONF
  41.     [ x$1 != x ] && echo "previous_theme      $1" >> $CONF
  42. }
  43. find_theme()
  44. {
  45.     cd /usr/share/slim/themes
  46.     theme=$(ls -Ad $1 2>/dev/null | head -n1)
  47.     [ x$theme == x ] && echo "Theme \"$1\" not exist!" >&2 && exit 1
  48.     echo $theme
  49. }
  50. i18n()
  51. {
  52.     lang=$1
  53.     curr=$(get_current)
  54.     strings=/usr/share/slim/themes/$curr/strings
  55.     [ ! -e $strings ] && strings=/usr/share/slim/strings
  56.     conf=/usr/share/slim/themes/$curr/slim.theme
  57.     for str in welcome_msg username_msg password_msg; do
  58.         # ll_CC.UTF-8@euro ; ll_CC.UTF-8 ; ll_CC ; ll ; en
  59.         for langtry in $lang ${lang%@*} ${lang%\.*} ${lang%_*} en; do
  60.             try="$(grep '^'${str:0:1}':'$langtry'   ' $strings | cut -d'    ' -f2)"
  61.             [ x"$try" != x ] && break
  62.         done
  63.         sed -i 's|^\('$str'\).*$|\1             '"$try"'|' $conf
  64.         echo "$str[$langtry]=$try" >&2
  65.     done
  66. }
  67.  
  68. set_theme()
  69. {
  70.     theme=$(find_theme "$1")
  71.     curr=$(get_current)
  72.     prev=$(get_prev)
  73.     if [ x$theme != x$curr ]; then
  74.         echo "Set theme $theme" >&2
  75.         [ x$prev != x ] && curr=$curr:$prev
  76.         set_prev $curr
  77.         set_current $theme
  78.     else
  79.         echo "Already set" >&2
  80.     fi
  81.  
  82.     i18n $LANG
  83.     exit 0
  84. }
  85.  
  86. forget_theme()
  87. {
  88.     theme=$(find_theme "$1")
  89.     curr=$(get_current)
  90.     prev=$(get_prev)
  91.     echo "Forget theme \"$theme\"" >&2
  92.     if [ x$theme == x$curr ]; then
  93.         last=$(echo $prev | cut -d: -f1); [ x$last == x ] && last=base
  94.         rest=${prev#*:}; [ x$rest == x$prev ] && rest=
  95.         prev=$rest; set_prev $prev
  96.         echo "Back to \"$last\" theme" >&2
  97.         set_current $last
  98.     fi
  99.     prev=$(echo $(echo $prev | tr ':' '\n' | sed "/$theme/d") | tr ' ' ':')
  100.     set_prev $prev
  101.  
  102.     exit 0
  103. }
  104.  
  105.  
  106. # parse only first option
  107. getopts ":lgs:f:VhtT:" opt
  108. case $opt in
  109.     l)
  110.         list=$(find /usr/share/slim/themes/*/slim.theme | sort | awk -F/ '{ print $6 }')
  111.         echo $list | tr ' ' ','; exit 0 ;;
  112.     g)
  113.         get_current; exit 0 ;;
  114.     s)
  115.         set_theme "$OPTARG" ;;
  116.     f)
  117.         forget_theme "$OPTARG" ;;
  118.     V)
  119.         echo $VERSION; exit 0 ;;
  120.     h)
  121.         help; exit 0 ;;
  122.     t)
  123.         i18n $LANG; exit 0 ;;
  124.     T)
  125.         i18n $OPTARG; exit 0 ;;
  126.     *)
  127.         if [ x$OPTARG == xs -o x$OPTARG == xf -o x$OPTARG == xT ]; then
  128.             echo "Option \"$OPTARG\" requires an argument."
  129.         else
  130.             [ x$OPTARG != x ] && echo "Illegal option \"$OPTARG\"."
  131.         fi
  132.         echo "$ME -h for help."; exit 1 ;;
  133. esac
  134.  
  135. help
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement