#!/bin/bash
backtitle="Armbian EMMC backup script, http://www.armbian.com | Author: nopnop2002"
title="Backup from EMMC to Image File"
logfile="/tmp/dd.log"
imgfile="/var/images/emmc.img.7z"
sleeptime=10
debugout="/tmp/debug.log"

function DebugEcho() {
  if [ $1 = '-n' ]; then
    echo -n $2 >> ${debugout}
  else
    echo $1 >> ${debugout}
  fi
}

# Get copied size
function GetCopied() {
   pid=`ps -ef | grep  "dd if=" | grep -v grep | awk  '{print $2}'`
#   echo -n "pid="
#   echo $pid
   kill -USR1 $pid
#   tail -1 /tmp/dd.log
#   echo -n "/tmp/dd.log="
   if [ -s ${logfile} ]; then
#     tail -1 /tmp/dd.log | awk '{print $1}'
#     copied=`tail -1 /tmp/dd.log | awk '{print $1}'`
     copied=`tail -1 ${logfile} | awk '{print $1}'`
     copied=`expr ${copied} \* 100`
   else
     copied=0
   fi
#   DebugEcho -n "copied="
#   DebugEcho $copied
}

# Get progress
function GetProgress() {
   DebugEcho -n "copied="
   DebugEcho $copied
   DebugEcho -n "emmcbyte="
   DebugEcho $emmcbyte
   progress=`expr ${copied} / ${emmcbyte}`
   if [ ${progress} -gt 100 ]; then
     progress=100
   fi
}


# Check root user
if [ "$UID" -ne 0 ]; then
  dialog --title "$title" --backtitle "$backtitle" \
  --infobox "\nMust be root" 5 60
  exit 1
fi

# Check p7zip install
p7zip=`dpkg -l | grep p7zip | wc -l`
if [ $p7zip == 0 ]; then
  dialog --title "$title" --backtitle "$backtitle" \
  --infobox "\np7zip not installed" 5 60
  exit 1
fi

# Check EMMC partition
emmc=`cat /proc/partitions | grep -c mmcblk1`
#echo -n "emmc="
#echo $emmc
if [ $emmc == 0 ]; then
  dialog --title "$title" --backtitle "$backtitle" \
  --infobox "\n/dev/mmcblk1 not found" 5 60
  exit 1
fi

# Check image file exist
if [ -f ${imgfile} ]; then
  dialog --title "$title" --backtitle "$backtitle" \
  --yes-label "Ok" --no-label "No" --yesno \
  "\n${imgfile} is already exist. purge ok?" 7 60
  if [ $? -eq 1 ]; then exit 1; fi
  rm ${imgfile}
fi

# Get EMMC size
#cat /proc/partitions | grep mmcblk1 | head -1 | awk '{print $3}'
emmcbyte=`cat /proc/partitions | grep mmcblk1 | head -1 | awk '{print $3}'`
#echo $emmcbyte
emmcbyte=`expr ${emmcbyte} \* 1024`
#echo $emmcbyte
emmcmegabyte=`expr ${emmcbyte} / 1000000`
#echo $emmcmegabyte

# Create working directory
if [ ! -d /var/images ]; then
  mkdir /var/images
fi

# Delete log file
if [ -f ${logfile} ]; then
  rm ${logfile}
fi

# Start dd with background
(dd if=/dev/mmcblk1 bs=10M | 7zr a -bd -t7z -m0=lzma -mx=9 -mfb=64 -md=32m -ms=on -si ${imgfile}) &> /tmp/dd.log &

# Display progress screen
begin_date=`date +%s`
active=1
while [ $active == 1 ]
do
#   ps -ef | grep  "dd if=" | grep -v grep | wc -l
   active=`ps -ef | grep  "dd if=" | grep -v grep | wc -l`
   DebugEcho -n "active="
   DebugEcho $active
   if [ $active == 1 ]; then
     GetCopied
     GetProgress
     DebugEcho -n "progress="
     DebugEcho $progress
     echo $progress | dialog --title "$title" --backtitle "$backtitle" \
     --gauge "\n\nCreating EMMC backup image (${emmcmegabyte} Mb). Please wait!" 10 80
   fi
   sleep ${sleeptime}
done


# Finish
end_date=`date +%s`
total=`expr $end_date - $begin_date`
min=`expr $total / 60`
sec=`expr $total % 60`

dialog --title "$title" --backtitle "$backtitle" \
--msgbox "\nAll done.Elapsed time (${min}Min.${sec}Sec.)" 7 60

