#!/bin/sh
# $Id: sbc_disk_info_restore_auto,v 1.1 2005/01/31 14:25:10 sesam Exp $
# Restores sesam disk_info saveset of a linux computer
# (Partitions, filesystems, mount ....)
#
#   SEP AG 07.2003
#
tmp_info_dir=$1
AUTO=$2
curr_work_dir=`pwd`
system_name=`uname -s`

#############################################################
# READ INI
read_ini()
{
  SM_INI=`grep -i '^sm_ini=' /etc/sesam2000.ini|cut -d"=" -f2`
  echo `grep -i "$1=" "$SM_INI"|cut -d"=" -f2`
}
#
#
# VERBOSE
my_echo()
{
  if [ "$VERBOSE" = "-v" ]
  then
    echo $1
  fi
}
#
# PATH SUBROUTINE
sub_path()
{
  my_echo "VERSION=$VERSION"

  GV_RO=`read_ini gv_ro`
  export GV_RO
  GV_RW=`read_ini gv_rw`
  export GV_RW
  GV_RW_INI=`read_ini gv_rw_ini`
  export GV_RW_INI
  GV_RO_DB=`read_ini gv_ro_db`
  export GV_RO_DB
  GV_RO_BIN=`read_ini gv_ro_bin`
  export GV_RO_BIN

  PATH=:${GV_RO_BIN}:${GV_RO_DB}:${GV_RO}skel:$PATH:/sbin:/bin:/usr/bin
  export PATH
}
#############################################################

case $system_name in
 Linux)
  ;;
 *)
   echo ""
   echo "Script is not yet adapted to this OS: $system_name"
   exit 1
   ;;
esac
if [ ! `whoami` = "root" ] ; then
  echo ""
  echo "You should be 'root' to do this. Continuing anyway ..."
  echo ""
fi

sub_path


a=ja

cd $tmp_info_dir
#
# First make the partitioning loop
#
for i in `cat disks.sdi`; do
  j=`echo $i | cut -c6-`
#
echo ""
echo "Partition table during backup was:"
cat sfdisk.$j.sdi
echo ""
echo "Current partition table:"
sfdisk -l $i
sfdisk -R $i
echo ""
echo "This script will repartition disk $i to its previous partitioning state."
echo ""
echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
echo ""


if [ x$a == xja ] ; then
echo ""
echo "Partitioning disk $i"
# restore partition info
dd if=dd.mbr12.$j.sdi of=$i bs=512 count=2
# repartition
sfdisk -f $i <sfdisk.make.$j.sdi 
#
echo ""
echo "The new partitioning is:"
sfdisk -l $i
echo ""
echo "If the disk is correctly partitioned, you should"
echo "now go on with the formatting."
echo ""
fi

done

#
# Now make the formatting loop
#
for i in `cat disks.sdi`; do
  j=`echo $i | cut -c6-`
echo ""
echo "This script will format all partitions on disk $i."
echo ""
echo "IT WILL DESTROY ALL DATA ON DISK $i !!!!"
echo ""


if [ x$a == xja ] ; then

check="-c"
   # Find swap partitions in output from sfdisk
   k=`grep "^$i.*82  Linux [Ss]wap" sfdisk.$j.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- swap partition"
      mkswap $check $disk
      echo ""
   done
   # Find ext2 partitions in mount output
   k=`grep "^$i" mount.ext2.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- ext2 partition"
      label=`grep "^$disk" mount.ext2.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         mke2fs -v $check $disk
      else
         mke2fs -v $check -L $label $disk
      fi
      tune2fs -O ^dir_index $disk
      echo ""
   done
   # Find ext3 partitions in mount output
   k=`grep "^$i" mount.ext3.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- ext3 partition"
      label=`grep "^$disk" mount.ext3.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         mke2fs -v -j $check $disk
      else
         mke2fs -v -j $check -L $label $disk
      fi
      tune2fs -O ^dir_index $disk
      echo ""
   done
   # Find reiserfs partitions in mount output
   k=`grep "^$i" mount.reiserfs.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- reiserfs partition"
      label=`grep "^$disk" mount.reiserfs.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         mkreiserfs -d -f -f $disk
      else
         mkreiserfs -d -f -f -l $label $disk
      fi
      echo ""
   done
   # Find xfs partitions in mount output
   k=`grep "^$i" mount.xfs.sdi | cut -d ' ' -f 1`
   for disk in $k; do
      echo "Formatting $disk -- xfs partition"
      label=`grep "^$disk" mount.xfs.sdi | cut -d ' ' -f 7 | cut -c2- | cut -d ] -f 1`
      if [ x$label = x ] ; then
         mkfs.xfs $disk
      else
         mkfs.xfs -L $label $disk
      fi
      echo ""
   done
fi

done

# goto work directory
cd $1

cat >sbc_mount_drives <<END_OF_DATA
#!/bin/sh
#
#  Mount disk drives  -- created by sbc_disk_info_restore
#
END_OF_DATA
cat >sbc_umount_drives <<END_OF_DATA
#!/bin/sh
#
#  Unmount disk drives  -- created by sbc_disk_info_restore
#
END_OF_DATA
echo "#" >>sbc_mount_drives
echo "#" >>sbc_umount_drives
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt ext2 \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.ext2.sdi >/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt ext3 \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.ext3.sdi >>/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt reiserfs \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.reiserfs.sdi >>/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/sbc_mkmnt xfs \1 \/mnt\/disk\2/p' $tmp_info_dir/mount.xfs.sdi >>/tmp/1$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.ext2.sdi >/tmp/2$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.ext3.sdi >>/tmp/2$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.reiserfs.sdi >>/tmp/2$$
sed -n 's/\(^.*\)\ on\ \(.*\)\ type.*$/umount -v \/mnt\/disk\2/p' $tmp_info_dir/mount.xfs.sdi >>/tmp/2$$
# sort so that root is mounted first and unmounted last
sort -k 4 </tmp/1$$ >>sbc_mount_drives
sort -r -k 3 </tmp/2$$ >>sbc_umount_drives
rm -f /tmp/1$$ /tmp/2$$

chmod 755 sbc_mount_drives sbc_umount_drives
./sbc_mount_drives

#create the proc directory
mkdir -v -p /mnt/disk/proc
mkdir -v -p /mnt/disk/sys

exit 0


