#!/bin/sh

# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (C) 2009-2014 Stephan Raue (stephan@openelec.tv)
# Copyright (C) 2020-present Team LibreELEC (https://libreelec.tv)

. /usr/lib/libreelec/functions

hidecursor

label=""
target=""
uuid=""

get_target() {
  for arg in $(cat /proc/cmdline); do
    case $arg in
      disk=*)
        disk="${arg#*=}"
        case $disk in
          LABEL=*)
            label="${disk#*=}"
            target="$(blkid -L ${label})"
            ;;
          UUID=*)
            uuid="${disk#*=}"
            target="$(blkid -U ${uuid})"
            ;;
          /*)
            target="$disk"
            ;;
        esac
        ;;
    esac
  done
}

show_reset_msg() {
  echo -e "RESET IN PROGRESS\n"
  echo -e "Please do not reboot or turn off your @DISTRONAME@ device!\n"
}

if [ -f /storage/.cache/reset_hard ]; then
  # hard reset
  rm -f /storage/.cache/reset_hard
  get_target
  if [ -n "${target}" ]; then
    show_reset_msg

    StartProgress spinner "Performing hard reset... "
    if [ -b "${target}" -a $(blkid -o value -s TYPE "${target}") = "ext4" ]; then
      umount /storage/
      mke2fs -t ext4 -m 0 "${target}" &>/dev/null
      if [ -n "${label}" ]; then
        tune2fs -U random -L "${label}" "${target}" &>/dev/null
      fi
      if [ -n "${uuid}" ]; then
        tune2fs -U "${uuid}" "${target}" &>/dev/null
      fi
    else
      find /storage/ -mindepth 1 -delete
    fi
    StopProgress "done!"

    echo
    StartProgress countdown "Rebooting in 5s... " 5 "NOW"
  fi
elif [ -f /storage/.cache/reset_soft ]; then
  # soft reset
  rm -f /storage/.cache/reset_soft
  get_target
  if [ -n "${target}" ]; then
    show_reset_msg

    StartProgress spinner "Performing soft reset... "
      find /storage/.cache/ /storage/.config/ /storage/.kodi/ -mindepth 1 -delete
    StopProgress "done!"

    echo
    StartProgress countdown "Rebooting in 5s... " 5 "NOW"
  fi
fi

sync
reboot -f
