#!/bin/ksh

################################################################################
# SCRIPT NAME: lv_builder
# Creats a script that will create logical volumes for Informix  Database use.
# Run from cron each night with the output going to /usr/local/bin/rebuild_lv
# Uses the output to lslv (example below) to get neede info to build script
# NOTE: This would have been much easier with Perl or Awk.
################################################################################

#LOGICAL VOLUME:     lv00                   VOLUME GROUP:   rootvg
#LV IDENTIFIER:      00002700edd09c0d.12    PERMISSION:     read/write
#VG STATE:           active/complete        LV STATE:       closed/syncd
#TYPE:               jfs                    WRITE VERIFY:   off
#MAX LPs:            128                    PP SIZE:        4 megabyte(s)
#COPIES:             1                      SCHED POLICY:   parallel
#LPs:                1                      PPs:            1
#STALE PPs:          0                      BB POLICY:      relocatable
#INTER-POLICY:       minimum                RELOCATABLE:    yes
#INTRA-POLICY:       edge                   UPPER BOUND:    32
#MOUNT POINT:        N/A                    LABEL:          None
#MIRROR WRITE CONSISTENCY: on                                     
#EACH LP COPY ON A SEPARATE PV ?: yes                                    



echo "#!/bin/ksh" > /usr/local/bin/rebuild_lv  # fist line of script

# get names of informix logical volumes

for LVNAME in` tbcheck -pr |nawk  ' /Chunk path/ {print $3}'|cut -d"/" -f3 | cut -c2-30 | sort -u`
do 

   # Note: Parameters to fuctions are refered in the fuction as $1 $2 ..etc
   # this is not the $1 $2 of the command line.

   writeline()                           # function to write to output file
   {
       if [ -n "$2" ]; then
           echo "$1 $2" >> /usr/local/bin/rebuild_lv
       fi
    }

    echo " echo \" building logical volume $LVNAME\"" >> /usr/local/bin/rebuild_lv 
    
# Below gets data from lslv and strip out the second column (after the :)
# assigning the data to each vairable. The counter is used to determine which
# line of data you are on so the correct variable will be loaded.

  CNT=1
  for i in `/usr/sbin/lslv $LVNAME |sed -e s/inner\ /inner_/ -e s/outer\ /outer_/| nawk 'FS=":" { print $2 }' | nawk '{print $1 }'`
  do
      case $CNT in
          1) LOGVL=$i;;   # when counter is 1 put the value retured from the 
          2) LVID=$i;;    # command in the  variavle $LOGVL. Each time through
          3) VG=$i;;      # counter gets bumped up one and a new variable gets
          4) TYPE=$i;;    # loaded with the next value.
          5) MAXLP=$i;;
          6) COPIES=$i;;
          7) LPs=$i;;
          8) STALLPP=$i;;
          9) INTERPOL=$i;;
          10) INTRAPOL=$i;;
          11) MOUNTPOINT=$i;;
          12) MIRRORWR=$i;;
          13) NUMPOL=$i;;
      esac
      CNT=`expr $CNT + 1`
  done 

# Below, gets data from lslv and strips out the third column (actualy the last)
# assigning the data to each vairable. The counter is used to determine which
# line of data you are on so the correct variable will be loaded 
  
  CNT=1
  for i in `/usr/sbin/lslv $LVNAME | awk 'FS=":" { print $3 }' | awk '{print $1 }'`
  do
      case $CNT in
          1) VOLGRP=$i;;
          2) PERM=$i;;
          3) LVSTATE=$i;;
          4) WRITVER=$i;;
          5) PPSIZE=$i;;
          6) SCHPOL=$i;;
          7) PPS=$i;;
          8) BBPOLICY=$i;;
          9) RELOCATE=$i;;
          10) UPPERBOUND=$i;; 
          11) LABLE=$i;;
      esac  
      CNT=`expr $CNT + 1`
  done
         
# call the function at the top of the script to output the line to the 
# executable script

    writeline "mklv" ' \'

    writeline "-y" "$LOGVL \\" 

    writeline "-t" "$TYPE \\"

    writeline "-x" "$MAXLP \\" 

    writeline "-c" "$COPIES \\"

    INTERPOL=x  # always use all the disks 
    writeline "-e" "$INTERPOL \\"

    INTRAPOL1=`echo $INTRAPOL |cut -c1`  #get the first letter
    INTRAPOL2=`echo $INTRAPOL |cut -c7`  #get the seventh letter if it is there
    writeline "-a" "$INTRAPOL1$INTRAPOL2 \\"

    if [ "$MIRRORWR" = "on" ]; then    # output of lslv is on or off and I need
        MIRRORWR=y                  # y or n
    else
        MIRRORWR=n
    fi 
    writeline "-w" "$MIRRORWR \\"

    NUMPOL=`echo $NUMPOL |cut -c1`  # get the first letter
    writeline "-s" "$NUMPOL \\" 

    WRITVER=n                        # always set to no 
    writeline "-v" "$WRITVER \\" 

    writeline $PPSIZE 

    SCHPOL=`echo $SCHPOL |cut -c1`  # get the first letter
    writeline "-d" "$SCHPOL \\"

    writeline $PPS

    writeline $BBPOLICY

    RELOCATE=`echo $RELOCATE |cut -c1`  # get the first letter
    writeline "-r" "$RELOCATE \\" 

    writeline "-u" "$UPPERBOUND \\"

    writeline "-L" "$LABLE \\"

    writeline $VOLGRP "\\"
 
    writeline $LPs "\\" 

# Use the output of lspv VOLUME NAME  to get the names of the disks that 
# contain the logical volume

    for k in `/usr/sbin/lspv | nawk '{print $1}'`
    do
       DISK=`/usr/sbin/lspv  -l $k | grep $LVNAME` 
       if [ -n "$DISK" ]; then                # if variable has size then write
           writeline "$k" "\\"
       fi
     done
     echo ';' >> /usr/local/bin/rebuild_lv     # Comand ends and new one starts
done
chmod 777 /usr/local/bin/rebuild_lv