[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/se3master/usr/share/se3/sbin/ -> update-share.sh (source)

   1  #!/bin/bash
   2  # Update share preexec postexec scripts
   3  SHARES=/usr/share/se3/shares/shares.d
   4  AVAIL=/usr/share/se3/shares/shares.avail
   5  SMB_CONF=smb_Win95.conf,smb_WinXP.conf,smb_Win2K.conf,smb_Vista.conf,smb_CIFSFS.conf,smb.conf
   6  DEFAULT_ARCHS="Win95 WinXP Win2K Vista CIFSFS"
   7  ARGS=0
   8  VERBOSE=0
   9  ADD=0
  10  REMOVE=0
  11  DEFAULTS=0
  12  LIST=0
  13  export VERBOSE
  14  
  15  
  16  function usage {
  17      echo "usage: $0 [-v] -d [share]"
  18      echo "usage: $0 [-v] -a share script [arch1 arch2 ...]"
  19      echo "usage: $0 [-v] -r share script [arch1 arch2 ..."
  20      echo "usage: $0 [-v] -l"
  21      echo "       -d : Use defaults scripts"
  22      echo "       -a : Add script to share"
  23      echo "       -r : Remove script from share"
  24      echo "       -l : List available scripts"
  25      echo "       -v : Verbose"
  26      exit $1;
  27  }
  28  
  29  function clean_scripts
  30  {
  31      rm -f $AVAIL/*~
  32  }
  33  function list_all_shares
  34  {
  35      IFS=,
  36      (
  37      for file in $SMB_CONF
  38      do
  39          cat /etc/samba/"$file"|grep "\["|grep "\]"|grep -v global|while read share
  40          do
  41              expr "$share" : '\[\(.*\)\]'
  42          done
  43      done
  44      )|sort|uniq
  45      IFS=
  46  }
  47  
  48  function get_action {
  49      ACTION="$(expr "$(grep action: $AVAIL/$1)" : '.*:[ ]*\(.*\)')"
  50      if [ "$ACTION" == "start" ]
  51      then
  52          echo S
  53      elif [ "$ACTION" == "stop" ]
  54      then
  55          echo K
  56      fi
  57  }
  58  
  59  function get_level {
  60      LEVEL="$(expr "$(grep level: $AVAIL/$1)" : '.*:[ ]*\(.*\)')"
  61      if (( 10#$LEVEL>0 && 10#$LEVEL<100 ))
  62      then
  63          echo $LEVEL
  64      fi
  65  }
  66  
  67  # Return defaults script for share
  68  # Output: script arch ...
  69  function get_defaults_scripts {
  70      for file in "$AVAIL"/*
  71      do
  72          # foreach arch
  73          SCRIPT_VALID_FOR_SHARE=0
  74          grep -H  "shares_.*:" "$file"|cut -d ':' -f2|cut -d '_' -f2|( while read arch
  75          do
  76              # foreach share
  77              shares="$(grep -H "shares_$arch:" "$file"|cut -d ':' -f3)"
  78              IFS=,
  79              for share in $shares
  80              do
  81                  if [ "$(echo $share|sed -s 's/ //g')" == "$1" ]
  82                  then
  83                      if (( SCRIPT_VALID_FOR_SHARE == 0 ))
  84                      then
  85                          echo -n "$(basename $file) "
  86                          SCRIPT_VALID_FOR_SHARE=1
  87                      fi
  88                      echo -n "$arch "
  89                  fi
  90              done
  91          done
  92          if (( SCRIPT_VALID_FOR_SHARE == 1 ))
  93          then
  94              echo
  95          fi
  96          )
  97      done
  98  }
  99  
 100  # Add $2 script for share $1 with arch $3
 101  function share_add_script {
 102      SHARE="$1"
 103      SCRIPT="$2"
 104      ACTION="$(get_action $SCRIPT)"
 105      LEVEL="$(get_level $SCRIPT)"
 106      if [[ -z $ACTION || -z $LEVEL ]]
 107      then
 108          echo "Bad headers for $2: action=$ACTION, level=$LEVEL"
 109          exit 1
 110      fi
 111      shift 2
 112      for arch in $@
 113      do
 114          [ ! -d "$SHARES/$SHARE/$arch" ] && mkdir -p "$SHARES/$SHARE/$arch"
 115          if (( VERBOSE == 1 ))
 116          then
 117              echo "Add $SCRIPT to $SHARE share ($arch)..."
 118          fi
 119          ln -sf "$AVAIL/$SCRIPT" "$SHARES/$SHARE/$arch/$ACTION$LEVEL$SCRIPT"
 120      done
 121  }
 122  
 123  function share_remove_script {
 124      SHARE="$1"
 125          SCRIPT="$2"
 126      shift 2
 127      for arch in $@
 128      do
 129          if (( VERBOSE == 1 ))
 130          then
 131              echo "Remove $SCRIPT from $SHARE share ($arch)..."
 132          fi
 133          rm -f "$SHARES/$SHARE/$arch"/[KS]*"$SCRIPT"
 134      done
 135  }
 136  
 137  function share_defaults_scripts {
 138      rm -rf "$SHARES/$1"/*
 139      (( VERBOSE == 1)) && echo -n "Add to share $1: "
 140      get_defaults_scripts $1|while read script archs
 141      do
 142          (( VERBOSE == 1 )) && echo
 143          (( VERBOSE == 1 )) && echo -n "      - $script ("
 144          for arch in $archs
 145          do
 146              (( VERBOSE == 1 )) && echo -n "$arch, "
 147              VERBOSE=0 share_add_script $1 $script $arch $2
 148          done
 149          (( VERBOSE == 1 )) && echo -n -e "\b\b)"
 150      done
 151      (( VERBOSE == 1 )) && echo
 152  }
 153  
 154  function scripts_list {
 155      for script in "$AVAIL"/*
 156      do
 157          basename "$script"
 158      done
 159  }
 160  
 161  while getopts ":valrd" cmd
 162  do
 163      case $cmd in    
 164          a) ADD=1
 165             ARGS=2
 166             break;;
 167          r) REMOVE=1
 168             ARGS=2
 169             break;;
 170          d) DEFAULTS=1 
 171             ARGS=0
 172             break;;
 173          l) LIST=1 
 174             ARGS=0
 175             break;;
 176          v) VERBOSE=1 ;;
 177          ?) echo "bad option!"
 178             usage 1 ;;
 179      esac
 180  done
 181  
 182  shift $(($OPTIND-1))
 183  
 184  if (( ARGS > $# ))
 185  then
 186      usage 1
 187  fi
 188  
 189  clean_scripts
 190  
 191  if (( ADD == 1 ))
 192  then
 193      if (( $# > 2 ))
 194      then
 195          share_add_script $@
 196      else
 197          share_add_script $1 $2 $DEFAULT_ARCHS
 198      fi
 199  elif (( REMOVE == 1 ))
 200  then
 201      if (( $# > 2 ))
 202      then
 203          share_remove_script $@
 204      else
 205          share_remove_script $1 $2 $DEFAULT_ARCHS
 206      fi
 207  elif (( DEFAULTS == 1 ))
 208  then    
 209      if (( $# > 0 ))
 210      then    
 211          share_defaults_scripts $1
 212      else
 213          list_all_shares|while read share
 214          do
 215              share_defaults_scripts $share
 216          done
 217      fi
 218  elif (( LIST == 1 ))
 219  then
 220      scripts_list
 221  else 
 222      usage 1
 223  fi


Generated: Tue Mar 17 22:47:18 2015 Cross-referenced by PHPXref 0.7.1