#!/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() {
    bbb_version=$1
    # Setup uEnv.txt
    if grep -q btrfs /etc/fstab ; then
        fstype=btrfs
    else
        fstype=ext4
    fi
    if [ ! $bbb_version ]; then
        kernelVersion=$(ls /usr/lib/*/am335x-boneblack.dtb | head -1 | cut -d/ -f4)
    else
        kernelVersion=$(ls /usr/lib/*/am335x-boneblack-${1}.dtb | head -1 | cut -d/ -f4)
    fi
    version=$(echo $kernelVersion | sed 's/linux-image-\(.*\)/\1/')
    initRd=initrd.img-$version
    vmlinuz=vmlinuz-$version
    bbb_loadaddr='0x82000000'
    bbb_initrd_addr='0x88080000'
    bbb_fdtaddr='0x88000000'
    # 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 ${bbb_dtb_file} dtb ; \
     cat $vmlinuz dtb >> temp-kernel ; \
     mkimage -A arm -O linux -T kernel -n "Debian kernel ${version}" \
             -C none -a 0x82000000 -e 0x82000000 -d temp-kernel uImage ; \
     rm -f temp-kernel ; \
     mkimage -A arm -O linux -T ramdisk -C gzip -a 0x88080000 -e 0x88080000 \
             -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
}
setup_flash_kernel() {
    if [ ! -d /etc/flash-kernel ] ; then
       mkdir /etc/flash-kernel
    fi
    echo -n "$1" > /etc/flash-kernel/machine
    command_line=""
    if [ -n "$2" ] ; then
        command_line="console=$2"
    fi
    if [ -n "$command_line" ] ; then
        echo flash-kernel flash-kernel/linux_cmdline string "$command_line" | debconf-set-selections
    fi
    apt-get install -y flash-kernel
}
case "$MACHINE" in
    beaglebone)
        beaglebone_setup_boot
        beaglebone_flash
        beaglebone_repack_kernel
        enable_serial_console ttyO0
        ;;
    beaglebonewifi)
        beaglebone_setup_boot wireless
        beaglebone_flash wireless
        beaglebone_repack_kernel wireless
        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
        ;;
    pcduino3)
        a20_setup_boot sun7i-a20-pcduino3.dtb
        enable_serial_console ttyS0
        ;;
esac