#!/bin/sh
#(c) Copyright Barry Kauler 04 February 2017, barryk.org
#Licence: GPL v3 (/usr/share/doc/legal)
#170225 snapshot, create configuration-$DATE, new version configuration-<version>.
#170524 remove 'session.sfs', this concept no longer used. various fixes.
#170525 make 'session.sfs' an optional technique. reconstitute session.sfs.
#170525 some exclusions from snapshot. 170527 fixes.
#170528 fixes. new "Current status" frame. /mnt/home now /mnt/wkg/home.

export TEXTDOMAIN=version-control
export OUTPUT_CHARSET=UTF-8

. /etc/DISTRO_SPECS
. /etc/rc.d/PUPSTATE

SESSIONSFSflag=1 #170525 see also 'init' script in initrd.q

DATE="$(date +%Y%m%d%H%M)"
mkdir -p /tmp/easy_version_control

test_cont_mntd_func() {
 MNTDFLAG="$(mount | grep "containers/${EC_NAME}/container")"
 if [ "$MNTDFLAG" ];then
  pupdialog --background '#FF4040' --backtitle "$(gettext 'ERROR: container') ${EC_NAME}" --msgbox "$(gettext 'You cannot perform a session erase, snapshot, or rollback, when a container is in use.')" 0 0
  return 1
 fi
 return 0
}

#170525 have coreutils 'sort', but this is cool...
#busybox sort does not have that option (sort in coreutils has '-V'), so improvise...
VERS="$(ls -l /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-* | rev | cut -f 1 -d '-' | rev)"
#refer: http://stackoverflow.com/questions/4493205/unix-sort-of-version-numbers
#sortedVERS="$(echo "$VERS" | sort -t. -k 1,1n -k 2,2n -k 3,3n -k 4,4n)" #lowest to highest.
revsortedVERS="$(echo "$VERS" | busybox sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr)" #highest to lowest.

#170525
if grep -qw aufs /proc/filesystems; then
 LAYERFS='aufs'
 RO='=ro'
else
 LAYERFS='overlay'
 RO=''
fi

#find all earlier versions and snapshots...
#170218 actions to ensure only one checkbox is ticked --any simpler way to do this?
BACKXML=""; REFRESHXML=""
rm -f /tmp/easy_version_control/chkbx-*
RELEASES="$(find /mnt/${WKG_DEV}/${WKG_DIR}repository -maxdepth 1 -type d -name 'easy-*' | rev | cut -f 1 -d '/' | rev | cut -f 2 -d '-' | sort -t. -k 1,1nr -k 2,2nr -k 3,3nr -k 4,4nr)" #version sort, highest to lowest
HI_REL="$(echo "$RELEASES" | head -n 1)"
for aVER in `echo "$RELEASES" | tr '\n' ' '`
do
 ZaVER="$(echo -n "$aVER" | tr '.' 'Z')" #cannot have . in label.
 SNAPXML=""
 for aSNAPDATE in `find /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${aVER} -maxdepth 1 -type f -name 'rw-*' | rev | cut -f 1 -d '/' | cut -f 2- -d '.' | rev | cut -f 2 -d '-' | tr '\n' ' '` #170525
 do
  echo "false" > /tmp/easy_version_control/chkbx-RW_${ZaVER}_${aSNAPDATE}
  REFRESHXML="${REFRESHXML}<action>refresh:RW_${ZaVER}_${aSNAPDATE}</action>"
  SNAPXML="${SNAPXML}
  <checkbox>
    <input file>/tmp/easy_version_control/chkbx-RW_${ZaVER}_${aSNAPDATE}</input>
    <label>Snapshot ${aSNAPDATE}</label>
    <variable>RW_${ZaVER}_${aSNAPDATE}</variable>
    <action>if true ls -1 /tmp/easy_version_control/chkbx-* | xargs -I SUB sed -i 's%.*%false%' SUB </action>
    <action>if true echo true > /tmp/easy_version_control/chkbx-RW_${ZaVER}_${aSNAPDATE} </action>
    REFRESHACTIONHERE
  </checkbox>"
 done
 echo "false" > /tmp/easy_version_control/chkbx-EASYVC_${ZaVER}
 REFRESHXML="${REFRESHXML}<action>refresh:EASYVC_${ZaVER}</action>"
 BACKXML="${BACKXML}
 <frame Easy ${aVER}>
   <vbox>
     <checkbox>
       <input file>/tmp/easy_version_control/chkbx-EASYVC_${ZaVER}</input>
       <label>Release ${aVER}</label>
       <variable>EASYVC_${ZaVER}</variable>
       <action>if true ls -1 /tmp/easy_version_control/chkbx-* | xargs -I SUB sed -i 's%.*%false%' SUB </action>
       <action>if true echo true > /tmp/easy_version_control/chkbx-EASYVC_${ZaVER} </action>
       REFRESHACTIONHERE
     </checkbox>
     ${SNAPXML}
   </vbox>
 </frame>"
done
PTN="s%REFRESHACTIONHERE%${REFRESHXML}%"
BACKXML2="$(echo "$BACKXML" | sed -e "$PTN")"

HI_CON=''
CONTAINERS="$(ls -1 /mnt/${WKG_DEV}/${WKG_DIR}containers)"
if [ "$CONTAINERS" ];then
 HI_CON="$(echo "$CONTAINERS" | head -n 1)"
 DEF_CON="<default>${HI_CON}</default>"
fi
#script to return list of snapshots in a container:
cat > /tmp/easy_version_control/ec-ver-snaps <<EOF
#!/bin/sh
#passed param is name of a container. else default to HI_CON
. /etc/rc.d/PUPSTATE
 EC_NAME="\$1"
 [ ! "\$EC_NAME" ] && EC_NAME="${HI_CON}"
 EC_SNAPS="\$(ls -1 /mnt/\${WKG_DEV}/\${WKG_DIR}containers/\${EC_NAME}/rw-* 2>/dev/null | rev | cut -f 1 -d '-' | rev)"
 if [ "\$EC_SNAPS" ];then
  echo "\$EC_SNAPS"
 else
  echo '-empty-'
 fi
EOF
chmod 755 /tmp/easy_version_control/ec-ver-snaps

M_close="$(gettext 'Close')"

M_help1="$(gettext 'Easy Linux keeps a record of everything. The user may take a snapshot of the current work at any time, and there will be upgrades to later versions. All snapshots and old versions are saved in the <i>repository</i> folder.')

$(gettext 'Easy Linux also supports containers, which are in the <i>containers</i> folder. Snapshots of current work in each container may also be saved, in this case inside the appropriate container folder.')

<b>$(gettext 'Warning')</b>
$(gettext 'Note that although it is nice to keep all of the history in the repository, this can get out of hand if memory storage is limited. A Flash stick of 8GB for example, may seem like a lot, but can quickly fill.')
$(gettext 'It is anticipated that some kind of pruning feature will be added to this Easy Version Control application, however, in the meantime it is easy to prune manually -- just delete unwanted folders and snapshots in the repository and containers folders.')

<b>Technical notes</b>
$(gettext 'Location of repository folder:') /mnt/${WKG_DEV}/${WKG_DIR}repository
$(gettext 'Location of containers folder:') /mnt/${WKG_DEV}/${WKG_DIR}containers"
export DLG_HELP1="<window resizable=\"false\" title=\"$(gettext 'Help: Easy Version Control')\" icon-name=\"gtk-index\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${M_help1}\"</label><variable>DLG_HELP1</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP1</action></button></hbox></vbox></window>"

M_help2="$(gettext 'Easy Linux runs on a layered filesystem. This has one or more read-only layers on the bottom, including <i>q.sfs</i> that has the entire Easy Linux files.
The top layer is read-write, and this has all of the changes you have made, such as new files and installed packages.')
$(gettext 'It is this read-write (rw) layer that will be saved.')

$(gettext 'Note that the purpose of taking a snapshot, is that it can be rolled back to at any future date.')

<b>$(gettext 'Some reasons for taking a snapshot')</b>
<b>$(gettext '1.')</b> $(gettext 'You have setup theming and network. Later on, you want to get rid of whatever you have done, and go back to this original state.')
<b>$(gettext '2.')</b> $(gettext 'You have added the devx.sfs as a read-only layer. Take a snapshot before compiling and installing source packages, then later can remove them all by rolling back to the snapshot.')

<b>$(gettext 'Technical notes')</b>
$(gettext 'The rw layer is located at:')
/mnt/${WKG_DEV}/${WKG_DIR}.session
$(gettext 'The snapshot will be saved as:')
/mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/rw-${DATE}.sfs"
export DLG_HELP2="<window resizable=\"false\" title=\"$(gettext 'Help: take snapshot')\" icon-name=\"gtk-index\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${M_help2}\"</label><variable>DLG_HELP2</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP2</action></button></hbox></vbox></window>"

if [ $SESSIONSFSflag -eq 1 ];then #170525
 M_help3b="$(gettext 'Files found inside each easy-<i>version</i> folder are:') vmlinuz, initrd.q, q.sfs, session.sfs, $(gettext 'and any extra SFS files that are desired to be loaded.')"
 M_help3c="
session.sfs: $(gettext 'Merge of session.sfs and rw layer of previous version')"
 M_help4b="$(gettext 'Easy Linux runs on a layered filesystem, with read-only (ro) layers <i>q.sfs</i> and <i>session.sfs</i>, and a read-write (rw) layer on top.')"
 M_help4c="
$(gettext 'Note that session.sfs is the merge of session.sfs and the rw-layer from the previous version of Easy Linux.')
"
else
 M_help3b="$(gettext 'Files found inside each easy-<i>version</i> folder are:') vmlinuz, initrd.q, q.sfs, $(gettext 'and any extra SFS files that are desired to be loaded.')"
 M_help3c=""
 M_help4b="$(gettext 'Easy Linux runs on a layered filesystem, with read-only (ro) layer <i>q.sfs</i>, and a read-write (rw) layer on top.')"
 M_help4c=""
fi

M_help3="$(gettext 'Previous versions (releases) of Easy Linux, if any, as well as any snapshots taken by the user, are saved in the <i>repository</i>.')

$(gettext 'The reason for keeping all of this history in the repository, is that the user can roll back to any earlier version and/or snapshot.')
$(gettext 'Reasons for doing this are varied. Sometimes, a later version proves to be troublesome, therefore it is a simple matter to rollback, until the problems with the later version are sorted out.')
$(gettext 'Snapshots are also useful, as they capture the state of the system when the user has it setup in a particular desired way, which can be rolled back to if there is a mess created later on.')

<b>$(gettext 'Technical notes')</b>
$(gettext 'Location of repository:') /mnt/${WKG_DEV}/${WKG_DIR}repository
$(gettext 'Folders within the repsitory are named easy-<i>version</i>, where <i>version</i> is the release number.')
$(gettext 'The current easy-<i>version</i> folder:') easy-${DISTRO_VERSION}
${M_help3b}
$(gettext 'If there are any snapshots, they are named:') rw-<i>date</i>.sfs
vmlinuz:  $(gettext 'This is the Linux kernel')
initrd.q: $(gettext 'This runs at bootup')
q.sfs:    $(gettext 'This has all of Easy Linux')${M_help3c}"
export DLG_HELP3="<window resizable=\"false\" title=\"$(gettext 'Help: Rollback')\" icon-name=\"gtk-index\" window_position=\"1\"><vbox><text use-markup=\"true\" width-chars=\"60\"><label>\"${M_help3}\"</label><variable>DLG_HELP3</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP3</action></button></hbox></vbox></window>"

M_help4="${M_help4b}
$(gettext 'It is this rw layer that will be erased.')
${M_help4c}
$(gettext 'The rw layer has all changes made by the user. Theming, network setup, installed packages, etc, are all in this rw layer.')
$(gettext 'Note however, anything installed to /mnt/wkg/home is outside the rw layer, and will not be erased.')

<b>Technical notes</b>
$(gettext 'Location of rw folder:') /mnt/${WKG_DEV}/${WKG_DIR}.session
$(gettext 'It is the content of this folder that will be erased.')
$(gettext '/mnt/wkg/home is a symlink to:') /mnt/${WKG_DEV}/${WKG_DIR}home
$(gettext 'This will not be erased. This is where user personal files are kept, such as downloads, photos, videos.')"
export DLG_HELP4="<window resizable=\"false\" title=\"$(gettext 'Help: Erase session')\" icon-name=\"gtk-index\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${M_help4}\"</label><variable>DLG_HELP4</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP4</action></button></hbox></vbox></window>"

M_help5="$(gettext 'A container is a copy of Easy Linux, to run applications isolated from the main Easy Linux and other containers.')

$(gettext 'This is done for two main reasons, security, and/or to provide a working environment that does not compromise the main Easy Linux. An example of the latter would be compiling and testing source packages.')

$(gettext 'A container is not really a copy, it just seems that way. It is built upon a layered filesystem, with <i>q.sfs</i> and other read-only layers that are shared with the main Easy Linux and other containers.')

$(gettext 'Each container has its own read-write layer, and this may be erased, or a snapshot taken. If there is a previous snapshot, then it can be rolled-back to.')

<b>Technical notes</b>
$(gettext 'Location of containers:') /mnt/${WKG_DEV}/${WKG_DIR}containers"
export DLG_HELP5="<window resizable=\"false\" title=\"$(gettext 'Help: Containers')\" icon-name=\"gtk-index\" window_position=\"1\"><vbox><text use-markup=\"true\"><label>\"${M_help5}\"</label><variable>DLG_HELP5</variable></text><hbox><button><label>${M_close}</label><action type=\"closewindow\">DLG_HELP5</action></button></hbox></vbox></window>"


export EVC_DIALOG="<window title=\"$(gettext 'Easy Version Control')\" icon-name=\"gtk-about\" window_position=\"1\">
 <vbox>
  <text><label>$(gettext 'Here you may choose to take a snapshot of the session, or roll back to an earlier snapshot or version')</label></text>

  <hbox>
    <vbox>
      <frame $(gettext 'Current status')>
        <text><label>$(gettext 'Version:') ${DISTRO_VERSION}</label></text>
      </frame>
    </vbox>
    <vbox>
      <frame $(gettext 'Session')>
       <hbox>
         <text><label>$(gettext 'Erase the working session:')</label></text>
         <vbox><button><label>Erase</label><action type=\"exit\">erase</action></button></vbox>
         <vbox><button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP4</action></button></vbox>
       </hbox>
       <hbox>
         <text><label>$(gettext 'Save the working session:')</label></text>
         <vbox><button><label>Snapshot</label><action type=\"exit\">snapshot</action></button></vbox>
         <vbox><button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP2</action></button></vbox>
       </hbox>
      </frame>
    </vbox>
  </hbox>

  <frame $(gettext 'Rollback')>
   <hbox>
    ${BACKXML2}
    <text space-expand=\"true\"><label>\" \"</label></text>
   </hbox>
   <hbox>
     <button><label>Rollback</label><action type=\"exit\">rollback</action></button>
      <button tooltip-text=\"Snapshot comments\"><input file>/usr/local/lib/X11/mini-icons/mini-Document.xpm</input><action>defaulttextviewer /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/snapshot-comments & </action></button>
      <text><label>\"  \"</label></text>
     <button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP3</action></button>
   </hbox>
  </frame>
  
  <frame $(gettext 'Containers')>
    <text><label>$(gettext 'Take a snapshot or erase the working session, or rollback to an earlier snapshot, in a container')</label></text>
    <hbox>
      <comboboxtext><variable>EC_NAME</variable>${DEF_CON}<input>ls -1 /mnt/${WKG_DEV}/${WKG_DIR}containers</input><action>refresh:EC_SNAP</action></comboboxtext>
      <button><label>Erase</label><action type=\"exit\">ec_erase</action></button>
      <button><label>Snapshot</label><action type=\"exit\">ec_snapshot</action></button>
      <text><label>\"  \"</label></text>
      <comboboxtext><variable>EC_SNAP</variable><input>/tmp/easy_version_control/ec-ver-snaps \${EC_NAME} </input></comboboxtext>
      <button><label>Rollback</label><action type=\"exit\">ec_rollback</action></button>
      <button tooltip-text=\"Snapshot comments\"><input file>/usr/local/lib/X11/mini-icons/mini-Document.xpm</input><action>defaulttextviewer /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/snapshot-comments & </action></button>
      <text><label>\"  \"</label></text>
      <button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP5</action></button>
    </hbox>
  </frame>
  
  <hbox>
   <hbox><button cancel></button></hbox>
   <button><input file>/usr/local/lib/X11/mini-icons/mini-question.xpm</input><action type=\"launch\">DLG_HELP1</action></button>
  </hbox>
 </vbox>
 </window>"

echo "$EVC_DIALOG" > evc.log
RETVARS="$(gtkdialog --program=EVC_DIALOG)"
eval "$RETVARS"

if [ "$EXIT" == "erase" ];then
 ###NOTICE### cannot do it now, done in initramfs.
 #rm -rf /mnt/${WKG_DEV}/${WKG_DIR}.session/*
 echo -n ",erase" > /.rollback.flg #init in initramfs will read this.
 pupdialog --background '#40FF40' --backtitle "$(gettext 'Erase almost completed')" --msgbox "$(gettext 'The rw layer will be erased. However, as this is currently in use, a reboot is required to take effect.')" 0 0
fi

if [ "$EXIT" == "snapshot" ];then
 #170525 some exclusions from snapshot...
 echo -e 'dev\nmnt\nproc\nrun\nsys\ntmp\nvar\n.fsckme.flg\nlost+found\n.XLOADED' > /tmp/easy_version_control/snapshot-exclusions1
 urxvt -bg '#09A0FF' -e mksquashfs /mnt/${WKG_DEV}/${WKG_DIR}.session /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/rw-${DATE}.sfs -ef /tmp/easy_version_control/snapshot-exclusions1 -no-exports
 cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/configuration /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/configuration-${DATE} #170225
 sync
# popup "background=#40FF10 level=top terminate=ok|<big>$(gettext 'Snapshot created!')</big>
#$(gettext 'Snapshot file:') /mnt/${WKG_DEV}/${WKG_DIR}repository/rw-${DATE}.sfs"
 echo -n "${DATE} " >> /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/snapshot-comments
 pupdialog --background '#40FF40' --backtitle "$(gettext 'Snapshot created')"  --inputbox "$(gettext 'Snapshot file:') /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/rw-${DATE}.sfs
$(gettext 'Please type a short one-line comment about this snapshot:')" 0 0 2> /tmp/easy_version_control/comment
 cat /tmp/easy_version_control/comment >> /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/snapshot-comments #170527
 exit
fi

#170525
reconstitute_sessionsfs_func() {
 localBackVer="$1"
 ROLAYERS=""
 if [ ! -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${localBackVer}/session.sfs ];then
  for APREVER in $revsortedVERS
  do
   [ "$APREVER" == "" ] && continue #precaution
   if vercmp ${APREVER} gt ${localBackVer}; then continue; fi
   if [ -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${localBackVer}/session.sfs ];then
    mkdir -p /tmp/easy_version_control/sessionsfs
    busybox mount -t squashfs -o loop,noatime /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${APREVER}/session.sfs /tmp/easy_version_control/sessionsfs
    ROLAYERS="${ROLAYERS}:/tmp/easy_version_control/sessionsfs${RO}"
    break
   else
    mkdir -p /tmp/easy_version_control/${APREVER}
    busybox mount -t squashfs -o loop,noatime /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${APREVER}/rw-${APREVER}.sfs /tmp/easy_version_control/${APREVER}
    ROLAYERS="${ROLAYERS}:/tmp/easy_version_control/${APREVER}${RO}"
   fi
  done
 fi
 if [ "$ROLAYERS" ];then
  xROLAYERS="$(echo -n "$ROLAYERS" | sed -e 's%^:%%')"
  mkdir -p /tmp/easy_version_control/tempwork
  mkdir -p /tmp/easy_version_control/rwlayer
  mkdir -p /tmp/easy_version_control/mntpt
  if [ "$LAYERFS" == "aufs" ];then
   mount -t aufs -o br=/tmp/easy_version_control/rwlayer=rw${ROLAYERS} aufs /tmp/easy_version_control/mntpt
  else
   mount -t overlay -o lowerdir=${xROLAYERS},upperdir=/tmp/easy_version_control/rwlayer,workdir=/tmp/easy_version_control/tempwork overlay /tmp/easy_version_control/mntpt
  fi
  urxvt -bg '#09A0FF' -e mksquashfs /tmp/easy_version_control/mntpt /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${localBackVer}/session.sfs #>/dev/null
  sync
  for AMNTD in `echo -n "$xROLAYERS" | tr ':' '\n' | cut -f 1 -d '=' | tr '\n' ' '`
  do
   busybox umount $AMNTD
  done
 fi
}

if [ "$EXIT" == "rollback" ];then
 backEASYver="$(echo "$RETVARS" | grep '^EASYVC_' | grep '"true"' | cut -f 2 -d '_' | cut -f 1 -d '=' | tr 'Z' '.')"
 if [ "$backEASYver" ];then
  #roll back to this version
  UMNTFLG=0
  if [ "$(mount | grep "/mnt/${BOOT_DEV} ")" == "" ];then
   mkdir -p /mnt/${BOOT_DEV}
   mount -t ${BOOT_FS} /dev/${BOOT_DEV} /mnt/${BOOT_DEV}
   UMNTFLG=1
  fi
  ###NOTICE### do not copy q.sfs now, as it may be in use, nor .session as in layer fs. init will do it.
  cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backEASYver}/vmlinuz /mnt/${BOOT_DEV}/${BOOT_DIR}
  cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backEASYver}/initrd.q /mnt/${BOOT_DEV}/${BOOT_DIR}
  #cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backEASYver}/q.sfs /mnt/${BOOT_DEV}/${BOOT_DIR}
  cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backEASYver}/configuration-${backEASYver} /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backEASYver}/configuration #170225
  sync
  if [ $SESSIONSFSflag -eq 1 ];then
   #170525 if no session.sfs in previous easy- folder, reconstitute it...
   reconstitute_sessionsfs_func ${backEASYver}
   echo -n "${backEASYver},erase" > /.rollback.flg #init in initramfs will read this. 170526 170529
  else
   echo -n "${backEASYver},${backEASYver}" > /.rollback.flg #init in initramfs will read this. 170526
  fi
  sync
  pupdialog --background '#40FF40' --backtitle "$(gettext 'Rollback almost completed')" --msgbox "$(gettext 'Current version:') ${DISTRO_VERSION}
$(gettext 'Rolled-back version:') ${backEASYver}
$(gettext 'This requires a reboot to take effect')" 0 0
 else
  backSNAP="$(echo "$RETVARS" | grep '^RW_' | grep '"true"' | cut -f 2,3 -d '_' | cut -f 1 -d '=' | tr 'Z' '.')"
  if [ "$backSNAP" ];then
   backVER="$(echo -n "backSNAP" | cut -f 1 -d '_')"
   backDATE="$(echo -n "backSNAP" | cut -f 2 -d '_')"
   if [ "$backVER" != "$DISTRO_VERSION" ];then
    UMNTFLG=0
    if [ "$(mount | grep "/mnt/${BOOT_DEV} ")" == "" ];then
     mkdir -p /mnt/${BOOT_DEV}
     mount -t ${BOOT_FS} /dev/${BOOT_DEV} /mnt/${BOOT_DEV}
     UMNTFLG=1
    fi
    ###NOTICE### do not copy q.sfs now, as it may be in use. init will do it.
    cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backVER}/vmlinuz /mnt/${BOOT_DEV}/${BOOT_DIR}
    cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backVER}/initrd.q /mnt/${BOOT_DEV}/${BOOT_DIR}
    #cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backVER}/q.sfs /mnt/${BOOT_DEV}/${BOOT_DIR}
    if [ $SESSIONSFSflag -eq 1 ];then
     #170525 if no session.sfs in previous easy- folder, reconstitute it...
     reconstitute_sessionsfs_func ${backVER}
    fi
    sync
   fi
   cp -f /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backVER}/configuration-${backDATE} /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${backVER}/configuration
   ###NOTICE### do not copy .session as in layer fs. init will do it.
   sync
   busybox umount /tmp/evc-snap-mnt
   [ $UMNTFLG -eq 1 ] && busybox umount /mnt/${BOOT_DEV}
   if [ "$backVER" == "$DISTRO_VERSION" ];then
    echo -n ",${backDATE}" > /.rollback.flg #init in initramfs will read this.
    MSGdone="$(gettext 'Rolled-back to snapshot:') rw-${backDATE}.sfs
$(gettext 'This will be copied to:') /mnt/${WKG_DEV}/${WKG_DIR}.session
$(gettext 'This requires a reboot to take effect.')"
   else
    echo -n "${backVER},${backDATE}" > /.rollback.flg #init in initramfs will read this.
    MSGdone="$(gettext 'Rolled-back to version:') ${backVER}
$(gettext 'Rolled-back to snapshot:') ${backDATE}
$(gettext 'This will be copied to:') /mnt/${WKG_DEV}/${WKG_DIR}.session
$(gettext 'This requires a reboot to take effect.')"
   fi
   pupdialog --background '#40FF40' --backtitle "$(gettext 'Rollback almost completed')" --msgbox "${MSGdone}" 0 0
  fi
 fi
fi

if [ "$EXIT" == "ec_erase" ];then
 test_cont_mntd_func #test if container in use.
 [ $? -ne 0 ] && exit
 rm -rf /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/.session/*
fi

if [ "$EXIT" == "ec_snapshot" ];then
 test_cont_mntd_func #test if container in use.
 [ $? -ne 0 ] && exit
 #170525 some exclusions from snapshot...
 echo -e 'dev\nmnt\nproc\nrun\nsys\ntmp\nvar\n.fsckme.flg\nlost+found\n.XLOADED' > /tmp/easy_version_control/snapshot-exclusions1
 urxvt -bg '#09A0FF' -e mksquashfs /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/.session /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/rw-${DATE}.sfs -ef /tmp/easy_version_control/snapshot-exclusions1 -no-exports
 cp -f /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/configuration /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/configuration-${DATE}
 sync
 echo -n "${DATE} " >> /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/snapshot-comments
 pupdialog --background '#40FF40' --backtitle "$(gettext 'Snapshot created')"  --inputbox "$(gettext 'Snapshot file:') /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/rw-${DATE}.sfs
$(gettext 'Please type a short one-line comment about this snapshot:')" 0 0 2> /tmp/easy_version_control/comment
 cat /tmp/easy_version_control/comment >> /mnt/${WKG_DEV}/${WKG_DIR}repository/easy-${DISTRO_VERSION}/snapshot-comments #170527
fi

if [ "$EXIT" == "ec_rollback" ];then
 test_cont_mntd_func #test if container in use.
 [ $? -ne 0 ] && exit
 mkdir -p /tmp/easy_version_control/tmpmnt
 busybox mount -t squashfs -o loop,noatime /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/rw-${EC_SNAP}.sfs /tmp/easy_version_control/tmpmnt
 rm -rf /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/.session/*
 cp -a /tmp/easy_version_control/tmpmnt/* /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/.session/
 [ -f /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/configuration-${EC_SNAP} ] && cp -f /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/configuration-${EC_SNAP} /mnt/${WKG_DEV}/${WKG_DIR}containers/${EC_NAME}/configuration
 sync
 busybox umount /tmp/easy_version_control/tmpmnt
fi

###end###
