#!/bin/sh
#
# .---.                  .              .
# |                      |              |
# |--- .--. .-.  .-.  .-.|  .-. .--.--. |.-.  .-. .--.  .-.
# |    |   (.-' (.-' (   | (   )|  |  | |   )(   )|  | (.-'
# '    '     --'  --'  -' -  -' '  '   -' -'   -' '   -  --'
#
#                    Freedom in the Cloud
#
# Hardware setup based on bin/freedombox-hardware-setup from freedom-maker
#
# License
# =======
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see .
PROJECT_NAME='freedombone'
export TEXTDOMAIN=${PROJECT_NAME}-image-hardware-setup
export TEXTDOMAINDIR="/usr/share/locale"
enable_serial_console() {
    # By default, spawn a console on the serial port
    device="$1"
    echo $"Adding a getty on the serial port"
    echo "T0:12345:respawn:/sbin/getty -L $device 115200 vt100" >> /etc/inittab
}
beaglebone_setup_boot() {
    # Setup uEnv.txt
    if grep -q btrfs /etc/fstab ; then
        fstype=btrfs
    else
        fstype=ext4
    fi
    kernelVersion=$(ls /usr/lib/*/am335x-boneblack.dtb | head -1 | cut -d/ -f4)
    version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
    initRd=initrd.img-$version
    vmlinuz=vmlinuz-$version
    # uEnv.txt for Beaglebone
    # based on https://github.com/beagleboard/image-builder/blob/master/target/boot/beagleboard.org.txt
    cat >> /boot/uEnv.txt < /boot/$initRd )
    rm -rf /tmp/initrd-repack
    (cd /boot ; \
     cp /usr/lib/$kernelVersion/am335x-boneblack.dtb dtb ; \
     cat $vmlinuz dtb >> temp-kernel ; \
     mkimage -A arm -O linux -T kernel -n "Debian kernel ${version}" \
             -C none -a 0x80200000 -e 0x80200000 -d temp-kernel uImage ; \
     rm -f temp-kernel ; \
     mkimage -A arm -O linux -T ramdisk -C gzip -a 0x81000000 -e 0x81000000 \
             -n "Debian ramdisk ${version}" \
             -d $initRd uInitrd )
}
a20_setup_boot() {
    dtb="$1"
    # Setup boot.cmd
    if grep -q btrfs /etc/fstab ; then
        fstype=btrfs
    else
        fstype=ext4
    fi
    kernelVersion=$(ls /usr/lib/*/$dtb | head -1 | cut -d/ -f4)
    version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
    initRd=initrd.img-$version
    vmlinuz=vmlinuz-$version
    # Create boot.cmd
    cat >> /boot/boot.cmd <> /etc/initramfs-tools/modules
}
case "$MACHINE" in
    beaglebone)
        beaglebone_setup_boot
        beaglebone_flash
        beaglebone_repack_kernel
        enable_serial_console ttyO0
        ;;
    cubietruck)
        a20_setup_boot sun7i-a20-cubietruck.dtb
        enable_serial_console ttyS0
        ;;
    a20-olinuxino-lime)
        a20_setup_boot sun7i-a20-olinuxino-lime.dtb
        enable_serial_console ttyS0
        ;;
    a20-olinuxino-lime2)
        a20_setup_boot sun7i-a20-olinuxino-lime2.dtb
        enable_serial_console ttyS0
        ;;
    a20-olinuxino-micro)
        a20_setup_boot sun7i-a20-olinuxino-micro.dtb
        enable_serial_console ttyS0
        ;;
    cubieboard2)
        a20_setup_boot sun7i-a20-cubieboard2.dtb
        enable_serial_console ttyS0
        ;;
esac