#!/bin/sh
#
# Create bootstrap information files -- prelude to creating a
#   Bacula Rescue data for USB key
#
#   Kern Sibbald, December MMII
#      This source distributed under the GPLv2
#
#find out on which device is the usb key

if [ -f ../../../scripts/nozen ]; then 
DIALOG=dialog
else
DIALOG=Xdialog
fi

cd ..
one=1
etherdone=0
none=" "
usbdev=`blkid|grep bs-rescue|awk '{print $1}'|cut -d: -f1`
usbdev=${usbdev%?}

export LANG=C
cwd=`pwd`
owd=`pwd`


os=`uname -s`
if [ ! "x$1" = "x" ] ; then
  host=clients/$1
else
  host=clients/`hostname`
fi

if [ -f scripts/differenthostname ]; then 
host=clients/`cat scripts/differenthostname`
rm scripts/differenthostname
fi

echo "Host specific data will be saved in the directory: ${host}"
echo " "

if [ -f $host.bak/scripts/mount_drives ] ;then
rm -r $host.bak
fi

if [ -f $host/scripts/mount_drives ] ;then
mv $host $host.bak
fi

mkdir -p $host
di=${cwd}/$host/diskinfo
scr=${cwd}/$host/scripts
dirusb=/media/bs-rescue/bacula-rescue/$host
case $os in
 Linux)
  ;;
 FreeBSD | SunOS | IRIX)
   echo ""
   echo "This code is not yet adapted to this OS"     
   exit 1
   ;;
 *)
   echo ""
   echo "Unknown operating system type: $os"     
   exit 1
   ;;
esac
if [ ! `whoami` = "root" ] ; then
  echo ""
  echo "You need to be root to run this script ..."
  echo ""
  exit 1
fi

#
#  First collect information
#
rm -rf format.* partition.* $di create-* $scr
echo "Begin collecting system info ..."
mkdir -p $di
cd $di
mount -l >mount.bsi
 fdisk -l|grep :|grep -v identifier|grep -v $usbdev|awk '{print $2}'|cut -d: -f1 >devtmp
cat devtmp|while read line;do
li=`echo $line|cut -d/ -f3`
fdisk -l $line > fdisk.$li
parted $line print > parted.$li
done
rm devtmp

mount -l -t jfs >mount.jfs.bsi
blkid |grep jfs >blkid.jfs.bsi
mount -l -t vfat >mount.vfat.bsi
blkid |grep vfat >blkid.vfat.bsi
mount -l -t xfs >mount.xfs.bsi
blkid |grep xfs >blkid.xfs.bsi
mount -l -t hfs >mount.hfs.bsi
blkid |grep hfs >blkid.hfs.bsi
mount -l -t hfsplus >mount.hfsplus.bsi
blkid |grep hfsplus >blkid.hfsplus.bsi
mount -l -t fuseblk >mount.fuseblk.bsi
mount -l -t ext2 >mount.ext2.bsi
blkid |grep ext2 >blkid.ext2.bsi
mount -l -t ext3 >mount.ext3.bsi
blkid |grep ext3 >blkid.ext3.bsi
mount -l -t ext4 >mount.ext4.bsi
blkid |grep ext4 >blkid.ext4.bsi

########################################## problem
#blkid |grep ntfs >mount.ntfs.bsi

blkid |grep ntfs >blkid.ntfs.bsi
touch mount.ntfs.bsi
cat mount.fuseblk.bsi |awk '{print $1}'|while read line; do
blkid|grep $line|grep ntfs>/dev/null 2>&1
if [ $? = 0 ];then
a=`cat mount.fuseblk.bsi|grep $line|sed 's/fuseblk/ntfs/g'`
echo $a >>mount.ntfs.bsi
fi
done

###########################################3

mount -l -t reiserfs >mount.reiser.bsi
blkid |grep reiserfs >blkid.reiserfs.bsi
cp /etc/fstab fstab.bsi
cp /etc/mtab  mtab.bsi     
df -Tl >df.bsi



# Pickup all disks except USB_DEV
sfdisk -s | grep -v ${usbdev}  >sfdisk.disks.bsi
#sfdisk -s  >sfdisk.disks.bsi
grep "^/dev/" sfdisk.disks.bsi | sed -n 's%\(^/dev/[A-Za-z]*\):.*$%\1%p' >disks.bsi
for i in `cat disks.bsi`; do
   j=`echo $i | cut -c6-`
   sfdisk -l $i >sfdisk.$j.bsi 
   sfdisk -d $i >sfdisk.make.$j.bsi
done
cat /proc/swaps > swaps.bsi
route -n >route.bsi
ifconfig >ifconfig.bsi

# Gather LVM information
# Only tested on LVM2
dolvm=0
if  which lvm > /dev/null 2>/dev/null ; then
    echo "Gathering LVM information"
    dolvm=1

    lvm pvs --noheadings --nosuffix -o pv_name,vg_name | cat > lvm-pv.bsi
    lvm vgs --noheadings --nosuffix -o vg_name,vg_extent_size --units=k | cat > lvm-vg.bsi
    lvm lvs --noheadings --nosuffix -o lv_name,vg_name,lv_size --units=k | cat > lvm-lv.bsi
fi

echo "Done collecting info."

#
# Done collecting information
#


echo "Begin creating scripts ..."
#
# First create partitioning script(s)
#
mkdir -p $scr
for i in `cat disks.bsi`; do
  j=`echo $i | cut -c6-`
  cat >$scr/partition.$j <<END_OF_DATA

#!/bin/sh
#
#  Partition disk $i  -- created by getdiskinfo
echo ""
echo "This script will repartition disk $i."
echo ""
echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
echo ""
if [ ! -f noprompt ];then
echo -n "Are you sure you want to continue? yes/no: "
read a
if [ x\$a != xyes ] ; then
   exit 1
fi 
fi
echo "Partitioning disk $i"
# zap partition info
dd if=/dev/zero of=$i bs=512 count=1
# repartition
sfdisk --force $i <$dirusb/diskinfo/sfdisk.make.$j.bsi | less
echo ""
echo "The previous partitioning was:"
cat $dirusb/diskinfo/sfdisk.$j.bsi
#
echo ""
echo "The new partitioning is:"
sfdisk -l $i
echo ""
echo "If the disk is correctly partitioned, you should"
echo "now run the \"format.$j\" script."
echo ""
partprobe >>/dev/null 2>&1
END_OF_DATA


chmod 755 $scr/partition.$j
done
echo "Done making partitioning scripts"

if [ $dolvm -eq 1 ] ; then
# 
# LVM phase 1: create the Physical Volumess
#
    cat > $scr/create-pv <<EOF
#!/bin/sh
#
# Create PVs for LVM -- created by getdiskinfo
echo ""
echo "This script will format all partitions listed in diskinfo/lvm-pv.bsi"
echo ""
echo "IT WILL DESTROY ALL DATA ON THESE PARTITIONS!!!!"
echo ""
EOF
    for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
        echo "echo \"    $pv\"" >> $scr/create-pv
    done

    cat >> $scr/create-pv <<EOF
echo ""
if [ ! -f noprompt ];then
echo -n "Are you sure you want to continue? yes/no: "
read a
if [ x\$a != xyes ] ; then
   exit 1
fi 
fi
EOF
    for pv in `cat lvm-pv.bsi | awk '{print $1}'`; do
        echo "echo \"Creating PV on $pv\"" >> $scr/create-pv
        echo "lvm pvcreate -ff -y -v $pv" >> $scr/create-pv
    done
    echo "echo \"Done.\"" >> $scr/create-pv
    echo "echo \"If there were no errors, run create-vg to create the volume groups.\"" >> $scr/create-pv
    chmod +x $scr/create-pv

#
# LVM phase 2: create Volume Groups
#
    cat > $scr/create-vg <<EOF
#!/bin/sh
echo ""
echo "This script will create all volume groups listed in diskinfo/lvm-vg.bsi"
echo "You must have sucesfully run create-pv to create the requesite PVs first"
echo ""
if [ ! -f noprompt ];then
echo -n "Are you sure you want to continue? yes/no: "
read a
if [ x\$a != xyes ] ; then
   exit 1
fi
fi
EOF
    cat lvm-vg.bsi | awk '{print $1, $2}' | while read vg pesize ; do
        echo "echo \"Creating VG $vg PE extent size $pesize kbytes"\" >> $scr/create-vg
        pelist=""
        for pv in `cat lvm-pv.bsi | awk "\\\$2 == \"$vg\" { print \\\$1 }"` ; do
            pelist="$pelist $pv"
        done
        echo "lvm vgcreate $vg -v -s ${pesize}k $pelist" >> $scr/create-vg
    done
    echo "echo \"Done.\"" >> $scr/create-vg
    echo "echo \"If there were no errors, run create-lv to create the logical volumes.\"" >> $scr/create-vg
    chmod +x $scr/create-vg


#
# LVM phase 3: create Logical Volumes 
#
# One quick sidenote: this script creates LVs with a size based on the
# physical extent count, rather than the size in bytes.  This is because
# at the time of writing this script, lvdisplay erroneously prints out
# the size in 512 byte sector count rather than 1024 byte blocks.
# Using the extent count should allow this script to continue to work
# even after the bug is fixed.

    cat > $scr/create-lv <<EOF
#!/bin/sh
echo ""
echo "This script will create all logical volumes listed in diskinfo/lvm-lv.bsi"
echo "You must have sucesfully run create-vg to create the requesite VGs first"
echo ""
if [ ! -f noprompt ];then
echo -n "Are you sure you want to continue? yes/no: "
read a
if [ x\$a != xyes ] ; then
   exit 1
fi
fi
EOF
    cat lvm-lv.bsi | awk '{print $1, $2, $3}' | while read lv vg lvsize ; do
        # lv=`echo $lv | sed -e 's/.*\///'`
        echo "echo \"Creating LV $lv\"" >> $scr/create-lv
        echo "lvm lvcreate -L ${lvsize}k $vg -n $lv" >> $scr/create-lv
    done
    chmod +x $scr/create-lv
fi

#
# Create formatting script(s)
#
echo "Begin making formatting script(s) ..."
for i in `cat disks.bsi`; do
  j=`echo $i | cut -c6-`
  cat >$scr/format.$j <<END_OF_DATA
#!/bin/sh
#
#  Format all partitions on disk $i -- created by getdiskinfo
#
echo ""
echo "This script will format all partitions on disk $i."
echo ""
echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
echo ""
if [ ! -f noprompt ];then
echo -n "Are you sure you want to continue? yes/no: "
read a
if [ x\$a != xyes ] ; then
   exit 1
fi 
fi
if [ ! -f badblocksnoprompt ];then
echo "Do you want to do a disk check for bad blocks?"
echo -n "It is recommended, but takes time. yes/no: "
read a
if [ x\$a = xyes ] ; then
   check="-c"
else
   check=
fi
else
   check=
fi
END_OF_DATA

   # Find swap partitions in output from sfdisk
   k=`grep "^$i.*82  Linux swap" sfdisk.$j.bsi | cut -d ' ' -f 1`
   for disk in $k; do
   fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done    
   echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$j
      echo "mkswap $check -U $u $disk" >>$scr/format.$j
      echo "echo \"\"" >>$scr/format.$j
   done

   # Find ext2 partitions in mount output
   k=`grep "^$i" mount.ext2.bsi | cut -d ' ' -f 1`
   for disk in $k; do
    fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done  
   echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.ext2.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ext2 -v \$check $disk" >>$scr/format.$j
      else
         echo "mkfs.ext2 -v \$check -L $label -U $u $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done
   # Find ext3 partitions in mount output
   k=`grep "^$i" mount.ext3.bsi | cut -d ' ' -f 1`
   for disk in $k; do
    fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done  
    echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.ext3.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ext3 -v -j \$check -U $u $disk" >>$scr/format.$j
      else
         echo "mkfs.ext3 -v -j \$check -L $label -U $u $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done


# Find ext4 partitions in mount output
   k=`grep "^$i" mount.ext4.bsi | cut -d ' ' -f 1`
   for disk in $k; do
    fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done  
    echo "echo \"Formating $disk -- ext4 partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.ext4.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ext4 -v -j \$check -U $u $disk" >>$scr/format.$j
      else
         echo "mkfs.ext4 -v -j \$check -L $label -U $u $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done

 # Find xfs partitions in mount output
   k=`grep "^$i" mount.xfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
 
   
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done


   
     echo "echo \"Formating $disk -- xfs partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.xfs.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.xfs -f \$check  $disk" >>$scr/format.$j
      else
         echo "mkfs.xfs -f \$check -L $label  $disk" >>$scr/format.$j
      fi
	echo "xfs_admin -U $u $disk">>$scr/format.$j
      echo "echo \"\"" >>$scr/format.$j
   done 


 # Find jfs partitions in mount output
   k=`grep "^$i" mount.jfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
 fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
    
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done
     echo "echo \"Formating $disk -- jfs partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.jfs.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.jfs -f \$check  $disk" >>$scr/format.$j
      else
         echo "mkfs.jfs -f \$check -L $label $disk" >>$scr/format.$j
      fi
	 echo "jfs_tune -U $u $disk " >>$scr/format.$j
      echo "echo \"\"" >>$scr/format.$j
   done 
 # Find vfat partitions in mount output
   k=`grep "^$i" mount.vfat.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     u=`echo $u|sed 's/-//g'`
     fi
     fields=$(( fields - 1 ))
     done  
      echo "echo \"Formating $disk -- vfat partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.vfat.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.vfat \$check -i $u $disk" >>$scr/format.$j
      else
         echo "mkfs.vfat \$check -n $label -i $u $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done
 # Find reiserfs partitions in mount output
   k=`grep "^$i" mount.reiser.bsi | cut -d ' ' -f 1`
   for disk in $k; do
      fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done
     echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.reiser.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkreiserfs -u $u $disk" >>$scr/format.$j
      else
         echo "mkreiserfs -l $label -u $u $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done


# Find hfs partitions in mount output

   k=`grep "^$i" mount.hfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
 
     echo "echo \"Formating $disk -- hfs partition\"" >>$scr/format.$j
       label=`grep "^$disk" mount.hfs.bsi  |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.hfs \$check  $disk" >>$scr/format.$j
      else
         echo "mkfs.hfs \$check -v $label  $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done 


# Find hfsplus partitions in mount output

   k=`grep "^$i" mount.hfsplus.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     
      echo "echo \"Formating $disk -- hfsplus partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.hfsplus.bsi  |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.hfsplus \$check  $disk" >>$scr/format.$j
      else
         echo "mkfs.hfsplus \$check -v $label  $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done


# Find ntfs partitions in mount output
   k=`grep "^$i" mount.ntfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     
      echo "echo \"Formating $disk -- ntfs partition\"" >>$scr/format.$j
      label=`grep "^$disk" mount.ntfs.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ntfs \$check  $disk" >>$scr/format.$j
      else
         echo "mkfs.ntfs \$check -L $label  $disk" >>$scr/format.$j
      fi
      echo "echo \"\"" >>$scr/format.$j
   done
   chmod 755 $scr/format.$j
done
#
# Create LVM formatting scripts(s)
#
# Somebody more clever than I with bash scripting
# could probably factor a lot of this code out.
#
if [ $dolvm -eq 1 ] ; then
    echo "Begin making LVM formatting script(s) ..."
    for i in `cat lvm-vg.bsi | awk '{print $1}'`; do
        cat >$scr/format.$i <<END_OF_DATA
#!/bin/sh
#
#  Format all partitions on VG $i -- created by getdiskinfo
#
echo ""
echo "This script will format all LVs on VG $i."
echo ""
echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
echo ""
if [ ! -f noprompt ];then
echo -n "Are you sure you want to continue? yes/no: "
read a
if [ x\$a != xyes ] ; then
   exit 1
fi
fi
if [ ! -f badblocksnoprompt ];then
echo "Do you want to do a disk check for bad blocks?"
echo -n "It is recommended, but takes time. yes/no: "
read a
if [ x\$a = xyes ] ; then
   check="-c"
else
   check=
fi
else
check=
fi
END_OF_DATA

   # Find swap partitions in output from sfdisk
   
    mapper="/dev/mapper/${i}"
   k=`grep "^$mapper" swaps.bsi | awk '{print $1}'`
   for disk in $k; do
   fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done  
   echo "echo \"Formatting $disk -- swap partition\"" >>$scr/format.$i
      echo "mkswap $check -U $u $disk" >>$scr/format.$i
      echo "echo \"\"" >>$scr/format.$i
   done

   # Find ext2 partitions in mount output
   k=`grep "^$mapper" mount.ext2.bsi | cut -d ' ' -f 1`
   for disk in $k; do
    fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done  
   echo "echo \"Formating $disk -- ext2 partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.ext2.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ext2 -v \$check $disk" >>$scr/format.$i
      else
         echo "mkfs.ext2 -v \$check -L $label -U $u $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done
   # Find ext3 partitions in mount output
   k=`grep "^$mapper" mount.ext3.bsi | cut -d ' ' -f 1`
   for disk in $k; do
    fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done  
    echo "echo \"Formating $disk -- ext3 partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.ext3.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ext3 -v -j \$check -U $u $disk" >>$scr/format.$i
      else
         echo "mkfs.ext3 -v -j \$check -L $label -U $u $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done


# Find ext4 partitions in mount output
   k=`grep "^$mapper" mount.ext4.bsi | cut -d ' ' -f 1`
   for disk in $k; do
    fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done  
    echo "echo \"Formating $disk -- ext4 partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.ext4.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ext4 -v -j \$check -U $u $disk" >>$scr/format.$i
      else
         echo "mkfs.ext4 -v -j \$check -L $label -U $u $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done

 # Find xfs partitions in mount output
   k=`grep "^$mapper" mount.xfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
 
   
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done


   
     echo "echo \"Formating $disk -- xfs partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.xfs.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.xfs -f \$check  $disk" >>$scr/format.$i
      else
         echo "mkfs.xfs -f \$check -L $label  $disk" >>$scr/format.$i
      fi
	echo "xfs_admin -U $u $disk">>$scr/format.$i
      echo "echo \"\"" >>$scr/format.$i
   done 


 # Find jfs partitions in mount output
   k=`grep "^$mapper" mount.jfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
 fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
    
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done
     echo "echo \"Formating $disk -- jfs partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.jfs.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.jfs -f \$check  $disk" >>$scr/format.$i
      else
         echo "mkfs.jfs -f \$check -L $label $disk" >>$scr/format.$i
      fi
	 echo "jfs_tune -U $u $disk " >>$scr/format.$i
      echo "echo \"\"" >>$scr/format.$i
   done 
 # Find vfat partitions in mount output
   k=`grep "^$mapper" mount.vfat.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     u=`echo $u|sed 's/-//g'`
     fi
     fields=$(( fields - 1 ))
     done  
      echo "echo \"Formating $disk -- vfat partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.vfat.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.vfat \$check -i $u $disk" >>$scr/format.$i
      else
         echo "mkfs.vfat \$check -n $label -i $u $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done
 # Find reiserfs partitions in mount output
   k=`grep "^$mapper" mount.reiser.bsi | cut -d ' ' -f 1`
   for disk in $k; do
      fields=`blkid |grep $disk|wc -w`
     kid=`blkid |grep $disk`
     while [ $fields -ne 0 ];do
     echo $kid|awk "{print \$ $fields}"|grep UUID>/dev/null 2>&1
     if [ $? = 0 ];then
     u=`echo $kid|awk "{print \$ $fields}"|cut -d'"' -f2`
     fi
     fields=$(( fields - 1 ))
     done
     echo "echo \"Formating $disk -- reiser partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.reiser.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkreiserfs -u $u $disk" >>$scr/format.$i
      else
         echo "mkreiserfs -l $label -u $u $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done


# Find hfs partitions in mount output

   k=`grep "^$mapper" mount.hfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
 
     echo "echo \"Formating $disk -- hfs partition\"" >>$scr/format.$i
       label=`grep "^$disk" mount.hfs.bsi  |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.hfs \$check  $disk" >>$scr/format.$i
      else
         echo "mkfs.hfs \$check -v $label  $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done 


# Find hfsplus partitions in mount output

   k=`grep "^$mapper" mount.hfsplus.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     
      echo "echo \"Formating $disk -- hfsplus partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.hfsplus.bsi  |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.hfsplus \$check  $disk" >>$scr/format.$i
      else
         echo "mkfs.hfsplus \$check -v $label  $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done


# Find ntfs partitions in mount output
   k=`grep "^$mapper" mount.ntfs.bsi | cut -d ' ' -f 1`
   for disk in $k; do
     
      echo "echo \"Formating $disk -- ntfs partition\"" >>$scr/format.$i
      label=`grep "^$disk" mount.ntfs.bsi |awk '{print $7}'|cut -d "]" -f1|cut -d "[" -f2`
      if [ x$label = x ] ; then
         echo "mkfs.ntfs \$check  $disk" >>$scr/format.$i
      else
         echo "mkfs.ntfs \$check -L $label  $disk" >>$scr/format.$i
      fi
      echo "echo \"\"" >>$scr/format.$i
   done




   chmod 755 $scr/format.$i
   done
fi

cd $scr

#
# Create network start script
#



if [ -f ../../../scripts/nozen ]; then 
DIALOG=dialog
else
DIALOG=Xdialog
fi

howmanyether=`ifconfig |cut -d" " -f1 |grep -v '^$' |grep -v lo|wc -l`
if [ $howmanyether -gt 1 ] ; then
netline=0
cp  ../../../scripts/dialognetworkorig  dialognetwork
sed -i 's/howmany/'$howmanyether'/g' dialognetwork
sed -i 's/DIAL/'$DIALOG'/g' dialognetwork
ifconfig |cut -d" " -f1 |grep -v '^$' |grep -v lo | while read LINE ; do
confnet=$LINE
netline=$(( netline + 1 ))
if [ $netline -ne $howmanyether ];then
echo $netline $LINE ' off  ' >> dialognetwork
else 
echo $netline $LINE ' off 2>ans ' >> dialognetwork
fi
done
dial=`cat dialognetwork`

echo $dial > dialognetwork
chmod +x dialognetwork
. ./dialognetwork
	if [ $? = 1 ];then
	exit 1
	fi
ans=`cat ans`
rm ans
rm dialognetwork
ifconfig |cut -d" " -f1 |grep -v '^$' |grep -v lo > net
net=`cat net`
netdevice=`echo $net |awk "{print \$ $ans}" `
etherdone=1
rm net

fi


if [ $etherdone = 0 ] ; then
 ether=`ifconfig |cut -d" " -f1 |grep -v '^$' |grep -v lo`

else
    ether=`echo $netdevice`
fi

ifconfig $ether 2>/dev/null >/dev/null
if [ $? = 0 ] ; then
  ip=`ifconfig $ether | grep inet | head -1 | sed -n 's/\ \+inet addr:\([0-9]\+\(\.[0-9]\+\)\{3\}\).*/\1/p'`
  cat >start_network <<END_OF_DATA
#!/bin/sh
#
#  Start network -- created by getdiskinfo
#
ip=$ip
dev=$ether
ifconfig lo up
ifconfig \$dev up \$ip
END_OF_DATA
else
   echo "Warning, no ethernet devices found."
   echo "#!/bin/sh" >start_network
fi

ip=`route -n | grep UG | head -1 | awk '{print $2}'`
dev=`route -n | grep UG | head -1 | awk '{print $8}'`
cat >>start_network <<END_OF_DATA
route add default gw $ip  $ether
END_OF_DATA

chmod 755 start_network



cat >$scr/mount_drives <<END_OF_DATA
#!/bin/sh
#
#  Mount disk drives  -- created by getdiskinfo
#
PATH=$PATH:/var/chroot/sbin:/var/chroot/bin
END_OF_DATA
TMP1=`mktemp gdi.1.XXXXXXXXXX`
TMP2=`mktemp gdi.2.XXXXXXXXXX`
if test x${TMP1} = x; then
   TMP1="/tmp/1$$"
   if test -f ${TMP1}; then
      echo "Temp file security problem on: ${TMP1}"
      exit 1
   fi
fi
if test x${TMP2} = x; then
   TMP2="/tmp/2$$"
   if test -f ${TMP2}; then
      echo "Temp file security problem on: ${TMP2}"
      rm -f ${TMP1}
      exit 1
   fi
fi
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.ext2.bsi >${TMP1}
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.ext3.bsi >>${TMP1}
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.reiser.bsi >>${TMP1}
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.hfs.bsi >>${TMP1}
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.xfs.bsi >>${TMP1}
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.jfs.bsi >>${TMP1}
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.fuseblk.bsi >>${TMP1}
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/\1 \/media\/bs-rescue\/chroot\2/p' $di/mount.vfat.bsi >>${TMP1}

# sort so that / is first
sort -k 2 <${TMP1} >${TMP2}
# output mkdir followed by its mount
sed -n 's/\(^.*\)\ \(.*$\)/mkdir -p \2  #\1\
mount \1 \2/p' ${TMP2} >>$scr/mount_drives

# Now build unmount_drives
cat >$scr/umount_drives <<END_OF_DATA
#!/bin/sh
#
#  Unmount disk drives  -- created by getdiskinfo
#
END_OF_DATA
sort -r -k 2 <${TMP1} >${TMP2}
sed -n 's/\(^.*\)\ \(.*$\)/umount \2/p' ${TMP2} >>$scr/umount_drives
echo "umount /mnt/cdrom 2>/dev/null >/dev/null" >>$scr/umount_drives
echo "sync" >>$scr/umount_drives
echo "sync" >>$scr/umount_drives

rm -f ${TMP1} ${TMP2}

chmod 755 $scr/mount_drives $scr/umount_drives

#output result of blkid

blkid | while read LINE ; do
echo $LINE >> $cwd/$host/diskinfo/blkidinfo
done

#find raid devices

.  $cwd/scripts/findraidevice/findraid_new

if [ ! -f $cwd/$host/scripts/recreate_raid_arrays ];then
 
echo "";echo "";echo "";echo "You do not seem to have any soft raid devices";echo "";echo "";echo ""
fi

. $cwd/scripts/new_baklient_conf
. $cwd/scripts/get_serial
rm $cwd/scripts/ans

echo "Done building scripts."
$DIALOG --title "That's it" --msgbox "We are done gathering info \n you can now restore your machine \n (in case it crashes) \n booting on this usb key \n \n \n P.S. If your computer's bios doesn't support booting off usb, \n insert the provided boot cd as well as this usb key and the magic will happen...." 600 600
