BASH   19

alternative

Guest on 21st September 2022 01:39:14 PM

  1. #!/bin/sh
  2.  
  3. LC_ALL=C
  4.  
  5. err() {
  6.         if [ $# -ne 2 ]; then
  7.                 err 1 "err expect 2 arguments: exit_number \"message\"" >&2
  8.         fi
  9.         echo "$2" >&2
  10.         exit $1
  11. }
  12.  
  13. list_alt() {
  14.         for cf in `find ${ALTERNATIVECONF} -type f -depth 2 -name "*.cf"`; do
  15.                 . $cf
  16.                 printf "%-20s -- %s\n" "$NAME" "$DESCRIPTION"
  17.         done
  18. }
  19.  
  20. get_alt() {
  21.         [ -d ${ALTERNATIVECONF}/$1 ] || err 1 "No alternative for $1"
  22.         [ -f ${ALTERNATIVECONF}/$1/$1.cf ] || err 1 "Corrupted alternative for $1"
  23.  
  24.         for cf in `find ${ALTERNATIVECONF}/$1 -type f -depth 2 -name "*.cf"`; do
  25.                 . $cf
  26.                 printf "%-20s -- %s\n" "$NAME" "$DESCRIPTION"
  27.         done
  28.         if [ -e ${ALTERNATIVECONF}/$1/current ]; then
  29.                 CURRENTALT=`realpath ${ALTERNATIVECONF}/$1/current`
  30.                 echo "${CURRENTALT##*/} is default"
  31.         else
  32.                 echo "None set currently"
  33.         fi
  34. }
  35.  
  36. set_alt() {
  37.         [ -d ${ALTERNATIVECONF}/$1 ] || err 1 "No alternative for $1"
  38.         [ -f ${ALTERNATIVECONF}/$1/$1.cf ] || err 1 "Corrupted alternative for $1"
  39.         [ -d ${ALTERNATIVECONF}/$1/$2 ] || err 1 "No such $2 option for $1"
  40.         [ -f ${ALTERNATIVECONF}/$1/$2/$2.cf ] || err 1 "Corrupted option $2 for $1"
  41.  
  42.         [ -w ${ALTERNATIVECONF}/$1 ] || err 1 "Permission denied"
  43.         if [ -e ${ALTERNATIVECONF}/$1/current ]; then
  44.                 [ -w  ${ALTERNATIVECONF}/$1/current ] || err 1 "Permission denied"
  45.                 rm -f ${ALTERNATIVECONF}/$1/current
  46.         fi
  47.         ln -sf ${ALTERNATIVECONF}/$1/$2 ${ALTERNATIVECONF}/$1/current
  48. }
  49.  
  50. usage() {
  51.         echo "Usage: alternative command [options]
  52.  
  53. Commands:
  54.    list   -- list available alternative module
  55.    get    -- get the value of a module
  56.    set    -- set the value of a module"
  57.     exit 1
  58. }
  59.  
  60. [ $# -lt 1 ] && usage
  61.  
  62. ALTERNATIVEPATH=`realpath $0`
  63. ALTERNATIVEPREFIX=${ALTERNATIVEPATH%\/bin/*}
  64. ALTERNATIVECONF=${ALTERNATIVEPREFIX}/etc/alternative.d
  65.  
  66. CMD=$1
  67. shift
  68. case ${CMD} in
  69.         list)
  70.                 [ $# -ne 0 ] && usage
  71.                 list_alt
  72.                 ;;
  73.         get)
  74.                 [ $# -ne 1 ] && usage
  75.                 get_alt $@
  76.                 ;;
  77.         set)
  78.                 [ $# -ne 2 ] && usage
  79.                 set_alt $@
  80.                 ;;
  81.         *)
  82.                 echo "Unknown command ${CMD}"
  83.                 usage
  84.                 ;;
  85. esac

Raw Paste


Login or Register to edit or fork this paste. It's free.