#!/bin/bash
# SoloConLinux. 
# Partitions of a file and calculation of the offsets of its partitions
if [ "$1" == "" ] ; then
	# Show Help
	echo
	echo "--------------------------------------------------------------"
	echo USAGE 1:
	echo "  $0 file-image.img"
	echo "  Return the list of all existing partitions of the file-image"
	echo "--------------------------------------------------------------"
	echo USAGE 2:
	echo "  $0 file-image.img NUMBER"
	echo "  Returns the offset number of the file-image partitions"
	echo "  to format or mount it."
	echo "--------------------------------------------------------------"
	echo
	exit 
fi
SizeSECTOR=$(fdisk -l $1 |grep '('|head -1|awk -F '/' '{print $3}'|awk '{print $1}')
if [ "$2" == "" ] ; then
	# Show file-image Partitions
	fdisk -l $1 |grep "^$1"
  exit
else
	PARTITION=$(fdisk -l $1 |grep "^$1$2" | awk '{print "Partition: "$1" - Size: "$5}')
  if [ "$PARTITION" == "" ] ; then
		echo "The indicated partition does not exist"
    exit
  fi
	OffsetINI=$(($(fdisk -l $1 |grep "^$1$2"|awk '{print $2}') * $SizeSECTOR))
	echo
	echo "============================================"
	echo $PARTITION
	echo $OffsetINI" (Start Offset)" 
	echo "============================================"
	echo
	echo "------------------------------------------------------------------"
	echo "EXTRA HELP"
	echo "Format"
	echo "  You can format the partition $1$2 with the command:"
	echo "  mkfs.ext4 $1 -E offset=$OffsetINI"
	echo "Mount"
	echo "  You can mount the partition $1$2with the command:"
	echo "  mount -o offset=$OffsetINI $1 /mount/point/path/" 
	echo "------------------------------------------------------------------"
	echo 
fi

