# Super Grub Disk - tools.cfg
# Copyright (C) 2009,2010,2011,2012,2013,2014,2015  Adrian Gibanel Lopez.
#
# Super Grub Disk 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 3 of the License, or
# (at your option) any later version.
#
# Super Grub Disk 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 Super Grub Disk.  If not, see <http://www.gnu.org/licenses/>.

# This script contains several helper functions used in other cfg files .



function strcontains {
        set str="$1"
        set pattern="$2"
        insmod regexp
        if regexp ".*${pattern}.*" "$str"; then
                rmmod regexp
                return 0;
        else
                rmmod regexp
                return 1;
        fi
}

# Function: withplatform
# Return 0 (true) if our grub platform is sufficient for the argument
# Return 1 (false) if our grub platform is not sufficient
#
# argument is just efi or anything else for now
# echo out a failure message with a sleep when the check fails
# NOTE only checks we are efi if we want it and are not efi if we don't
function withplatform {
        if [ "${1}" = "efi" ]; then
                if [ "${grub_platform}" != "efi" ]; then
                        sgdwpwant="UEFI"
                        sgdwphave="BIOS"
                else
                        return 0
                fi
        else
                if [ "${grub_platform}" = "efi" ]; then
                        sgdwpwant=$"BIOS Compatibility Mode"
                        sgdwphave="UEFI"
                else
                        return 0
                fi
        fi
        echo $"Sorry, but we are booted via ""${sgdwphave}"$" and can not load this OS."
        echo $"Please try booting SG2D via"" ${sgdwpwant}."
        echo $"Press escape to return to the menu"
        sleep --interruptible 9999
        return 1
}

# Function: searchindevice
# According to global variables
# Return 0 (true) if the device should be searched for OS / cfgs / etc.
# Return 1 (false) if it should not be searched
#
# Device should be entered without ()
# Input example: fd0
#
# TODO: To be simplified when test function is fixed in upstream GRUB2
function searchindevice {

    set device="$1"

    if strcontains "$device" "fd" ; then
      fdcontains="yes"
    else
      fdcontains="no"
    fi

    if strcontains "$device" "cd" ; then
      cdcontains="yes"
    else
      cdcontains="no"
    fi

    if strcontains "$device" "$sg2d_dev_name" ; then
      sgdcontains="yes"
    else
      sgdcontains="no"
    fi

    if [ '(' "$fdcontains" != "yes" -a "$cdcontains" != "yes" -a "$sgdcontains" != "yes" ')' \
    -o \
    '(' "$fdcontains" = "yes"  -a  "$fd0search" = "on" ')' \
    -o \
    '(' "$cdcontains" = "yes"  -a  "$cdsearch" = "on" ')' ] \
    -o \
    '(' "$sgdcontains" = "yes"  -a  "$sgdsearch" = "on" ')' \
    ; then
      return 0;
    else
      return 1;
    fi

}

function update_sg2d_devices {

  if [ "${update_devices}" = "" -o "${update_devices}" = "TRUE" ] ; then

    set sg2d_devices=""

    insmod regexp

    for dev in (*); do
        # $device: parenthesis removed from $dev
        regexp -s device '\((.*)\)' ${dev}
        # Discard floppy and cdrom devices in search
        # But search them if their search has been enforced

        if searchindevice "${device}" ; then
                sg2d_devices="$sg2d_devices ${dev}"
        fi
    done
    export sg2d_devices
    update_devices="FALSE"
    export update_devices
    rmmod regexp
  fi

}

# Function: getdevice
# Saves into $1 variable the $2 without parenthesis
# Input example: device (hd0)
# Output example: hd0
function getdevice {

    insmod regexp
    regexp -s $1 '\((.*)\)' $2
    rmmod regexp
    export $1

}

# Function: basename
# Saves into $1 variable the $2 basename
# Input example: (hd0,msdos1)/boot/grub/grub.cfg
# Output example: grub.cfg
function basename {

    insmod regexp
    regexp -s 1:"$1" '^.*/(.*)$' "$2"
    rmmod regexp
    export $1
}

# Function: sbasename
# Saves into $1 variable the $2 with padding spaces
# Input example: (hd0,msdos1)/boot/grub/grub.cfg
# Output example: grub.cfg
function sbasename {

    basename tmpbasename $2
    set identspaces="                     "
    insmod regexp
    regexp -s 1:"$1" '(.{21})$' "${identspaces}${tmpbasename}"
    rmmod regexp
    export $1

}


# Function: echon_min_two_digits
# Prints its arguments but adds a leading zero to single digits.
# Input example: 1970 - 1 - 1 ' ' 0 : 0 : 0 ' '
# Output example: "1970-01-01 00:00:00 "
function echon_min_two_digits {
  while [ "$#" -ge 1 ]; do
    if regexp '^[0-9]$' "$1"; then echo -n 0; fi
    echo -n "$1"
    shift
  done
}


# Function: echon_datetime
# Prints the date and time, and if given, a message.
# Input example: hello
# Output example: "1970-01-01 00:00:00 hello"
function echon_datetime {
  echon_min_two_digits $YEAR - $MONTH - $DAY ' ' $HOUR : $MINUTE : $SECOND
  if [ -n "$*" ]; then echo " $*"; fi
}
