#!/bin/ash
# Copyright (c) 2013 - OpenSLX GmbH
#
# This program is free software distributed under the GPL version 2.
# See http://openslx.org/COPYING
#
# If you have any feedback please consult http://openslx.org/feedback and
# send your feedback to feedback@openslx.org
#
# General information about OpenSLX can be found under http://openslx.org
#
# Local hard disk autodetection script for OpenSLX linux stateless clients,
# detecting swap and special partitions

#############################################################################

perror () {
  echo "ERROR: $@" 1>&2
  if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then
    dialog --title "ERROR" --stdout --msgbox "$@" 15 60
  else
    dialog --title "ERROR" --no-cancel --stdout --pause "$@\n\nReboot in:" 15 60 600
    reboot
  fi
  exit 1
}

pwarning  () {
  echo "WARNING: $@" 1>&2
  if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then
    dialog --title "WARNING" --stdout --msgbox "$@" 15 60
  else
    dialog --title "WARNING" --stdout --infobox "$@" 15 60
    sleep 5
  fi
}

. /opt/openslx/config || perror "Could not source config."

#set -x
#exec > /log 2>&1


############### format aux code ###############

# format all partitions in disk $1
# param $1 is the disk to be formatted
format_format_disk () {
  if [ -z "$SLX_PARTITION_FS" ]; then
    pwarning "SLX_PARTITION_FS not set in config. Using default (ext4)"
    SLX_PARTITION_FS="ext4"
  fi
  format_reread_disk $1
  format_format_partitions $1 $SLX_PARTITION_FS
}

# reread partition table, so new partitions can be formatted
# param $1 is the disk to be read
format_reread_disk () {
  sfdisk -R /dev/$1 > /dev/null 2>&1 || perror "Error reloading partition table"
  format_partitions=$(cat /proc/partitions | grep -E "*$1[0-9]$" | tr -s ' ' | cut -d ' ' -f5)
  echo -e "Partitions from disk $1:\n$format_partitions"
}

# iterate through partitions and call format_partition
# param $1 is the disk
# param $2 is the desired filesystem for the partitions
format_format_partitions () {
  for part in $format_partitions; do
    local part_number=$(echo "$part" | sed 's/.*[a-zA-Z]\(.*\)/\1/')
    format_format_partition $1 $part_number $2
  done
}


# format partition, so the filesystem is rebuilt
# param $1 is the disk where the partition is located
# param $2 is the partition to be formatted number
# param $3 is the desired filesystem for the partitions
format_format_partition () {
  local partition="/dev/$1$2"
  umount -f $partition  > /dev/null 2>&1
  echo "Formatting $partition type=$3"
  mkfs.$3 $partition > /dev/null 2>&1 || pwarning "Could not format $partition. If you are using MSDOS it might be the extended partition."
}


#--------------------------------------------------------------------------------------
#Gathering partition information

# BLOCKSIZE=$(sfdisk -l /dev/$SLX_CHOOSEN_DISK | grep -o -e "blocks of [0-9]* bytes" | cut -d ' ' -f3)
BLOCKSIZE=1024

# pick the size of a sector (usually 512 bytes)
# SECTORSIZE=$(sfdisk /dev/$SLX_CHOOSEN_DISK -l -u S 2> /dev/null | grep Units | cut -d ' ' -f5)
SECTORSIZE=512

# Partition that should be set as extended partition (only partitions 2-4 can be extended partitions)
EXTENDED_PARTITION_NR=4

PARTITIONSPATH="/proc/partitions"
# picking disk that will be used
DISKS=$(cat $PARTITIONSPATH | tr -s ' ' | cut -d ' ' -f5 | grep -e "[a-z]$")
if [ -z "$DISKS" ]; then
  perror "Can't find an hard disk."
fi
#--------------------------------------------------------------------------------------

define_partition_table() {
  if [ -z "$SLX_PARTITION_TABLE" ]; then
    SLX_PARTITION_TABLE='
    44,10G,/tmp
    45,10G,/var/scratch
    82,4G'
    pwarning "You didn't define a partition table in config. Using default partition table:\n$SLX_PARTITION_TABLE"
  fi
}

size_conv() {
  [ $(echo "$1<1024" | bc) -eq 1 ] && { echo "$1 B"; exit 0; }
  local result="$(echo $1 | awk '{ sum=$1; hum[1024^3]="GB"; hum[1024^2]="MB"; hum[1024]="KB";
                    for (x=1024^3; x>=1024; x/=1024){
                      if (sum>=x) { printf "%.2f %s\n",sum/x,hum[x]; break }
                    }
                 }')"
  echo $result
}

choose_disk() {
  local disk_number=$(echo $DISKS | tr ' ' \\n           | wc -l)
  if [ "$SLX_AUTOMATIC_PARTITIONING" = "yes" ]; then
    if [ -n "$SLX_CHOOSEN_DISK" ]; then
      echo $DISKS | grep -wq "$SLX_CHOOSEN_DISK" || perror "ERROR: Automatic partitioning enabled, but in config specified $SLX_CHOOSEN_DISK was not found." 
    elif [ "$disk_number" -eq 1 ]; then
      SLX_CHOOSEN_DISK=$DISKS
      pwarning "Automatic partitioning enabled, but SLX_CHOOSEN_DISK not specified in config. Choosing only existing disk: $DISKS for partitioning."
    else
      perror "Automatic partitioning enabled, but SLX_CHOOSEN_DISK not specified in config. More than one disk exists, please specify disk in config and run again."
    fi
  else
    local dialog_string="Existing disks with partition tables:\n\n"
    local disksize=""
    local freespace=0
    local parts=""
    local partsize=0
    local used=0
    local options=""

    for disk in $DISKS; do
      disksize=$(echo "$(cat $PARTITIONSPATH | grep -e $disk$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE" | bc)
      parts=$(cat $PARTITIONSPATH | grep -e $disk[0-9] | tr -s ' ' | cut -d ' ' -f5)
      dialog_string="${dialog_string}$disk     $(size_conv $disksize)\n"
      used=0
      options="${options}$disk $disk "

      for part in $parts; do
        partsize=$(echo "$(cat $PARTITIONSPATH | grep -e $part$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE" | bc)
        used=$(echo "$used+$partsize" | bc)
        dialog_string="${dialog_string} $part   $(size_conv $partsize)\n"
      done

      dialog_string=$dialog_string"--------------------------------\n"
      freespace=$(echo "$disksize-$used" | bc)
      dialog_string="${dialog_string}Used    $(size_conv $used)\n"
      dialog_string="${dialog_string}Free    $(size_conv $freespace)\n\n"
    done

    if [ $(echo $DISKS | tr ' ' \\n           | wc -l) -gt 0 ]; then
      SLX_CHOOSEN_DISK=$(echo $options | xargs dialog --title "Choose a disk to partition:" --no-tags --cr-wrap --no-collapse --stdout --menu "${dialog_string}" 0 0 0)
      if [ -z $SLX_CHOOSEN_DISK ]; then
        pwarning "Partitioning aborted by user."
        exit 1
      fi
    else
      perror "Can't find a hard disk."
    fi
  fi
  CHOOSEN_DISK_SIZE=$(echo "$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK$ | tr -s ' ' | cut -d ' ' -f4)*$BLOCKSIZE" | bc)
  CHOOSEN_DISK_PARTS=$(cat $PARTITIONSPATH | grep -e $SLX_CHOOSEN_DISK[0-9] | tr -s ' ' | cut -d ' ' -f5)
}

check_disk_size() {
  # check if the choosen disk is greater than the new partition table size.
  local part_number=$(echo $SLX_PARTITION_TABLE | wc -w)
  local part_space=$(echo $SLX_PARTITION_TABLE | grep -oE '[0-9]+G' | awk '{ SUM += $0 } END { print SUM }')
  NEEDED_DISK_SPACE=$(echo "$part_space*1024*1024*1024+$part_number*1024*1024" | bc)

  if [ $(echo "$CHOOSEN_DISK_SIZE<$NEEDED_DISK_SPACE" | bc) -eq 1 ]; then
    perror "Insufficient space on disk /dev/$SLX_CHOOSEN_DISK\n  DISK SIZE: $(size_conv $CHOOSEN_DISK_SIZE)\n  REQUIRED SIZE: $(size_conv $NEEDED_DISK_SPACE)"
  fi
}

select_partition_type() {
  # choosing the partition type (MSDOS or GPT) if it wasn't set in config file
  if [ -z "$SLX_PARTITION_TYPE" ]; then
    if [ "$SLX_AUTOMATIC_PARTITIONING" = "yes" ]; then
      pwarning "SLX_PARTITION_TYPE not defined in config, using default: msdos"
      SLX_PARTITION_TYPE=msdos
    else
      SLX_PARTITION_TYPE=$(dialog --no-tags --title "Choose a partition type:" --menu --stdout "Partitions types:" 0 0 0 "msdos" " MSDOS " "GPT" " GPT ")
      if [ -z $SLX_PARTITION_TYPE ]; then
        pwarning "Partitioning aborted by user."
        exit 1
      fi
    fi
  fi
}

confirm_partitioning() {
  local dialog_string="New partition table after partitioning:\n\n"
  local counter=1
  local id=0
  local size=0
  local mountpoint=""
  local bootable=0
  local part_type="Primary"
  for part in $SLX_PARTITION_TABLE; do
    IFS=,
    set $part

    id=$1
    shift
    size=${1%G}
    shift
    [ -n $1 ] && mountpoint=$1
    shift
    case "$*" in
      *bootable*) bootable=1 ;;
               *) bootable=0 ;;
    esac

    [ "$id" = "82" ] && mountpoint="swap"

    if [ $SLX_PARTITION_TYPE = 'GPT' ]; then
      #update dialog status
      dialog_string="${dialog_string}${mountpoint} (/dev/"${SLX_CHOOSEN_DISK}${counter}")\n"
      if [ "${id}" = "82" ]; then
        dialog_string="${dialog_string}   GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
      elif [ "${id}" = "83" ]; then
        dialog_string="${dialog_string}   GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
      else
        dialog_string="${dialog_string}   GUID: "$id"000000-0000-0000-0000-000000000000\n"
      fi
      dialog_string="${dialog_string}   Size: "$size" GB\n"
    else
      if [ $counter -eq $EXTENDED_PARTITION_NR ]; then
        part_type="Logical"
        counter=$(($counter+1))
      fi
      #update dialog
      dialog_string="${dialog_string}$mountpoint (/dev/${SLX_CHOOSEN_DISK}$counter)\n"
      dialog_string="${dialog_string}   Type: $part_type\n"
      dialog_string="${dialog_string}   ID: $id\n"
      dialog_string="${dialog_string}   Size: $size GB\n"
    fi

    counter=$(($counter+1))
  done
  unset IFS

  dialog_string="${dialog_string}\nAll existing partitions on /dev/$SLX_CHOOSEN_DISK will be deleted, continue?"

  # asking confirmation to create the partitions
  dialog --title "WARNING" --stdout --defaultno --yesno "$dialog_string" 0 0
  if [ $? -eq 1 ]; then
    pwarning "Partitioning aborted by user."
    exit 1
  fi
}

# function to create gpt type partition tables (uses sgdisk)
partition_disk_gpt() {
  # delete partition table
  sgdisk -Z /dev/$SLX_CHOOSEN_DISK > /dev/null 2>&1 || perror "Error erasing old partition table"

  #set dialog
  dialog_string="Partitions created:\n\n"
  echo "0" | dialog --title "Partitioner" --stdout --gauge "$dialog_string" 0 0 0

  # loop that will create each GPT partition, change GUIDS, change names, and set bootable flags.
  local counter=1
  local part_number=$(echo $SLX_PARTITION_TABLE | wc -w)
  local id=0
  local size=0
  local mountpoint=""
  local bootable=0
  for part in $SLX_PARTITION_TABLE; do
    IFS=,
    set $part

    id=$1
    shift
    size=${1%G}
    shift
    [ -n $1 ] && mountpoint=$1
    shift
    case "$*" in
      *bootable*) bootable=1 ;;
               *) bootable=0 ;;
    esac

    #create boot partition and set boot flag
    if [ "$bootable" -eq 1 ]; then
      sgdisk /dev/${SLX_CHOOSEN_DISK} -n ${counter}:0:+1M 1>&2 || perror "Error setting size of GPT partition ${SLX_CHOOSEN_DISK}${counter}" 
      sgdisk /dev/${SLX_CHOOSEN_DISK} -t ${counter}:21686148-6449-6E6F-744E-656564454649 1>&2 || perror "Error setting id ${id} of GPT partition ${SLX_CHOOSEN_DISK}${counter}"
      sgdisk /dev/${SLX_CHOOSEN_DISK} -A ${counter}:set:2 1>&2 || perror "Error setting boot flag for GPT partition ${SLX_CHOOSEN_DISK}${counter}"
      dialog_string="${dialog_string}Special bootloader partition (/dev/${SLX_CHOOSEN_DISK}${counter})\n"
      dialog_string="${dialog_string}   GUID: 21686148-6449-6E6F-744E-656564454649\n"
      dialog_string="${dialog_string}   Size: 1 MB\n"
      counter=$(($counter+1))
      part_number=$(($part_number+1))
    fi

    #set size of partition
    sgdisk /dev/${SLX_CHOOSEN_DISK} -n ${counter}:0:+${size}G 1>&2 || perror "Error setting size of GPT partition ${SLX_CHOOSEN_DISK}${counter}"

    #set id of partition
    if [ "${id}" = "82" ] || [ "${id}" = "83" ]; then
      sgdisk /dev/${SLX_CHOOSEN_DISK} -t ${counter}:${id}00 1>&2 || perror "Error setting id ${id} of GPT partition ${SLX_CHOOSEN_DISK}${counter}"
    else
      sgdisk /dev/${SLX_CHOOSEN_DISK} -t ${counter}:${id}000000-0000-0000-0000-000000000000 -c ${counter}:\"${mountpoint}\" 1>&2 || perror "Error setting id ${id} of GPT partition ${SLX_CHOOSEN_DISK}${counter}"
    fi

    #update dialog status
    [ "$id" = "82" ] && mountpoint="swap"
    dialog_string="${dialog_string}${mountpoint} (/dev/${SLX_CHOOSEN_DISK}${counter})\n"
    if [ "${id}" = "82" ]; then
      dialog_string="${dialog_string}   GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
    elif [ "${id}" = "83" ]; then
      dialog_string="${dialog_string}   GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
    else
      dialog_string="${dialog_string}   GUID: ${id}000000-0000-0000-0000-000000000000\n"
    fi
    dialog_string="${dialog_string}   Size: ${size} GB\n"
    echo $(echo "(100/$part_number)*$counter" | bc) | dialog --title "Partitioner" --stdout --gauge "${dialog_string}" 0 0 0

    counter=$(($counter+1))
  done
  unset IFS

  format_format_disk $SLX_CHOOSEN_DISK

  if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then
    dialog --title "Partitioner" --stdout --msgbox "$dialog_string" 0 0
  fi
}

#function to create msdos type partition tables (uses sfdisk)
partition_disk_msdos(){
  # delete partition table
  sgdisk -Z /dev/$SLX_CHOOSEN_DISK > /dev/null 2>&1 || perror "Error erasing old partition table"

  #set dialog
  dialog_string="Partitions created:\n\n"

  # constructing the sfdisk input file
  echo "unit: sectors"> /tmp/partitiontable.tmp

  # loop that will create each GPT partition, change GUIDS, change names, and set bootable flags.
  local counter=1
  local id=0
  local size=0
  local mountpoint=""
  local bootable=0
  local part_type="Primary"
  local start=$(echo "1*1024*1024/$SECTORSIZE" | bc)
  for part in $SLX_PARTITION_TABLE; do
    IFS=,
    set $part

    id=$1
    shift
    size=${1%G}
    shift
    [ -n $1 ] && mountpoint=$1
    shift
    case "$*" in
      *bootable*) bootable=1 ;;
               *) bootable=0 ;;
    esac

    if [ $counter -eq $EXTENDED_PARTITION_NR ]; then
      echo "/dev/${SLX_CHOOSEN_DISK}$counter : start= $start, size= $(echo "${CHOOSEN_DISK_SIZE}/$SECTORSIZE-1" | bc), Id= 5" >> /tmp/partitiontable.tmp
      part_type="Logical"
      start=$(echo "$start+1*1024*1024/$SECTORSIZE" | bc)
      counter=$(($counter+1))
    fi

    size_bytes=$(echo "${size}*1024*1024*1024/$SECTORSIZE" | bc)

    if [ "$bootable" -eq 1 ]; then
       echo "/dev/${SLX_CHOOSEN_DISK}$counter : start= $start, size= $size_bytes, Id= $id, bootable " >> /tmp/partitiontable.tmp
    else
       echo "/dev/${SLX_CHOOSEN_DISK}$counter : start= $start, size= $size_bytes, Id= $id" >> /tmp/partitiontable.tmp
    fi

    start=$(echo "$start+$size_bytes+1*1024*1024/$SECTORSIZE" |bc)

    #update dialog
    [ "$id" = "82" ] && mountpoint="swap"
    dialog_string="${dialog_string}$mountpoint (/dev/${SLX_CHOOSEN_DISK}$counter)\n"
    dialog_string="${dialog_string}   Type: $part_type\n"
    dialog_string="${dialog_string}   ID: $id\n"
    dialog_string="${dialog_string}   Size: $size GB\n"

    counter=$(($counter+1))
  done
  unset IFS

  sfdisk -q --no-reread -f /dev/${SLX_CHOOSEN_DISK} < /tmp/partitiontable.tmp 1>&2 || return 0
  # rm -f /tmp/partitiontable.tmp
  
  format_format_disk $SLX_CHOOSEN_DISK

  if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then
    dialog --title "Partitioner" --stdout --msgbox "$dialog_string" 0 0
  else
    dialog --title "Partitioner" --stdout --infobox "$dialog_string" 0 0
    sleep 5
  fi
}


define_partition_table
choose_disk
select_partition_type
if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then
  confirm_partitioning
fi
check_disk_size

if [ $SLX_PARTITION_TYPE = 'GPT' ]; then
  partition_disk_gpt
else
  partition_disk_msdos
fi

if [ "$SLX_AUTOMATIC_PARTITIONING" != "yes" ]; then
  #reboot prompt
  dialog --title "reboot computer" --stdout --yes-label "Reboot" --no-label "Continue" --yesno "For changes to take effect you have to reboot your machine." 5 65
  [ $? -eq 0 ] && reboot || exit 0
else
  reboot
fi

####################### CODE FOR THE PRESERVATION OF EXISTING PARTITIONS (EXPERIMENTAL) #########################
#elif [ $(sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $SLX_CHOOSEN_DISK'1' | tr -s ' ' | cut -d ' ' -f7 | cut -d '=' -f2) = 'ee' ]; then
## finding existent partitions in GPT
#  echo 'GPT'
#  NONEXISTENT_PARTITIONS=0
#  for (( i = 1; i < $COUNTER; i++ )); do
#    if [ $i -ne 4 ]; then
#			for PART in $PARTS; do
#        if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ]; then
#          sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep "0657FD6D-A4AB-43C4-84E5-0933C84B4F4F" > /dev/null
#        elif [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then
#          sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep "0FC63DAF-8483-4772-8E79-3D69D8477DE4" > /dev/null
#        else
#          sgdisk /dev/$SLX_CHOOSEN_DISK -i ${PART:3} 2> /dev/null | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}"000000-0000-0000-0000-000000000000" > /dev/null
#        fi
#        if [ $? -eq 0 ]; then
#          PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART
#          NEEDED_SPACE=$(($NEEDED_SPACE-${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
#          break
#        fi
#  		done
#  		if [[ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = "0" ]]; then
#	    	NONEXISTENT_PARTITIONS=$(($NONEXISTENT_PARTITIONS+1))
#  		fi
#    fi
#  done
#  # checking if there's sufficient space for the new partitions.
#  if [[ $FREESPACE -lt $NEEDED_SPACE ]]; then
#    dialog --msgbox "ERROR: Insufficient free space in disk /dev/$SLX_CHOOSEN_DISK." 6 40
#    exit 1
#  fi
#  if [[ $NONEXISTENT_PARTITIONS -eq 0 ]]; then
#  	dialog_string="All your requested partitions already exists.\n\n"
#  	dialog_string=$dialog_string"Partition    ID\n"
#	  for (( i = 1; i < $COUNTER; i++ )); do
#	  	if [[ $i -ne 4 ]]; then
#	  		dialog_string=$dialog_string"${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]}         ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}\n"
#	  	fi
#	  done
#	else
#	  dialog_string="Creating partitions...\n\n"
#	  echo "0" | dialog --gauge "$dialog_string" 0 0 0
#    CREATED=0
#	  for (( i = 1; i < $COUNTER; i++ )); do
#	    j=1
#	    if [[ $i -ne 4 ]]; then
#		    if [[ "${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]}" = "0" ]]; then
#		      sgdisk /dev/$SLX_CHOOSEN_DISK -n $j:0:+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}G 2> /dev/null > /dev/null
#		      while [[ $? -eq 4 ]]; do
#		        j=$(($j+1))
#		        sgdisk /dev/$SLX_CHOOSEN_DISK -n $j:0:+${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}G 2> /dev/null > /dev/null
#		      done
#		      PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$SLX_CHOOSEN_DISK$j
#		      SIZE=$((${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
#		      ID=${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}
#		      MOUNTPOINT=${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]}
#		      dialog_string=$dialog_string$MOUNTPOINT" (/dev/"${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]}")\n"
#		      if [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "82" ]; then
#		        dialog_string=$dialog_string"   GUID: 0657FD6D-A4AB-43C4-84E5-0933C84B4F4F\n"
#		      elif [ "${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]}" = "83" ]; then
#		        dialog_string=$dialog_string"   GUID: 0FC63DAF-8483-4772-8E79-3D69D8477DE4\n"
#		      else
#		      	sgdisk /dev/$SLX_CHOOSEN_DISK -t $j:$ID"000000-0000-0000-0000-000000000000" -c $j:$MOUNTPOINT >/dev/null 2>/dev/null
#		        dialog_string=$dialog_string"   GUID: "$ID"000000-0000-0000-0000-000000000000\n"
#		      fi
#		      dialog_string=$dialog_string"   Size: "$SIZE" Bytes ("${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}" GB)\n"
#		      CREATED=$(($CREATED+1))
#	      	echo $(( $CREATED*100/($NONEXISTENT_PARTITIONS) )) | dialog --gauge "$dialog_string" 0 0 0
#		    fi
#	      if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/bootable"]} -eq 1 ]; then
#	      	sgdisk /dev/$SLX_CHOOSEN_DISK -A $j:set:2 >/dev/null 2>/dev/null
#	      fi
#	    fi
#	  done
#  fi
#else
## finding existent partitions in MS-DOS
#  echo 'MSDOS'
#  for PART in $PARTS; do
#    for (( i = 1; i < $COUNTER; i++ )); do
#      if [ $i -ne 4 ]; then
#        if [ ${PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]} = 0 ]; then
#          sfdisk -d /dev/$SLX_CHOOSEN_DISK 2> /dev/null | grep $PART | tr -s ' ' | cut -d ' ' -f7 | grep ${PARTTBL["$SLX_CHOOSEN_DISK$i/id"]} > /dev/null
#          if [ $? -eq 0 ]; then
#            echo "the partition $PART is the ${PARTTBL["$SLX_CHOOSEN_DISK$i/mountpoint"]} partition."
#            PARTTBL["$SLX_CHOOSEN_DISK$i/exists"]=$PART
#            NEEDED_SPACE=$(($NEEDED_SPACE-${PARTTBL["$SLX_CHOOSEN_DISK$i/size"]}*1024*1024*1024))
#            break
#          fi
#        fi
#      fi
#    done
#  done
#  echo "FREESPACE="$FREESPACE
#  echo "NEEDED_SPACE="$NEEDED_SPACE
#  if [[ $FREESPACE -lt $NEEDED_SPACE ]]; then
#    dialog --msgbox "ERROR: Insufficient free space in disk /dev/$SLX_CHOOSEN_DISK." 6 40
#    exit 1
#  fi
#fi
