#!/bin/bash
#
# rpi-image-tool
#
# Copyright 2017, Mike Ray, <mike.ray@btinternet.com>
# Copyright 2018, F123 Consulting, <information@f123.org>
#
# This 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, or (at your option) any later
# version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; see the file COPYING.  If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
#--code--

export TEXTDOMAIN=rpi-image-tool
#export TEXTDOMAINDIR=$PWD/locale
export TEXTDOMAINDIR=./locale

. gettext.sh

. ./functions


usage() {
	USAGE=$(gettext "tool_usage")
	echo -e "${USAGE}"
}


#-- Main code

if ! check_root ; then
	PROGNAME=$0
	echo $(eval_gettext "Script must be run as root, try: sudo \$PROGNAME") ; echo
	exit 1
fi


WORKDIR=
IMAGEFILE=
FUNCTION=
SPLIT=64
BLOCKSIZE=1M
BLOCKCOUNT=

BOOTLABEL=RPi-Boot
ROOTLABEL=RPi-Root

BOOTMOUNTPOINT=boot
ROOTMOUNTPOINT=root


while getopts ':C:D:M:N:b:c:hr:s:w:z:' flag ; do
	case $flag in
	h)
		usage
		exit 0
	;;
C)
		FUNCTION=cleanup
		WORKDIR=${OPTARG}
		break 2
	;;
	D)
		FUNCTION=mountdevice
		DEVICE=${OPTARG}
	;;
	M)
		FUNCTION=mountimage
		IMAGEFILE=${OPTARG}
	;;
	N)
		FUNCTION=new
		IMAGEFILE=${OPTARG}
	;;
	c)
		BLOCKCOUNT=${OPTARG}
		validate_blockcount ${BLOCKCOUNT}
	;;
	s)
		SPLIT=${OPTARG}
		validate_split ${SPLIT}
	;;
	b)
		BOOTLABEL="${OPTARG}"
	;;
	r)
		ROOTLABEL="${OPTARG}"
	;;
	w)
		WORKDIR=${OPTARG}
	;;
	z)
		BLOCKSIZE=${OPTARG}
		validate_blocksize ${BLOCKSIZE}
	;;
	(?)
		echo $(gettext "Invalid option") >&2
		echo >&2
		usage
		exit 1
	;;
	:)
		echo $(eval_gettext "Option - \$OPTARG requires an argument.") >&2
		echo >&2
		usage
		exit 1
	;;
	esac
done

[ -z ${WORKDIR} ] && { echo $(gettext "Work directory not set") ; echo ; usage; exit 1 ; }

if [ ${FUNCTION} = 'mountimage' ] || [ $FUNCTION = 'new' ]; then
	[ -z "${IMAGEFILE}" ] && { echo $(gettext "Image file name not set") ; echo ; usage; exit 1 ; }
fi

if [ ${FUNCTION} = 'mountdevice' ]; then
	[ -z "${DEVICE}" ] && { echo $(gettext "Device name not set") ; echo ; usage ; exit 1 ; }
fi

if [ ${FUNCTION} = 'new' ]; then
	[ -z ${BLOCKCOUNT} ] && { echo $(gettext "Block count not set") ; echo ; usage ; exit 1 ; }
	[ -z ${BLOCKSIZE} ] && { echo $(gettext "Block size not set") ; echo ; usage ; exit 1 ; }
	[ -z ${SPLIT} ] && { echo $(gettext "Split size not set") ; echo ; usage ; exit 1 ; }
fi

case ${FUNCTION} in
	cleanup)
		clean_up ${WORKDIR}
# ${BOOTMP} ${ROOTMP}
	;;
	mountimage)
		mount_image ${WORKDIR} ${IMAGEFILE} ${BOOTMOUNTPOINT} ${ROOTMOUNTPOINT}
	;;
	mountdevice)
		mount_device ${WORKDIR} ${DEVICE} ${BOOTMOUNTPOINT} ${ROOTMOUNTPOINT}
	;;
	new)
		new_image ${WORKDIR} ${IMAGEFILE} ${BOOTLABEL} ${ROOTLABEL} ${BOOTMOUNTPOINT} ${ROOTMOUNTPOINT} ${BLOCKCOUNT} ${BLOCKSIZE} ${SPLIT}
	;;
esac

exit 0


