#!/bin/sh
#
# CLI utility to create a persistence file easily.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU  General Public License as published by
# the Free Software Foundation; either version 3 of the License or (at
# your option) any later version.
# Please take a look at http://www.gnu.org/copyleft/gpl.htm
#
# Written by: Cyrille Pontvieux <jrd~at~enialis~dot~net>
# Modified by: Pierrick Le Brun <akuna~at~free~dot~fr>

# Gettext internationalization
export TEXTDOMAIN="persistence-wizard"
export TEXTDOMAINDIR="/usr/share/locale"
. gettext.sh

version=1.2
NAME="$1"
SIZE="$2"
FS="$3"
SCRIPT_NAME="$(basename $0)"

if [ `id -u` != "0" ]; then
	echo "`eval_gettext 'ERROR:'` `eval_gettext 'You need to be root to run $SCRIPT_NAME'`"
	exit 1
fi

if [ -z "$3" ]; then
	echo "Usage: $SCRIPT_NAME `eval_gettext 'NAME SIZE_IN_MEGABYTES FS_TYPE'`"
	echo ""
	echo "`eval_gettext 'This will create a persistent file named NAME \
with a size (in MB) defined by SIZE_IN_MEGABYTES \
and with a file system defined by FS_TYPE.'`" 
	echo "" 	
	echo "`eval_gettext 'For example:'` $SCRIPT_NAME salixlive 500 ext2" 
	echo "" 	
	echo "`eval_gettext 'Supported file system format are:'`" 
	echo "xfs, ext2, ext3, ext4, reiserfs, vfat, ntfs." 	
	echo "" 
	echo "`eval_gettext 'You must then copy the generated persistent file  \
on /live/media on Salix Live USB key or else on \
a hard disk drive if the persistence will be used with a Salix LiveCD.'`"
	echo ""
	echo "Copyright Cyrille Pontvieux - jrd@salixos.org"
	echo "`eval_gettext 'Released under GPL v3 or (at your option, any later version)'`"
	echo "version = $version"
	exit 1
fi

unset FS_SUPPORT
for i in xfs ext2 ext3 ext4 reiserfs vfat ntfs; do
	if [ "$FS" = "$i" ]; then
		FS_SUPPORT='OK'
	fi 	
done

if [ -z "$FS_SUPPORT" ]; then
	echo "`eval_gettext 'ERROR:'` `eval_gettext 'The file system you specified is not supported by $SCRIPT_NAME'`"
	exit 1
fi


if [ $SIZE -gt 0 ]; then 
	INTEGER='OK'
fi
if [ -z "$INTEGER" ]; then
	echo "`eval_gettext 'ERROR:'` `eval_gettext 'The size you specified is not correct, it should be a positive integer.'`"
	exit 1
fi

FREE=$('df' -k .|tail -n1|awk '{print $4}')
AFTER=$(( $FREE - 1024 * $SIZE ))
if [ $AFTER -gt 0 ]; then
	dd if=/dev/zero of="$NAME" bs=1M count=$SIZE
	if [ "$FS" = "xfs" ]; then  
		mkfs.xfs -f -L "persistent" "$NAME" && CHECK='OK'
	elif [[ "$FS" = "ext2" ]]; then
		mkfs.ext2 -F -m 0 -L "persistent" "$NAME" && CHECK='OK'
	elif [[ "$FS" = "ext3" ]]; then
		mkfs.ext3 -F -m 0 -L "persistent" "$NAME" && CHECK='OK'
	elif [[ "$FS" = "ext4" ]]; then
		mkfs.ext4 -F -m 0 -L "persistent" "$NAME" && CHECK='OK'
	elif [[ "$FS" = "reiserfs" ]]; then
		mkfs.reiserfs -f -f -l "persistent" "$NAME" && CHECK='OK'
	elif [[ "$FS" = "vfat" ]]; then
		mkfs.vfat -F 32 -I -R 2 -n "persistent" "$NAME" && CHECK='OK'
	elif [[ "$FS" = "ntfs" ]]; then
		mkfs.ntfs -f -I -F -L "persistent" "$NAME" && CHECK='OK'
	fi
	if [ -n "$CHECK" ]; then
		echo ""
		echo "`eval_gettext 'The persistent file $NAME is ready.'`"
	else
		echo "`eval_gettext 'ERROR:'` `eval_gettext '$SCRIPT_NAME was not able to create the persistent file $NAME'`"
		exit 1
	fi
else
  echo "`eval_gettext 'ERROR:'` `eval_gettext 'There is not enough free space left on your device \
for creating the persistent file $NAME'`"
  exit 1
fi
