
#!/bin/ksh
set +x
#--------------------------------------------------------------------------#
#                                                                          #
# Name: create_qtree.sh                                                    #
#                                                                          #
# Description: Script to create qtree(s) according standard FSOD service.  #
#                                                                          #
# Version: 2.9                                                             #
#                                                                          #
# References:                                                              #
#                                                                          #
# Documentation:                                                           #
#                                                                          #
# Parameters: none                                                         #
#                                                                          #
# Usage: create_qtree.sh                                                   #
#                                                                          #
# Global Description:                                                      #
#                                                                          #
# Author(s): Atos                                                          #
#                                                                          #
# Change log:                                                              #
# aut   date          vers   comments                                      #
# RL    28-04-2010    1.0    Initial version Richard Loos                  #
# RL    31-05-2010    1.1    While loop                                    #
# RL    03-06-2010    1.2    Preferred aggregate for customer              #
# RL    22-09-2010    1.3    Add new filers                                #
# RL    27-09-2010    1.4    Add LUN creation                              #
# RL    28-09-2010    1.5    Updates                                       #
# RL    30-09-2010    1.6    Add more qtrees on one volume                 #
# RL    08-11-2010    1.7    Changes for DR                                #
# RL    14-12-2010    1.8    Add new filers                                #
# RL    31-03-2011    1.9    Updates                                       #
# RL    16-05-2011    2.0    Add ABE                                       #
# RL    25-08-2011    2.1    Only allow lower-case letters for qtrees      #
# RL    13-10-2011    2.2    Remove old and add new filers                 #
# RL    14-03-2012    2.3    Updates list of filers                        #
# RL    11-04-2012    2.4    Updates list of filers                        #
# RL    04-10-2012    2.5    Updates list of filers                        #
# RL    18-10-2012    2.6    Add create vfiler, snapvault, deduplication   #
# RL    24-01-2013    2.7    Updates list of filers                        #
# RL    10-04-2013    2.8    Updates list of filers + update SV function   #
# RL    16-04-2013    2.9    Updates 'head -1' (line 390) to SV function   #
#                                                                          #
#--------------------------------------------------------------------------# 
echo "Give change number [f.e. C1234567]?: "
read change_number

#--------------------------------------------------------------------------#
# Initialize variabels                                                     #
#--------------------------------------------------------------------------#
VER="2.9"
CLR="`tput clear`"
DATADIR=${HOME}/data/out
TMPDIR=${HOME}/data/tmp
tmp_bin_file=${DATADIR}/GZ${change_number}_create_qtree_tmp.sh
bin_file=${DATADIR}/GZ${change_number}_create_qtree.sh
exports_file=${DATADIR}/GZ${change_number}_create_exports.out
quotas_file=${DATADIR}/GZ${change_number}_create_quotas.out
conf_file=${DATADIR}/GZ${change_number}_snapmirror.conf
resolv_file=${DATADIR}/GZ${change_number}_resolv.conf

#--------------------------------------------------------------------------#
# Collect information to create volume, qtree(s), LUN, quotas and exports  #
#--------------------------------------------------------------------------#
while :
do
echo "Choose type of class:
	a) AC = Archive Class (DBM-Tier4)
	b) BC = Business Class (DBM-Tier2b)
	c) CC = Compliance archive Class
	e) EC = Economy Class
	f) FC = First Class
"
read class_choice
case "$class_choice" in
a) class_type="ac"
while :
do
echo "Give preferred filer name:
	1) nlnaf39 (Amsterdam Naritaweg)
	2) nlnaf40 (Amsterdam Naritaweg)
	3) nlnaf52 (HWU)
	4) nlnaf54 (Best)
	5) nlnaf55 (HWU)
	6) nlnaf56 (HWU)
	7) nlnaf58 (Hurk)
	8) nlnaf59 (Best)
	9) nlnaf62 (Amsterdam Naritaweg)
	10) nlnaf80 (Best)
	11) nlnaf81 (Hurk)
"
read filer_name
	case "$filer_name" in
	1) filer="nlnaf39"
	break
	;;
	2) filer="nlnaf40"
	break
	;;
	3) filer="nlnaf52"
	break
	;;
	4) filer="nlnaf54"
	break
	;;
	5) filer="nlnaf55"
	break
	;;
	6) filer="nlnaf56"
	break
	;;
	7) filer="nlnaf58"
	break
	;;
	8) filer="nlnaf59"
	break
	;;
	9) filer="nlnaf62"
	break
	;;
	10) filer="nlnaf81"
	break
	;;
	11) filer="nlnaf82"
	break
	;;
	esac
	done
break
;;
b) class_type="bc"
while :
do
echo "Give preferred filer name:
	1) nlnaf51 (HWU)
	2) nlnaf53 (Best)
	3) nlnaf55 (HWU)
	4) nlnaf57 (Hurk)
	5) nlnaf58 (Hurk)
	6) nlnaf59 (Best)
	7) nlnaf60 (Best)
	8) nlnaf61 (Amsterdam Naritaweg)
	9) nlnaf62 (Amsterdam Naritaweg)
	10) nlnaf67 (Best)
	11) nlnaf68 (Best)
	12) nlnaf69 (Hurk)
	13) nlnaf70 (Hurk)
	14) nlnaf71 (Hurk)
	15) nlnaf72 (Hurk)
	16) nlnaf73 (Best)
	17) nlnaf74 (Best)
	18) nlnaf75 (Hurk)
	19) nlnaf76 (Hurk)
	20) nlnaf77 (Best)
	21) nlnaf78 (Best)
	22) nlnaf79 (Best)
	23) nlnaf80 (Best)
	24) nlnaf81 (Hurk)
	25) nlnaf82 (Hurk)

"
read filer_name
	case "$filer_name" in
	1) filer="nlnaf51"
	break
	;;
	2) filer="nlnaf53"
	break
	;;
	3) filer="nlnaf55"
	break
	;;
	4) filer="nlnaf57"
	break
	;;
	5) filer="nlnaf58"
	break
	;;
	6) filer="nlnaf59"
	break
	;;
	7) filer="nlnaf60"
	break
	;;
	8) filer="nlnaf61"
	break
	;;
	9) filer="nlnaf62"
	break
	;;
	10) filer="nlnaf67"
	break
	;;
	11) filer="nlnaf68"
	break
	;;
	12) filer="nlnaf69"
	break
	;;
	13) filer="nlnaf70"
	break
	;;
	14) filer="nlnaf71"
	break
	;;
	15) filer="nlnaf72"
	break
	;;
	16) filer="nlnaf73"
	break
	;;
	17) filer="nlnaf74"
	break
	;;
	18) filer="nlnaf75"
	break
	;;
	19) filer="nlnaf76"
	break
	;;
	20) filer="nlnaf77"
	break
	;;
	21) filer="nlnaf78"
	break
	;;
	22) filer="nlnaf79"
	break
	;;
	23) filer="nlnaf80"
	break
	;;
	24) filer="nlnaf81"
	break
	;;
	25) filer="nlnaf82"
	break
	;;
	esac
	done
break
;;
c) class_type="cc"
while :
do
echo "Give preferred filer name:
	1) nlnaf31 (HWU)
	2) nlnaf35 (Amsterdam Naritaweg)
	3) nlnaf63 (HWU)
	4) nlnaf64 (Hurk)

"
read filer_name
	case "$filer_name" in
	1) filer="nlnaf31"
	break
	;;
	2) filer="nlnaf35"
	break
	;;
	3) filer="nlnaf63"
	break
	;;
	4) filer="nlnaf64"
	break
	;;
	esac
	done
break
;;
e) class_type="ec"
while :
do
echo "Give preferred filer name:
	1) nlnaf31 (HWU)
	2) nlnaf32 (HWU)
	3) nlnaf33 (HWU)
	4) nlnaf34 (HWU)
	5) nlnaf35 (Amsterdam Naritaweg)
	6) nlnaf36 (Amsterdam Naritaweg)
	7) nlnaf37 (Amsterdam Naritaweg)
	8) nlnaf38 (Amsterdam Naritaweg)
	9) nlnaf52 (HWU)
	10) nlnaf54 (Best)
	11) nlnaf58 (Hurk)
	12) nlnaf59 (Best)
	13) nlnaf62 (Amsterdam Naritaweg)
	14) nlnaf65 (Hurk)

"
read filer_name
	case "$filer_name" in
	1) filer="nlnaf31"
	break
	;;
	2) filer="nlnaf32"
	break
	;;
	3) filer="nlnaf33"
	break
	;;
	4) filer="nlnaf34"
	break
	;;
	5) filer="nlnaf35"
	break
	;;
	6) filer="nlnaf36"
	break
	;;
	7) filer="nlnaf37"
	break
	;;
	8) filer="nlnaf38"
	break
	;;
	9) filer="nlnaf52"
	break
	;;
	10) filer="nlnaf54"
	break
	;;
	11) filer="nlnaf58"
	break
	;;
	12) filer="nlnaf59"
	break
	;;
	13) filer="nlnaf62"
	break
	;;
	14) filer="nlnaf65"
	break
	;;
	esac
	done
break
;;
f) class_type="fc"
while :
do
echo "Give preferred filer name:
	1) None

"
read filer_name
	case "$filer_name" in
	1) filer=""
        echo "No first class filers available"
        exit 0
	break
	;;
	esac
	done
break
;;
*) echo ${CLR}
   echo "Invalid choice, press <enter> to go further"
   read ans
;;
esac
done

#--------------------------------------------------------------------------#
# Define parameters                                                        #
#--------------------------------------------------------------------------#
echo "Give customer code [f.e. aip, nxp, slb]?: "
read small_cust_code

>${TMPDIR}/create_qtree.tmp
>${TMPDIR}/create_qtree2.tmp
>${TMPDIR}/create_qtree3.tmp
>${bin_file}
chmod 750 ${bin_file}

filer2=`ssh ${filer} cf partner | awk '{print $1}'`

#--------------------------------------------------------------------------#
# The snapvault function                                                   #
#--------------------------------------------------------------------------#
function DefineSnapvault
{
echo "Secondary storage required as backup with SnapVault [y/n]?: "
read ans_sv
case "$ans_sv" in
y) if [ "${ans_sv}" = "y" ]
   then
     new_volume3=av`echo ${new_volume} |cut -c3-`
     filer_name3="`ssh ${filer} snapvault status | grep ${small_cust_code} | grep ${cust_code} | awk '{print $2}' | awk -F ":" '{print $1}' | uniq | head -1`"
     echo "List of aggregates on this filer ${filer_name3}:"
     ssh ${filer_name3} df -gA
     echo "Preferred aggregates for customercode ${small_cust_code}:"

     for Aggregate3 in `ssh ${filer_name3} aggr status | grep online | awk '{ print $1 }'| tr -d ' '| sort`
     do
       for Vollist3 in `ssh ${filer_name3} aggr show_space ${Aggregate3} | grep "_${small_cust_code}" |awk '{print $1}'`
       do
         echo "${Aggregate3}">>${TMPDIR}/create_qtree3.tmp
       done
     done

     Aggr_list3=`cat ${TMPDIR}/create_qtree3.tmp | uniq | awk '{print $1}'`
     if [ -z "${Aggr_list3}" ]
     then Aggr_list3="None"
     fi

     echo "${Aggr_list3}"

     echo "Give preferred aggregate [f.e. aggr2001]?: "
     read ans_aggregate3

     echo "Do you need snapvault daily snapshots [y/n]?: "
     read ans_svdsnap
     if [ "${ans_svdsnap}" = "y" ]
     then
       echo "Give value for daily snapshots [f.e. 35]?: "
       read ans_vsvdsnap
       sv_snap_daily="${ans_vsvdsnap}"
       echo "Give preferred time for creating snapshot [f.e. 23 (23 hours CEST)]?: "
       read ans_tsvdsnap
       time_sv_daily="${ans_tsvdsnap}"
     fi 

     echo "Do you need snapvault weekly snapshots [y/n]?: "
     read ans_svwsnap
     if [ "${ans_svwsnap}" = "y" ]
     then
       echo "Give value for weekly snapshots [f.e. 5]?: "
       read ans_vsvwsnap
       sv_snap_weekly="${ans_vsvwsnap}"
       echo "Give preferred time for creating snapshot [f.e. 23 (23 hours CEST)]?: "
       read ans_tsvwsnap
       time_sv_weekly="${ans_tsvwsnap}"
     fi
     echo "Start creating output files in ${DATADIR}"
   fi
break
;;
n) echo "Start creating output files in ${DATADIR}"
break
;;
*) echo ${CLR}
echo "Invalid choice, press <enter> to go further"
read ans
;;
esac
}

#--------------------------------------------------------------------------#
# Execution                                                                #
#--------------------------------------------------------------------------#
echo "List of aggregates on this filer ${filer}:"
ssh ${filer} df -gA
echo "Preferred aggregates for customercode ${small_cust_code}:"

for Aggregate in `ssh ${filer} aggr status | grep online | awk '{ print $1 }'| tr -d ' '| sort`
do
  for Vollist in `ssh ${filer} aggr show_space ${Aggregate} | grep "_${small_cust_code}" |awk '{print $1}'`
  do
    echo "${Aggregate}">>${TMPDIR}/create_qtree.tmp
  done
done

Aggr_list=`cat ${TMPDIR}/create_qtree.tmp | uniq | awk '{print $1}'`
if [ -z "${Aggr_list}" ]
then Aggr_list="None"
fi

echo "${Aggr_list}"

echo "Do you need a new vfiler for customer ${small_cust_code} [y/n]?: "
read ans_vfiler
case "$ans_vfiler" in
y) echo "Give preferred vfiler name [f.e. nlnafv${small_cust_code}01]?: "
   read vfiler_name
   echo "Give preferred aggregate for root volume vfiler [f.e. aggr601]?: "
   read ans_aggregate_root
   cust_code=`echo ${vfiler_name} |cut -c7-`
   Vrootvol="vol0_vfiler${cust_code}"
   echo "Give IP-address vfiler [f.e. 199.6.147.4]: "
   read ans_vfiler_ip01
   echo "" 
   echo "Bitmask (Bits)	Dotted Decimal"
   echo "/16		255.255.0.0"
   echo "/17		255.255.128.0"
   echo "/18		255.255.192.0"
   echo "/19		255.255.224.0"
   echo "/20		255.255.240.0"
   echo "/21		255.255.248.0"
   echo "/22		255.255.252.0"
   echo "/23		255.255.254.0"
   echo "/24		255.255.255.0"
   echo "/25		255.255.255.128"
   echo "/26		255.255.255.192"
   echo "/27		255.255.255.224"
   echo "/28		255.255.255.240"
   echo "/29		255.255.255.248"
   echo "/30		255.255.255.252"
   echo "/31		255.255.255.254"
   echo "/32		255.255.255.255"
   echo ""   
   echo "Give netmask [f.e. 255.255.255.0]: "
   read ans_netmask
   echo "Give VLAN number [f.e. 1234]?: "
   read ans_vlan_number
   echo "Do you need second IP-address vfiler [y/n]?: "
   read ans_sec_ip
   if [ "${ans_sec_ip}" = "y" ]
   then
     echo "Give second IP-address vfiler [f.e. 10.217.93.4]: "
     read ans_vfiler_ip02
     echo "Give netmask [f.e. 255.255.255.0]: "
     read ans_sec_netmask
     echo "Give second VLAN number [f.e. 5678]?: "
     read ans_sec_vlan_number
   fi
   echo "Give default IP-address gateway [f.e. 199.6.147.1]: "
   read ans_gateway
   echo "Do you use DNS [y/n]?: "
   read ans_dns
   if [ "${ans_dns}" = "y" ]
   then
     echo "Give DNS domain name [f.e. org.${small_cust_code}.local]: "
     read ans_dns_domain
     >${resolv_file}
     cnt_ip="84"
     while :
     do
     echo "Give IP-address DNS domain server [f.e. 199.6.147.${cnt_ip}, please <enter> when empty]: "
     read ans_dns_server
     
     let cnt_ip=${cnt_ip}+1
     if [ "${ans_dns_server}" != "" ]
     then
       echo "nameserver ${ans_dns_server}">>${resolv_file}
     else
       break
     fi
     done
   fi
   echo "ssh ${filer} vol create ${Vrootvol} -l C ${ans_aggregate_root} 1g">>${bin_file}
   echo "ssh ${filer} snap sched ${Vrootvol} 6 2 6@8,12,16,20">>${bin_file}

   for Ipspace_cust in `ssh ${filer} ipspace list | grep "${small_cust_code}" | grep "${ans_vlan_number}" | sed -e 's/ /;/g' | awk '{print $1}'`
   do
     if [ "${Ipspace_cust}" = "" ]
     then
       echo "List of ipspaces on this filer ${filer}:"
       ssh ${filer} ipspace list|grep -v configured

       echo "Give preferred ipspace name [f.e. ${small_cust_code}01-ipspace]?: "
       read ans_ipspace_name

       ssh ${filer} ifconfig -a | grep trunked | grep "s" | sort | uniq | awk '{print $2}'
       echo "Give preferred interface group [f.e. sig01-vio]?: "
       read ans_ifgrp
       ans_ifgrp_vlan="${ans_ifgrp}-${ans_vlan_number}"
       echo "ssh ${filer} ipspace create ${ans_ipspace_name}">>${bin_file}
       echo "ssh ${filer2} ipspace create ${ans_ipspace_name}">>${bin_file} 
       echo "ssh ${filer} vlan add ${ans_ifgrp} ${ans_vlan_number}">>${bin_file}
       echo "ssh ${filer2} vlan add ${ans_ifgrp} ${ans_vlan_number}">>${bin_file}
       echo "ssh ${filer} ipspace assign ${ans_ipspace_name} ${ans_ifgrp}-${ans_vlan_number}">>${bin_file}
       echo "ssh ${filer2} ipspace assign ${ans_ipspace_name} ${ans_ifgrp}-${ans_vlan_number}">>${bin_file}
       echo "ssh ${filer} vfiler create ${vfiler_name} -n -s ${ans_ipspace_name} -i ${ans_vfiler_ip01} /vol/${Vrootvol}">>${bin_file} 
       echo "ssh ${filer} ifconfig ${ans_ifgrp_vlan} ${vfiler_name} netmask ${ans_netmask} partner ${ans_ifgrp_vlan}">>${bin_file} 
       echo "ssh ${filer2} ifconfig ${ans_ifgrp_vlan} partner ${ans_ifgrp_vlan}">>${bin_file} 
       if [ "${ans_sec_ip}" = "y" ]
       then
         ans_ifgrp_vlan02="${ans_ifgrp}-${ans_sec_vlan_number}"
         echo "ssh ${filer} vfiler add ${vfiler_name} -i ${ans_vfiler_ip02}">>${bin_file} 
         echo "ssh ${filer} ifconfig ${ans_ifgrp_vlan02} ${ans_vfiler_ip02} netmask ${ans_sec_netmask} partner ${ans_ifgrp_vlan02}">>${bin_file} 
       fi 
       echo "ssh ${filer} vfiler run ${vfiler_name} route add default ${ans_gateway} 1">>${bin_file} 
     else
       ans_ipspace_name=`echo "${Ipspace_cust}" | awk -F";" '{print $1}'`
       ans_ifgrp_vlan=`echo "${Ipspace_cust}" | awk -F"(" '{print $2}' | awk -F")" '{print $1}' | awk -F";" '{print $1}'`
       ans_ifgrp_vlan02=`echo "${Ipspace_cust}" | awk -F"(" '{print $2}' | awk -F")" '{print $1}' | awk -F";" '{print $2}'`
       vlan02=`echo "${Ipspace_cust}" | awk -F"(" '{print $2}' | awk -F")" '{print $1}' | awk -F";" '{print $2}' | awk -F"-" '{print $3}'`
       echo "ssh ${filer} vfiler create ${vfiler_name} -n -s ${ans_ipspace_name} -i ${ans_vfiler_ip01} /vol/${Vrootvol}">>${bin_file} 
       if [ "${ans_sec_ip}" = "y" ]
       then
         echo "ssh ${filer} vfiler add ${vfiler_name} -i ${ans_vfiler_ip02}">>${bin_file} 
         if [ "${ans_vlan_number}" != "${vlan02}" ]
         then
           cnt_ifgrp=`echo "${ans_ifgrp_vlan}" | wc -m`
           cnt_vlan=`echo "${ans_vlan_number}" | wc -m`
           let new_cnt=${cnt_ifgrp}-${cnt_vlan}
           new_ifgrp=`echo "${ans_ifgrp_vlan}" | cut -c1-${new_cnt}`

           echo "ssh ${filer} ifconfig ${new_ifgrp}${ans_vlan_number} alias ${vfiler_name} netmask ${ans_netmask} partner ${new_ifgrp}${ans_vlan_number}">>${bin_file} 
           echo "ssh ${filer} ifconfig ${ans_ifgrp_vlan02} alias ${ans_vfiler_ip02} netmask ${ans_sec_netmask} partner ${ans_ifgrp_vlan02}">>${bin_file} 
         else
           cnt_ifgrp=`echo "${ans_ifgrp_vlan}" | wc -m`
           cnt_vlan=`echo "${ans_vlan_number}" | wc -m`
           let new_cnt=${cnt_ifgrp}-${cnt_vlan}
           new_ifgrp=`echo "${ans_ifgrp_vlan}" | cut -c1-${new_cnt}`

           echo "ssh ${filer} ifconfig ${new_ifgrp}${ans_vlan_number} alias ${vfiler_name} netmask ${ans_netmask} partner ${new_ifgrp}${ans_vlan_number}">>${bin_file} 
           echo "ssh ${filer} ifconfig ${new_ifgrp}${ans_sec_vlan_number} alias ${ans_vfiler_ip02} netmask ${ans_sec_netmask} partner ${new_ifgrp}${ans_sec_vlan_number}">>${bin_file} 
         fi
       else
       echo "ssh ${filer} ifconfig ${ans_ifgrp_vlan} alias ${vfiler_name} netmask ${ans_netmask} partner ${ans_ifgrp_vlan}">>${bin_file} 
       fi

     fi  
   done

   if [ "${ans_dns}" = "y" ]
   then
     echo "ssh ${filer} vfiler run ${vfiler_name} options dns.domainname ${ans_dns_domain}">>${bin_file}
     echo "ssh ${filer} vfiler run ${vfiler_name} options dns.enable on">>${bin_file}
   fi
   echo "ssh ${filer} vfiler run ${vfiler_name} options cifs.smb2.client.enable on">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options cifs.smb2.enable on">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options cifs.tcp_window_size 1048576">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options rsh.access none">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options rsh.enable off">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options nfs.export.auto-update off">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options nfs.udp.enable off">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options security.passwd.firstlogin.enable on">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options security.passwd.rules.everyone on">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options security.passwd.rules.minimum.symbol 1">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} options ssh.idle.timeout 600">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} secureadmin setup ssh">>${bin_file}
   echo "ssh ${filer} vfiler run ${vfiler_name} secureadmin enable ssh2">>${bin_file}

;;
n) echo "List of vfilers on this filer ${filer}:"
   for Vfiler in `ssh ${filer} vfiler status | grep "${small_cust_code}" | grep -v "DR backup" | awk '{ print $1 }'`
   do
     echo "${Vfiler}"
   done

   echo "Give name vfiler [f.e. nlnafvaip01, nlnafvnxp02, nlnafvslb03]?: "
   read vfiler_name
   cust_code=`echo ${vfiler_name} |cut -c7-`
   Vrootvol="`ssh ${filer} vfiler status -r ${vfiler_name} | grep etc | cut -d: -f2 | cut -d[ -f1| sed -e 's/ //g' | awk -F "/" '{print $3}'`"

   echo "List of volumes of vfiler ${vfiler_name}:"
   for Volume in `ssh ${filer} vol status | grep ${cust_code} | grep -v vfiler | sort | awk '{print $1}'`
   do
   for Contain_aggregate in `ssh ${filer} vol status ${Volume} | grep "Containing aggregate" | awk '{print $3}'`
     do
       echo "${Volume} on aggregate ${Contain_aggregate}"
     done
   done

;;
*) echo ${CLR}
   echo "Invalid choice, press <enter> to go further"
   read ans
;;
esac


while :
do
echo "Do you want a new volume [y/n]?: "
read ans_volume
case "$ans_volume" in
y) echo "Give preferred aggregate [f.e. aggr601]?: "
read ans_aggregate
echo "Give new volume name: [f.e. ${class_type}_${cust_code}_vol001]?: "
read new_volume
echo "Are local snapshots needed [y/n]?: "
read ans_snap
	while :
	do
	case "$ans_snap" in
	y) echo "Do you need hourly snapshots [y/n]?: "
	read ans_hsnap
		while :
		do
		case "$ans_hsnap" in
		y) echo "Do you want current day snapshots or office hours [c/o]?: "
		read ans_hhsnap
			while :
			do
			case "$ans_hhsnap" in
			c) snap_hourly="8@0,3,6,9,12,15,18,21"
			break
			;;
			o) snap_hourly="7@7,9,11,13,15,17,19"
			break
			;;
			*) echo ${CLR}
			   echo "Invalid choice, press <enter> to go further"
			   read ans
			;;
			esac
			done
		break
		;;
		n) snap_hourly="0"
		break
		;;
		*) echo ${CLR}
		   echo "Invalid choice, press <enter> to go further"
		   read ans
		;;
		esac
		done

	echo "Do you need nightly snapshots [y/n]?: "
	read ans_nsnap
		while :
		do
		case "$ans_nsnap" in
		y) echo "Give value for nightly snapshots [f.e. 7]?: "
		read ans_vnsnap
		snap_nightly="${ans_vnsnap}"
		break
		;;
		n) snap_nightly="0"
		break
		;;
		*) echo ${CLR}
		   echo "Invalid choice, press <enter> to go further"
		   read ans
		;;
		esac
		done

	echo "Do you need weekly snapshots [y/n]?: "
	read ans_wsnap
		while :
		do
		case "$ans_wsnap" in
		y) echo "Give value for weekly snapshots [f.e. 4]?: "
		read ans_vwsnap
		snap_weekly="${ans_vwsnap}"
		break
		;;
		n) snap_weekly="0"
		break
		;;
		*) echo ${CLR}
		   echo "Invalid choice, press <enter> to go further"
		   read ans
		;;
		esac
		done
	break
	;;
	n) snap_hourly="0"
	   snap_nightly="0"
	   snap_weekly="0"
	break
	;;
	*) echo ${CLR}
	   echo "Invalid choice, press <enter> to go further"
	   read ans
	;;
	esac
	done
echo "Do you prefer to use deduplication (sis) [y/n]?: "
read ans_sis
if [ "${ans_sis}" = "y" ]
then
  echo "Give preferred time for deduplication process [f.e. 19 (19 hours CEST)]?: "
  read ans_timesis
fi
break
;;
n) echo "Give existing volume name: "
read old_volume
echo "Do you prefer to use deduplication (sis) [y/n]?: "
read ans_sis
if [ "${ans_sis}" = "y" ]
then
  echo "Give preferred time for deduplication process [f.e. 19 (19 hours CEST)]?: "
  read ans_timesis
fi
break
;;
*) echo ${CLR}
   echo "Invalid choice, press <enter> to go further"
   read ans
;;

esac
done

if [ "${ans_volume}" = "y" ]
then
  echo "* tree@/vol/${new_volume} - - - - -">${quotas_file}
  echo "# *** GZ:${change_number} ***">>${quotas_file}
  echo "# *** GZ:${change_number} ***">${exports_file}
else
  echo "# *** GZ:${change_number} ***">${quotas_file}
  echo "# *** GZ:${change_number} ***">${exports_file}
fi
 
vol_size="0"

>${tmp_bin_file}
chmod 660 ${tmp_bin_file}

while :
do
echo "Do you want a new qtree [y/n]?: "
read ans_nqtree
case "$ans_nqtree" in
y) while :
do
echo "Give new qtree name [f.e. siebel_fs]?: "
read ans_qtree
ans_qtree=`echo ${ans_qtree}| tr '[A-Z]' '[a-z]'`
echo "Will the qtree be a lun (iscsi), windows (ntfs) or unix filesystem [l/w/u]?: "
read ans_tqtree
	case "$ans_tqtree" in
        l) echo "List of LUN's of this customercode ${cust_code}:"
        ssh ${filer} vfiler run ${vfiler_name} lun show|grep vol|awk '{print $1}'
        echo "Give name of new LUN [f.e. lun01]?: "
        read ans_lunname
        echo "Give size of new LUN in GB [f.e. 250]?: "
        read ans_lunsize
        echo "List of igroups of this customercode ${cust_code}:"
        ssh ${filer} vfiler run ${vfiler_name} igroup show|grep ostype|awk '{print $1}'
        echo "Give igroup name [f.e. ${small_cust_code}01-igroup]?: "
        read ans_igroup
	echo "Give list of node names [f.e. iqn.1991-05.com.microsoft:hyperv01.box.int,iqn.1991-05.com.microsoft:pvs01.box.int]?: "
	read ans_nodes
        nodes_list=`echo ${ans_nodes} | sed 's/,/ /g'`
	break
	;;
	w) echo "Give name for CIFS share [f.e. ${ans_qtree}]?: "
	read ans_share
	echo "Hidden share ('$') needed [y/n]?: "
	read ans_hshare
		while :
		do
		case "$ans_hshare" in
		y) ans_share="${ans_share}\$"
		break
		;;
		n) ans_share="${ans_share}"
		break
		;;
		*) echo ${CLR}
		   echo "Invalid choice, press <enter> to go further"
		   read ans
		;;
		esac
		done
	echo "Do you prefer to enable acccess-based enumeration (ABE) [y/n]?: "
	read ans_abeshare
	break
	;;
	u) echo "Give list of exported servers [f.e. nlxsl100be,nlxsl101be]?: "
	read ans_exports
        exports_list=`echo ${ans_exports} | sed 's/,/:/g'`
        
        if [ "${ans_volume}" = "y" ]
	then
          if [ "${ans_qtree}" = "" ]
	  then
            echo "/vol/${new_volume}	-sec=sys,rw=${exports_list},root=${exports_list}">>${exports_file}
          else
            echo "/vol/${new_volume}/${ans_qtree}	-sec=sys,rw=${exports_list},root=${exports_list}">>${exports_file}
          fi
        else
          echo "/vol/${old_volume}/${ans_qtree} 	-sec=sys,rw=${exports_list},root=${exports_list}">>${exports_file}
	fi	
	break
	;;
	*) echo ${CLR}
	   echo "Invalid choice, press <enter> to go further"
	   read ans
	;;
	esac
done

if [ "${ans_tqtree}" = "l" ]
then 
  let "ans_quota= ${ans_lunsize} * 112 / 100"
  let "vol_size= ${vol_size}+${ans_quota}"
else
  echo "Give value of new qtree in GB [f.e. 20]?: "
  read ans_quota
  let "vol_size= ${vol_size}+${ans_quota}"
fi

if [ "${ans_volume}" = "y" ]
then
  if [ "${ans_qtree}" != "" ]
  then
    echo "/vol/${new_volume}/${ans_qtree} tree ${ans_quota}G - - - -">>${quotas_file}
  fi
else
  echo "/vol/${old_volume}/${ans_qtree} tree ${ans_quota}G - - - -">>${quotas_file}
fi

if [ "${new_volume}" != "" ]
then
  if [ "${ans_qtree}" = "" ]
  then
    echo "ssh ${filer} vfiler run ${vfiler_name} exportfs /vol/${new_volume}">>${tmp_bin_file}
  else
    echo "ssh ${filer} vfiler run ${vfiler_name} qtree create /vol/${new_volume}/${ans_qtree}">>${tmp_bin_file}
    if [ "${ans_tqtree}" = "l" ]
    then
      echo "ssh ${filer} vfiler run ${vfiler_name} igroup create -i -t windows ${ans_igroup} ${nodes_list}">>${tmp_bin_file}
      echo "ssh ${filer} vfiler run ${vfiler_name} lun create -s ${ans_lunsize}g -t windows /vol/${new_volume}/${ans_qtree}/${ans_lunname}">>${tmp_bin_file}
      echo "ssh ${filer} vfiler run ${vfiler_name} lun comment /vol/${new_volume}/${ans_qtree}/${ans_lunname} \"GZ:${change_number}\" ">>${tmp_bin_file}
      echo "ssh ${filer} vfiler run ${vfiler_name} lun map /vol/${new_volume}/${ans_qtree}/${ans_lunname} ${ans_igroup}">>${tmp_bin_file}
    elif [ "${ans_tqtree}" = "w" ]
    then
      echo "ssh ${filer} vfiler run ${vfiler_name} qtree security /vol/${new_volume}/${ans_qtree} ntfs">>${tmp_bin_file}
      echo "ssh ${filer} vfiler run ${vfiler_name} cifs shares -add \"${ans_share}\" \"/vol/${new_volume}/${ans_qtree}\" -comment \"${filer}:/vol/${new_volume}/${ans_qtree}\" ">>${tmp_bin_file}
      echo "ssh ${filer} vfiler run ${vfiler_name} cifs access \"${ans_share}\" S-1-5-11 Full Control">>${tmp_bin_file}
      echo "ssh ${filer} vfiler run ${vfiler_name} cifs access -delete \"${ans_share}\" everyone Full Control">>${tmp_bin_file}
      if [ "${ans_abeshare}" = "y" ]
      then
        echo "ssh ${filer} vfiler run ${vfiler_name} cifs shares -change \"${ans_share}\" -accessbasedenum ">>${tmp_bin_file}
      fi
    else 
      echo "ssh ${filer} vfiler run ${vfiler_name} exportfs /vol/${new_volume}/${ans_qtree}">>${tmp_bin_file}
    fi
  fi
else 
  echo "#">>${tmp_bin_file}
  if [ "${ans_tqtree}" = "l" ]
  then
    echo "ssh ${filer} vfiler run ${vfiler_name} qtree create /vol/${old_volume}/${ans_qtree}">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} igroup create -i -t windows ${ans_igroup} ${nodes_list}">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} lun create -s ${ans_lunsize}g -t windows /vol/${new_volume}/${ans_qtree}/${ans_lunname}">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} lun comment /vol/${new_volume}/${ans_qtree}/${ans_lunname} \"GZ:${change_number}\" ">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} lun map /vol/${new_volume}/${ans_qtree}/${ans_lunname} ${ans_igroup}">>${tmp_bin_file}
  elif [ "${ans_tqtree}" = "w" ]
  then
    echo "ssh ${filer} vfiler run ${vfiler_name} qtree create /vol/${old_volume}/${ans_qtree}">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} qtree security /vol/${old_volume}/${ans_qtree} ntfs">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} cifs shares -add \"${ans_share}\" \"/vol/${old_volume}/${ans_qtree}\" -comment \"${filer}:/vol/${old_volume}/${ans_qtree}\" ">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} cifs access \"${ans_share}\" S-1-5-11 Full Control">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} cifs access -delete \"${ans_share}\" everyone Full Control">>${tmp_bin_file}
  else 
    echo "ssh ${filer} vfiler run ${vfiler_name} qtree create /vol/${old_volume}/${ans_qtree}">>${tmp_bin_file}
    echo "ssh ${filer} vfiler run ${vfiler_name} exportfs /vol/${old_volume}/${ans_qtree}">>${tmp_bin_file}
  fi

fi
;;
n) 
break
;;
*) echo ${CLR}
   echo "Invalid choice, press <enter> to go further"
   read ans
;;

esac
done

if [ "${ans_snap}" = "y" ]
then 
  let "vol_size= ${vol_size} * 120 / 100"
fi

while :
do
echo "Secondary storage required as backup with SnapMirror [y/n]?: "
read ans_volume2
	case "$ans_volume2" in
        y) if [ "${ans_volume}" = "y" ]
          then
            echo "Secondary storage required for vfiler DR purposes [y/n]?: "
            read ans_dr
            if [ "${ans_dr}" = "y" ]
            then 
              new_volume2="${new_volume}"
              filer_name2="`ssh ${filer} snapmirror status | grep ${Vrootvol} | awk '{print $2}' | awk -F ":" '{print $1}'`"
	      echo "List of aggregates on this filer ${filer_name2}:"
	      ssh ${filer_name2} df -gA
              echo "Preferred aggregates for customercode ${small_cust_code}:"
              for Aggregate2 in `ssh ${filer_name2} aggr status | grep online | awk '{ print $1 }'| tr -d ' '| sort`
              do
                for Vollist2 in `ssh ${filer_name2} aggr show_space ${Aggregate2} | grep "_${small_cust_code}" |awk '{print $1}'`
                do
                  echo "${Aggregate2}">>${TMPDIR}/create_qtree2.tmp
                done
              done
              Aggr_list2=`cat ${TMPDIR}/create_qtree2.tmp | uniq | awk '{print $1}'`
              if [ -z "${Aggr_list2}" ]
              then Aggr_list2="None"
              fi
              echo "${Aggr_list2}"
	      echo "Give preferred aggregate [f.e. aggr1001]?: "
              read ans_aggregate2

              DefineSnapvault
            else
              echo "Choose type of class:
		a) AO = Archive Class (DBM-Tier4)
		b) BO = Business Class (DBM-Tier2b)
		c) CO = Compliance archive Class
		e) EO = Economy Class
		f) FO = First Class
		"
		while :
		do
		read class_choice2
			case "$class_choice2" in
			a) class_type2="ao"
			new_volume2=ao`echo ${new_volume} |cut -c3-`
			break
			;;
			b) class_type2="bo"
			new_volume2=bo`echo ${new_volume} |cut -c3-`
			break
			;;
			c) class_type2="co"
			new_volume2=co`echo ${new_volume} |cut -c3-`
			break
			;;
			e) class_type2="eo"
			new_volume2=eo`echo ${new_volume} |cut -c3-`
			break
			;;
			f) class_type2="fo"
			new_volume2=fo`echo ${new_volume} |cut -c3-`
			break
			;;
			*) echo ${CLR}
			   echo "Invalid choice, press <enter> to go further"
			   read ans
			;;
			esac
		done

	      echo "List of FSOD filers:"
	      for filer2 in $(cat ${HOME}/etc/filers)
              do
                echo "${filer2}"
              done
              echo " "

              echo "Give filer name for secondary storage [f.e. nlnaf51, nlnaf52]?: "
              read filer_name2
	
              echo "Do you prefer qtree (q) or volume (v) snapmirror [q/v]?: "
              read ans_snapmirror

	      echo "List of aggregates on this filer ${filer_name2}:"
	      ssh ${filer_name2} df -gA
              echo "Preferred aggregates for customercode ${small_cust_code}:"
              for Aggregate2 in `ssh ${filer_name2} aggr status | grep online | awk '{ print $1 }'| tr -d ' '| sort`
              do
                for Vollist2 in `ssh ${filer_name2} aggr show_space ${Aggregate2} | grep "_${small_cust_code}" |awk '{print $1}'`
                do
                  echo "${Aggregate2}">>${TMPDIR}/create_qtree2.tmp
                done
              done
              Aggr_list2=`cat ${TMPDIR}/create_qtree2.tmp | uniq | awk '{print $1}'`
              if [ -z "${Aggr_list2}" ]
              then Aggr_list2="None"
              fi
              echo "${Aggr_list2}"
	      echo "Give preferred aggregate [f.e. aggr2001]?: "
              read ans_aggregate2
            fi

        else
          for old_destination in `ssh ${filer} snapmirror status | grep ${old_volume} | awk '{print $2}'`
          do
            filer_name2=`echo "${old_destination}" | awk -F ":" '{print $1}'`
            old_volume_tmp=`echo "${old_destination}" | awk -F ":" '{print $2}'`
            old_volume_tmp2=`echo "${old_volume_tmp}" | awk -F "/" '{print $3}'`
            if [ -z "${old_volume_tmp2}" ]
            then
              old_volume2="${old_volume_tmp}"
            else
              old_volume2="${old_volume_tmp2}"
            fi
          done
        fi  

	echo "Start creating output files in ${DATADIR}"
	break
	;;
        n) DefineSnapvault
	break
	;;
	*) echo ${CLR}
	   echo "Invalid choice, press <enter> to go further"
	   read ans
	;;
	esac
done

#--------------------------------------------------------------------------#
# Create script for creation of volumes, qtrees, LUN's, etc.               #
#--------------------------------------------------------------------------#
if [ "${new_volume}" != "" ]
then
  echo "ssh ${filer} vol create ${new_volume} -l C ${ans_aggregate} ${vol_size}g">>${bin_file}
  echo "ssh ${filer} snap sched ${new_volume} ${snap_weekly} ${snap_nightly} ${snap_hourly}">>${bin_file}
  echo "ssh ${filer} snap reserve ${new_volume} 0">>${bin_file}
  echo "ssh ${filer} vol options ${new_volume} fractional_reserve 0">>${bin_file}
  echo "ssh ${filer} vfiler add ${vfiler_name} /vol/${new_volume}">>${bin_file}
  cat ${tmp_bin_file}>>${bin_file}
  echo "ssh ${filer} vfiler run ${vfiler_name} quota on ${new_volume}">>${bin_file}
  if [ "${ans_sis}" = "y" ]
  then
    echo "ssh ${filer} sis on /vol/${new_volume}">>${bin_file}
    echo "ssh ${filer} sis start -s /vol/${new_volume}">>${bin_file}
    echo "ssh ${filer} sis config -s ${ans_timesis}@mon-sat /vol/${new_volume}">>${bin_file}
  fi
else 
  echo "ssh ${filer} vol size ${old_volume} +${vol_size}g">>${bin_file}
  cat ${tmp_bin_file}>>${bin_file}
  echo "ssh ${filer} vfiler run ${vfiler_name} quota off ${old_volume}">>${bin_file}
  echo "ssh ${filer} vfiler run ${vfiler_name} quota on ${old_volume}">>${bin_file}
  if [ "${ans_sis}" = "y" ]
  then
    echo "ssh ${filer} sis on /vol/${old_volume}">>${bin_file}
    echo "ssh ${filer} sis start -s /vol/${old_volume}">>${bin_file}
    echo "ssh ${filer} sis config -s ${ans_timesis}@mon-sat /vol/${old_volume}">>${bin_file}
  fi
fi

if [ "${ans_volume2}" = "y" ]
then
  if [ "${ans_volume}" = "y" ]
  then
    if [ "${ans_dr}" = "y" ]
    then
      echo "ssh ${filer_name2} vol create ${new_volume2} -l C ${ans_aggregate2} ${vol_size}g">>${bin_file}
      echo "#ssh ${filer} options rsh.access legacy">>${bin_file}
      echo "#ssh ${filer} options rsh.enable on">>${bin_file}
      echo "#ssh ${filer_name2} vfiler dr resync ${vfiler_name}@${filer}-sm">>${bin_file}
      echo "#ssh ${filer} options rsh.access none">>${bin_file}
      echo "#ssh ${filer} options rsh.enable off">>${bin_file}
      echo "${filer}-sm:${new_volume} ${filer_name2}:${new_volume2} - 0 0-23/4 * *">${conf_file}
      if [ "${ans_sv}" = "y" ]
      then
        echo "ssh ${filer_name3} vol create ${new_volume3} -l C ${ans_aggregate3} ${vol_size}g">>${bin_file}
        echo "ssh ${filer_name3} snap sched ${new_volume3} 0 0 0">>${bin_file}
        echo "ssh ${filer_name3} snap reserve ${new_volume3} 0">>${bin_file}
        echo "ssh ${filer_name3} vol options ${new_volume3} fractional_reserve 0">>${bin_file}
        if [ "${ans_svdsnap}" = "y" ]
        then
          echo "ssh ${filer} snapvault snap sched ${new_volume} sv_daily 1@mon-sun@${time_sv_daily}">>${bin_file}
          echo "ssh ${filer_name3} snapvault snap sched -x ${new_volume3} sv_daily ${ans_vsvdsnap}@mon-sun@1">>${bin_file}
        fi
        if [ "${ans_svwsnap}" = "y" ]
        then
          echo "ssh ${filer} snapvault snap sched ${new_volume} sv_weekly 1@mon-sun@${time_sv_weekly}">>${bin_file}
          echo "ssh ${filer_name3} snapvault snap sched -x ${new_volume3} sv_weekly ${ans_vsvwsnap}@mon-sun@1">>${bin_file}
        fi
      echo "ssh ${filer_name3} snapvault start -S ${filer}-sm:/vol/${new_volume}/${ans_qtree} /vol/${new_volume3}/${ans_qtree}">>${bin_file}
      fi
    else 
      echo "ssh ${filer_name2} vol create ${new_volume2} -l C ${ans_aggregate2} ${vol_size}g">>${bin_file}
      if [ "${ans_snapmirror}" = "v" ]
      then
        echo "ssh ${filer_name2} vol restrict ${new_volume2}">>${bin_file}
        echo "ssh ${filer_name2} snapmirror initialize -S ${filer}-sm:${new_volume} ${new_volume2}">>${bin_file}
        echo "${filer}-sm:${new_volume} ${filer_name2}:${new_volume2} - 0 1 * *">${conf_file}
      else
        echo "ssh ${filer_name2} snapmirror initialize -S ${filer}-sm:/vol/${new_volume}/${ans_qtree} /vol/${new_volume2}/${ans_qtree}">>${bin_file}
        echo "${filer}-sm:/vol/${new_volume}/${ans_qtree} ${filer_name2}:/vol/${new_volume2}/${ans_qtree} - 0 1 * *">${conf_file}
      fi
    fi
  else
    echo "ssh ${filer_name2} vol size ${old_volume2} +${vol_size}g">>${bin_file}
  fi
else [ "${ans_sv}" = "y" ]
  echo "ssh ${filer_name3} vol create ${new_volume3} -l C ${ans_aggregate3} ${vol_size}g">>${bin_file}
  echo "ssh ${filer_name3} snap sched ${new_volume3} 0 0 0">>${bin_file}
  echo "ssh ${filer_name3} snap reserve ${new_volume3} 0">>${bin_file}
  echo "ssh ${filer_name3} vol options ${new_volume3} fractional_reserve 0">>${bin_file}
  if [ "${ans_svdsnap}" = "y" ]
  then
    echo "ssh ${filer} snapvault snap sched ${new_volume} sv_daily 1@mon-sun@${time_sv_daily}">>${bin_file}
    echo "ssh ${filer_name3} snapvault snap sched -x ${new_volume3} sv_daily ${ans_vsvdsnap}@mon-sun@1">>${bin_file}
  fi
  if [ "${ans_svwsnap}" = "y" ]
  then
    echo "ssh ${filer} snapvault snap sched ${new_volume} sv_weekly 1@mon-sun@${time_sv_weekly}">>${bin_file}
    echo "ssh ${filer_name3} snapvault snap sched -x ${new_volume3} sv_weekly ${ans_vsvwsnap}@mon-sun@1">>${bin_file}
  fi
  echo "ssh ${filer_name3} snapvault start -S ${filer}-sm:/vol/${new_volume}/${ans_qtree} /vol/${new_volume3}/${ans_qtree}">>${bin_file}
fi

#--------------------------------------------------------------------------#
# In case of Sanoma vfiler destination volume name will change             #
#--------------------------------------------------------------------------#
if [ "${small_cust_code}" = "san" ]
then
  cp ${bin_file} ${tmp_bin_file}
  cat ${tmp_bin_file} | sed "s/ao_san01_/ao_san02_/g">${bin_file}

  if [ -f "${conf_file}" ]
  then
    cp ${conf_file} ${conf_file}.tmp
    cat ${conf_file}.tmp  | sed "s/ao_san01_/ao_san02_/g">${conf_file}
    rm ${conf_file}.tmp
  fi
fi

#--------------------------------------------------------------------------#
# Remove tmp files                                                         #
#--------------------------------------------------------------------------#
rm ${TMPDIR}/create_qtree.tmp
rm ${TMPDIR}/create_qtree2.tmp
rm ${TMPDIR}/create_qtree3.tmp
rm ${tmp_bin_file}

