#!/bin/sh

set -e

export TEXTDOMAIN=kcm_mobile_update_scripts
. gettext.sh

path_to_img=$1
out_script_path=/tmp
path_to_mount=~/update/mnt
mkdir -p $path_to_mount

sector_size=$(LANG="" fdisk -l $path_to_img | grep "Sector size (logical/physical): " | cut -d' ' -f4)
start_block=$(LANG="" fdisk -l $path_to_img -o Start,Type | grep "Linux filesystem" | cut -d' ' -f1)

if [ -z $sector_size ] || [ -z $start_block ]; then
	echo $(gettext "error in calculating the sector size and start block") >&2
	exit 1
else
	offset=$(echo "$sector_size * $start_block" | bc)
	echo $(eval_gettext "Mount offset: \$offset. Mounting...")
	mount -o loop,offset=$offset $path_to_img $path_to_mount
	echo $(gettext "Copy files...")
	cp $path_to_mount/sbin/astra-mobile-system-update $out_script_path
	echo $(gettext "Unmount img")
	umount $path_to_mount
	echo $(gettext "The update is ready to install. after 20 seconds, the computer will restart to install updates. \
	When updating, do not press any buttons, the update lasts about 10 minutes")
	( sleep 20 && $out_script_path/astra-mobile-system-update && sudo init 6 ) &
fi
