#!/usr/bin/env bash
#
# Script to convert Pidora 2014 to a NOOBs useable operating system
#
#
# To use this script, provide the image name as
# the first argument.
# Example "./makenoobs image-name.img"
#
# Requirements (Fedora 20 package):
# bash (bash)
# fdisk (util-linux)
# kpartx (kpartx)
# pv (pipe viewer)
# tree (File system tree viewer)
#
# Version 1.0 2014-08-15
#
# Authors:
# Andrew Greene, Seneca College 2014-08-15
#
set -e
date=$(date +"%y-%m-%d")
mydir=./
bootdir="$mydir/tmp/boot"
rootfsdir="$mydir/tmp/rootfs"
noobsdir="$mydir/Noobs-Pidora"
PidoraVer=20
# partition
loopdev=$(kpartx -av "$1" | awk 'NR==1 {print $3}')
echo -e "Using loopback device: ${loopdev::5}"
#create temp dirs
mkdir -p $bootdir
mkdir -p $rootfsdir
mkdir -p $noobsdir/$date
sleep 1
# mount boot / rootfs partition
mount -o rw /dev/mapper/${loopdev::5}p1 $bootdir
mount -o rw /dev/mapper/${loopdev::5}p2 $rootfsdir
# Modifying cmdline.txt file for Noobs
cd $bootdir
echo "Compressing Boot Partition"
sed -i "s|root=/dev/mmcblk0p2|root=/dev/mmcblk0p5|" cmdline.txt
kernelver=$(ls kernel-* | awk -F- '{print $2}')
bootsize=$(du -h . | tail -1 | awk '{print $1}')
tar cf - . --exclude=boot.tar.xz | pv -s `du -sb . | grep -o '[0-9]\+'` -N tar | xz -9 -c - > ../../$noobsdir/$date/boot.tar.xz
cd -
echo "Compressing Rootfs Partition"
cd $rootfsdir
rootsize=$(du -h . | tail -1 | awk '{print $1}')
tar cf - . --exclude=rootfs.tar.xz | pv -s `du -sb . | grep -o '[0-9]\+'` -N tar | xz -9 -c - > ../../$noobsdir/$date/rootfs.tar.xz
cd -
umount $bootdir
umount $rootfsdir
# deleting loopback devices
kpartx -dv "$1"
rm -rf $bootdir $rootfsdir
echo "Creating Noobs Pidora Release Files"
# Creating OS Json File
cat << EOF > $noobsdir/$date/os.json
{
"name": "Pidora",
"url": "http://pidora.ca/",
"version": "$PidoraVer",
"release_date": "2013-08-09",
"kernel": "$kernelver",
"description": "Pidora is a Fedora Remix optimized for the Raspberry Pi"
}
EOF
cat << EOF > $noobsdir/$date/partitions.json
{
"partitions": [
{
"label": "boot",
"filesystem_type": "FAT",
"partition_size_nominal": 100,
"want_maximised": false,
"uncompressed_tarball_size": $bootsize,
"mkfs_options": ""
},
{
"label": "rootfs",
"filesystem_type": "ext4",
"partition_size_nominal": 1500,
"want_maximised": true,
"uncompressed_tarball_size": $rootsize,
"mkfs_options": ""
}
]
}
EOF
#get the latest release notes from pidora.ca
echo "updateing Noobs Release Notes!"
curl -s "http://pidora.ca/Noobs/release-notes.txt" > $noobsdir/$date/release-notes.txt
RepString=$(grep "Fedora package collection version:" $noobsdir/$date/release-notes.txt)
#replace Package collection
sed -i "s|$RepString|Fedora package collection version: $PidoraVer|" $noobsdir/$date/release-notes.txt
#replace Kernel NVR
RepString=$(grep "Kernel version: " $noobsdir/$date/release-notes.txt)
sed -i "s|$RepString|Kernel version: ($kernelver)|" $noobsdir/$date/release-notes.txt
#replace Firmware NVR
RepString=$(grep "raspberrypi-vc" $noobsdir/$date/release-notes.txt)
#Get latest firmware from pidora.ca
FirmwareVer=$(curl -s "http://pidora.proximity.on.ca/pidora/rpfr-updates/20/SRPMS/r/" | grep "raspberrypi-vc" | awk -v val=$num -F'"' '{print $2}' |awk -F. '{print $1"."$2}')
sed -i "s|$RepString|$FirmwareVer|" $noobsdir/$date/release-notes.txt
#replace Firmware release date
RepString=$(grep -ws "Date Released:" $noobsdir/$date/release-notes.txt -A 1 | tail -1)
FirmwareDate=${FirmwareVer:15:8}
#convert release date
FormatDate=$(date -d "$FirmwareDate" +'%B %d, %Y')
sed -i "s|$RepString|$FormatDate|" $noobsdir/$date/release-notes.txt
curl -s "http://pidora.ca/Noobs/partition_setup.sh" > $noobsdir/$date/partition_setup.sh
#download Noobs slide images from pidora.ca
echo "Downloading Noobs Pidora Slide Images!"
wget -P $noobsdir/$date/ -A png "http://pidora.ca/Noobs/Pidora.png" > /dev/null 2>&1
wget -P $noobsdir/$date/ -e robots=off --cut-dirs=1 --reject="index.html*" --recursive --relative --level=1 --no-host "http://pidora.ca/Noobs/slides/" > /dev/null 2>&1
wget -P $noobsdir/$date/ -e robots=off --cut-dirs=1 --reject="index.html*" --recursive --relative --level=1 --no-host "http://pidora.ca/Noobs/slides_vga/" > /dev/null 2>&1
echo "NOOBs Pidora OS location: $noobsdir/$date"
tree $noobsdir/$date