#!/bin/sh

# Copyright (C) 2016
# Roger Shimizu <rogershimizu@gmail.com>
#
# Based on script kirkwood-qnap
# Copyright (C) 2014  Ian Campbell <ijc@debian.org>

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
# USA.

# This script attempts to determine the correct device tree blob to
# use on kirkwood based Buffalo Linkstation devices, which varies according
# to DTB file ships with the kernel installed.
#
# This is required because there's a split/rename for device tree file of
# a few Buffalo Linkstation devices. With help of this script, kernel
# upgrade/downgrade would be smooth as usual.

set -e

FK_DIR="/usr/share/flash-kernel"

. "${FK_CHECKOUT:-$FK_DIR}/functions"

info=0
if [ "x$1" = "x--info" ] ; then
    info=1
    [ -n "$2" ] && kvers=$2
elif [ "x$2" = "x--info" ] ; then
    info=1
    kvers=$1
elif [ -n "$1" ]; then
    kvers=$1
fi

[ -z "$kvers" ] &&
    kvers=$(linux-version list | linux-version sort | tail -1)

last_result=
success() {
    local res=$1

    if [ $info -gt 0 ] ; then
        echo "kirkwood-linkstation: success: $res" >&2
        if [ -n "$last_result" -a "x$res" != "x$last_result" ] ; then
            echo "kirkwood-linkstation: result mismatch" >&2
        fi
        last_result=$res
    else
        echo $res
        exit 0
    fi
}

fallback_dtb() {
    for dtb_name in $dtbs; do
        dtb=$(find_dtb_file)
        if [ -f "$dtb" ]; then
            echo "$dtb_name"
            return
        fi
    done

    # Workaround: DTB file will be removed before this script is invoked,
    # when upgrading or removing kernel package.
    # So here just provide a fake DTB as warning message, instead of error,
    # which will block upgrading or removing of kernel package.
    [ ! -d /usr/lib/linux-image-$kvers ] &&
        echo "kernel_removed?"
}

if [ $info -gt 0 ] ; then
  (
    /bin/echo -e "Kernel:\t\t$(uname -a)"
    /bin/echo -e -n "cpuinfo:\t" ; grep ^Hardware /proc/cpuinfo
    /bin/echo -e -n "dt model:\t"
    if [ -e /proc/device-tree/model ] ; then
        cat /proc/device-tree/model
        /bin/echo
    else
        /bin/echo "n/a"
    fi
    /bin/echo
  ) >&2
fi

get_machine
if [ -z "$machine" -a $info -gt 0 ] ; then
   machine=`get_machine`
fi
echo "kirkwood-linkstation: machine: $machine" >&2

case $machine in
"Buffalo Linkstation LS-VL")
    dtbs="kirkwood-linkstation-lsvl.dtb kirkwood-lswvl.dtb"
    ;;
"Buffalo Linkstation LS-WSXL")
    dtbs="kirkwood-linkstation-lswsxl.dtb kirkwood-lswxl.dtb"
    ;;
"Buffalo Linkstation LS-WVL"|"Buffalo Linkstation LS-WVL/VL")
    dtbs="kirkwood-linkstation-lswvl.dtb kirkwood-lswvl.dtb"
    ;;
"Buffalo Linkstation LS-WXL"|"Buffalo Linkstation LS-WXL/WSXL")
    dtbs="kirkwood-linkstation-lswxl.dtb kirkwood-lswxl.dtb"
    ;;
*)
    error "Unknown machine $machine"
    ;;
esac

dtb_name=$(fallback_dtb)
if [ -n "$dtb_name" ]; then
    success $dtb_name
else
    echo "kirkwood-linkstation: Unable to determine $machine variant " >&2
fi
