#!/bin/sh
#(c) Copyright Barry Kauler 2009,2017. License GPL v3 (/usr/share/doc/legal)
#creates a 520MB skeleton image for 3buildeasydistro. only need to run this once.
#put resultant image into folder sd-skeleton-images, including the -DISKID file.
#170129 first release.
#170724 create skeleton with 2nd ext4 16MB partition.
#170730 change from gpt to mbr, still with esp fat partition.
#170805 syslinux, now have menu.
#170818 change Rollback msg.
#180530 this script was way out of date. now using refind for uefi, image now 640MB.
#       partition is 639MB, starting offset 1MB, extra 1MB on end, total image file is 641MB.
#       this is the recommended script for easy os.
#190123 also copy kbdmap.c32

. ./DISTRO_SPECS

if ! which syslinux;then exit 1; fi

#check syslinux installed properly...
MBRBIN=""
[ -f /usr/lib/syslinux/mbr.bin ] && SLPATH="/usr/lib/syslinux"
[ "$SLPATH" = "" ] && [ -f /usr/lib/SYSLINUX/mbr.bin ] && SLPATH="/usr/lib/SYSLINUX" #141102 ubuntu utopic unicorn.
[ "$SLPATH" = "" ] && [ -f /usr/share/syslinux/mbr.bin ] && SLPATH="/usr/share/syslinux"
[ "$SLPATH" ] && MBRBIN="${SLPATH}/mbr.bin"
#170129 ubuntu 16.04 xenial xerus devs have really gone to town moving stuff around...
[ ! "$MBRBIN" ] && [ -f /usr/lib/syslinux/mbr/mbr.bin ] && MBRBIN='/usr/lib/syslinux/mbr/mbr.bin'
echo
if [ ! "$MBRBIN" ];then
 echo "Sorry, cannot find file 'mbr.bin'. Looked in /usr/lib/syslinux,
/usr/lib/syslinux/mbr, /usr/lib/SYSLINUX and /usr/share/syslinux. You have an
abnormal installation of Syslinux. Aborting"
 exit
fi

echo
echo 'Plug in a usb flash stick, 1GB or greater.'
echo -n 'Type drive name of stick (ex: sdc): '
read TARGETDRIVE
echo -n "It is ${TARGETDRIVE}, is that OK? ENTER if OK: "
read isitok

PARTNUM1="1"
PARTNUM2="2"
if [ "`echo -n "$TARGETDRIVE" | grep 'mm'`" != "" ];then
 PARTNUM1="p1"
 PARTNUM2="p2"
fi

#sizes...
FDISKINFO="$(fdisk -l /dev/${TARGETDRIVE} 2>/dev/null)" #141104 leave as fdisk.
SIZEBYTES=`echo "$FDISKINFO" | grep '^Disk /dev' | cut -f 5 -d ' '` #141104
SIZEKB=`expr $SIZEBYTES \/ 1024`
SIZEM=`expr $SIZEKB \/ 1024`
SHORT20M=`expr $SIZEM - 20`
SHORT5BLOCKS=`expr $SHORT20M \/ 4`
echo "Size of drive: ${SIZEM}MB"

echo "Zeroizing first 672MB of drive..."
dd if=/dev/zero of=/dev/${TARGETDRIVE} bs=4M count=168
sync

echo
echo 'Creating a new dos partition table, using fdisk...'
busybox echo -e 'o\nw\n' | fdisk /dev/${TARGETDRIVE}
sync

echo "Writing ${MBRBIN}"
dd bs=440 conv=notrunc count=1 if=${MBRBIN} of=/dev/${TARGETDRIVE}
sync

#create 639MB partition, so total size of image will be 640MB (and a 2nd partition will start on a 8MB boundary)
echo "Creating 639MB esp partition..."
busybox echo -e 'n\np\n1\n2048\n+639M\nw\n' | fdisk /dev/${TARGETDRIVE}
sync
#set partition-id to efi...
busybox echo -e 't\nef\nw' | fdisk /dev/${TARGETDRIVE}
sync
#set the boot flag...
busybox echo -e 'a\nw' | fdisk /dev/${TARGETDRIVE}
sync

echo "Create fat32 filesystem:"
mkdosfs -F 32 -n easy1 /dev/${TARGETDRIVE}${PARTNUM1} #FAT32.
sync

echo "Install syslinux in 1st partition, ${TARGETDRIVE}${PARTNUM1}..."
mkdir -p /mnt/${TARGETDRIVE}${PARTNUM1}
busybox mount -t vfat /dev/${TARGETDRIVE}${PARTNUM1} /mnt/${TARGETDRIVE}${PARTNUM1}
cp -a boot/boot-skeleton/* /mnt/${TARGETDRIVE}${PARTNUM1}/
sync

#could update some syslinux files, if exist....
[ -f ${SLPATH}/ldlinux.c32 ] && cp -a -f ${SLPATH}/ldlinux.c32 /mnt/${TARGETDRIVE}${PARTNUM1}/
[ -f ${SLPATH}/libutil.c32 ] && cp -a -f ${SLPATH}/libutil.c32 /mnt/${TARGETDRIVE}${PARTNUM1}/
[ -f ${SLPATH}/menu.c32 ] && cp -a -f ${SLPATH}/menu.c32 /mnt/${TARGETDRIVE}${PARTNUM1}/
#190123 add kbdmap.c32, needs dep libcom32.c32...
[ -f ${SLPATH}/kbdmap.c32 ] && cp -a -f ${SLPATH}/kbdmap.c32 /mnt/${TARGETDRIVE}${PARTNUM1}/
[ -f ${SLPATH}/libcom32.c32 ] && cp -a -f ${SLPATH}/libcom32.c32 /mnt/${TARGETDRIVE}${PARTNUM1}/

sync
busybox umount /mnt/${TARGETDRIVE}${PARTNUM1}

#this is for bios (not uefi) booting:
syslinux --install /dev/${TARGETDRIVE}${PARTNUM1}
sync


#150324 somehow won't boot, for amd64 install. reinstalling mbr.bin here fixes it...
sync
dd bs=440 conv=notrunc count=1 if=${MBRBIN} of=/dev/${TARGETDRIVE}
sync

#have a daemon, send this uevent to it, then /usr/local/pup_event/frontend_change will be called...
echo change > /sys/block/${TARGETDRIVE}/uevent
sleep 5
echo
echo "${TARGETDRIVE} now has the 640MB skeleton image for Easy Linux."

echo "Copying image to file 'easy-skeleton-${DISTRO_TARGETARCH}-640mb.img'"
#copy an extra 1M. this will be zeroes, prevents an old ext4 partition being recognised when write it  to a drive.
[ -f easy-skeleton-${DISTRO_TARGETARCH}-640mb.img ] && rm -f easy-skeleton-${DISTRO_TARGETARCH}-6400mb.img
dd if=/dev/${TARGETDRIVE} of=easy-skeleton-${DISTRO_TARGETARCH}-640mb.img bs=1M count=641 #instead of 640.
sync

DISKID="$(fdisk -l /dev/${TARGETDRIVE} | grep '^Disk identifier' | rev | cut -f 1 -d ' ' | rev)"
echo -n "$DISKID" > easy-skeleton-${DISTRO_TARGETARCH}-640mb-DISKID

echo "Compressing to easy-skeleton-${DISTRO_TARGETARCH}-6400mb.img.gz"
[ -f easy-skeleton-${DISTRO_TARGETARCH}-640mb.img.gz ] && rm -f easy-skeleton-${DISTRO_TARGETARCH}-640mb.img.gz
gzip easy-skeleton-${DISTRO_TARGETARCH}-640mb.img
sync

echo
echo "Done. Copy easy-skeleton-${DISTRO_TARGETARCH}-640mb.img.gz and easy-skeleton-${DISTRO_TARGETARCH}-640mb-DISKID to folder sd-skeleton-images"

################
echo
echo "Now can create a skeleton image with a second partition, ext4 no-journal, 16M size"
echo -n 'ENTER only to NOT do this: '
read goondoit
[ "$goondoit" == "" ] && exit
echo
echo "Creating 16MB partition /dev/${TARGETDRIVE}${PARTNUM2}"
echo -e "n\n2\n\n+16384K\nw" | fdisk -u /dev/${TARGETDRIVE}
sync
echo
echo "Creating ext4 f.s. without journal in /dev/${TARGETDRIVE}${PARTNUM2}"
mke2fs -q -t ext4 -O ^has_journal -L easy2 -m 0 -b 4096 /dev/${TARGETDRIVE}${PARTNUM2}
sync
echo change > /sys/block/${TARGETDRIVE}/uevent
sleep 5
echo
echo "Copying image to file 'easy-skeleton-${DISTRO_TARGETARCH}-656mb.img'"
#copy an extra 1M. this will be zeroes, prevents an old ext4 partition being recognised when write it  to a drive.
[ -f easy-skeleton-${DISTRO_TARGETARCH}-656mb.img ] && rm -f easy-skeleton-${DISTRO_TARGETARCH}-656mb.img
dd if=/dev/${TARGETDRIVE} of=easy-skeleton-${DISTRO_TARGETARCH}-656mb.img bs=1M count=657 #instead of 656.
sync
echo
echo "Creating easy-skeleton-${DISTRO_TARGETARCH}-656mb-DISKID"
DISKID="$(fdisk -l /dev/${TARGETDRIVE} | grep '^Disk identifier' | rev | cut -f 1 -d ' ' | rev)"
echo -n "$DISKID" > easy-skeleton-${DISTRO_TARGETARCH}-656mb-DISKID
echo
echo "Compressing to easy-skeleton-${DISTRO_TARGETARCH}-656mb.img.gz"
[ -f easy-skeleton-${DISTRO_TARGETARCH}-656mb.img.gz ] && rm -f easy-skeleton-${DISTRO_TARGETARCH}-656mb.img.gz
gzip easy-skeleton-${DISTRO_TARGETARCH}-656mb.img
sync
echo
echo "Done. Copy easy-skeleton-${DISTRO_TARGETARCH}-656mb.img.gz and easy-skeleton-${DISTRO_TARGETARCH}-656mb-DISKID to folder sd-skeleton-images"
###END###


