#!/bin/sh
#supports_backup in PINN

set -x

if [ -z "$part1" ] || [ -z "$part2" ] || [ -z "$part3" ] || [ -z "$part4" ]; then
  printf "Error: missing environment variable part1,2,3 or 4\n" 1>&2
  exit 1
fi

p1=`echo ${part1} | sed -e 's/dev/dev\/block/g'`
p2=`echo ${part2} | sed -e 's/dev/dev\/block/g'`
p3=`echo ${part3} | sed -e 's/dev/dev\/block/g'`
p4=`echo ${part4} | sed -e 's/dev/dev\/block/g'`

mkdir -p /tmp/1 /tmp/2 /tmp/3 /tmp/4

mount "$part1" /tmp/1
mount "$part2" /tmp/2
mount "$part3" /tmp/3
mount "$part4" /tmp/4

#-----------------------------
fixup()
{
    #move from boot to data partition for temporary space
    mv /tmp/1/$1.img /tmp/4/ramdisk.cpio.gz
    cd /tmp/4
    gunzip ramdisk.cpio.gz
    mkdir /tmp/4/tmp
    cd /tmp/4/tmp
    cpio -i -F ../ramdisk.cpio

    if [ -e etc/recovery.fstab ]; then #for TWRP
        if echo $part1 | grep "/dev/sda" - > /dev/null; then
            sed etc/recovery.fstab -i -e "s|sda|sdb|g"
        fi  

        awk -v p1=$p1 -v p2=$p2 -v p3=$p3 -v p4=$p4 '
              BEGIN {FS=" "; OFS="\t";}
              /^\/boot/ { $3=p1 }
              /^\/system/ { $3=p2 }
              /^\/vendor/ { $3=p3 }
              /^\/data/ { $3=p4 }
                        {print}
               ' etc/recovery.fstab > etc/recovery.fstab.new
        mv etc/recovery.fstab.new etc/recovery.fstab
    fi

    if [ -e etc/recovery.usb.fstab ]; then #for TWRP
        if echo $part1 | grep "/dev/sda" - > /dev/null; then
            sed etc/recovery.usb.fstab -i -e "s|sda|sdb|g"
        fi  

        awk -v p1=$p1 -v p2=$p2 -v p3=$p3 -v p4=$p4 '
              BEGIN {FS=" "; OFS="\t";}
              /^\/boot/ { $3=p1 }
              /^\/system/ { $3=p2 }
              /^\/vendor/ { $3=p3 }
              /^\/data/ { $3=p4 }
                        {print}
               ' etc/recovery.usb.fstab > etc/recovery.usb.fstab.new
        mv etc/recovery.usb.fstab.new etc/recovery.usb.fstab
    fi

    fstab="fstab.rpi4"
    if [ ! -e $fstab ]; then
        fstab="fstab.rpi3"
    fi

    if [ -e $fstab ]; then

        #17.1 Nov2020 uses DT, so add missing entries to fstab
        grep "/boot" $fstab >/dev/null
        if [ $? -eq 1 ]; then #error, not found, so add
            rm $fstab.new
            while read line
            do
                echo $line | grep -q "/data"
                if [ $? -eq 0 ]; then #found the following line, so add
                    echo "/dev/block/mmcblk0p1              /boot        vfat    ro                                                  wait,first_stage_mount" >>$fstab.new
                    echo "/dev/block/mmcblk0p2              /system      ext4    ro,barrier=1                                        wait,first_stage_mount" >>$fstab.new
                    echo "/dev/block/mmcblk0p3              /vendor      ext4    ro,barrier=1                                        wait,first_stage_mount" >>$fstab.new                    
                fi    
                echo $line >>$fstab.new
            done <$fstab
            mv $fstab.new $fstab
            
            sed -i -e "s|^dtoverlay=android-usb|#&|"    /tmp/1/config.txt
            sed -i -e "s|^dtoverlay=android-sdcard|#&|" /tmp/1/config.txt
        fi

        sed -i -e "s|^/dev.* /boot |$p1  /boot |" $fstab
        sed -i -e "s|^/dev.* /system |$p2  /system |" $fstab
        sed -i -e "s|^/dev.* /vendor |$p3  /vendor |" $fstab
        sed -i -e "s|^/dev.* /data |$p4  /data |" $fstab

        cp $fstab /tmp/3/etc
    fi

    cpio -i -t -F ../ramdisk.cpio | cpio -o -H newc >../ramdisk_new.cpio
    cd ..
    rm ramdisk.cpio
    mv ramdisk_new.cpio ramdisk.cpio
    gzip ramdisk.cpio
    mv ramdisk.cpio.gz /tmp/1/$1.img
    rm -rf /tmp/4/tmp
}
#-----------------------------

fixup ramdisk
fixup ramdisk-recovery

cd /tmp

umount /tmp/1
umount /tmp/2
umount /tmp/3
umount /tmp/4
rmdir 1
rmdir 2
rmdir 3
rmdir 4
