#!/bin/bash
#################################################################################
# /usr/lib/setup/armedslack-nofscheck
#
# On some devices such as the Raspberry Pi, there is no RTC (Real Time Clock)
# which causes fsck checks on the system, which is undesirable because it keeps
# happening since there's no RTC!
# Users using such devices should switch off the fsck checks.
#
# Stuart Winter <mozes@slackware.com>
# 03-Jan-2022, test for presence of RTC using hwclock
#              support file system labels.
# 12-Jan-2008, check only for Raspberry Pi v1.
# 10-Mar-2015, check for RPi models 1 & 2, and the HummingBoard i1
# 01-Aug-2018, Check for RPI3 models.
#################################################################################
# Copyright 2008 - 2022 Stuart Winter, Donostia, Spain.
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

FSTAB=/var/log/setup/tmp/SeTnative

# Silently exit if there aren't any 'ext' file systems,
# as we only support those presently.
[[ $( grep -E '^/|^LABEL=' $FSTAB | awk '$3~/ext[0-9]/ {print $1}' | wc -l ) -eq 0 ]] && exit 0

# Silently exit if the hardware clock is accessible:
hwclock -r > /dev/null 2>&1 && exit 0

dialog \
   --backtitle "Hardware Model without an RTC (Real Time Clock)" \
   --title "SKIP FSCK CHECKS?" --yesno \
"No Real Time Clock (RTC) detected.\n\n\
This means that fsck time-based checks will always run on bootup, and you may \
experience trouble booting the Operating System.\
\n\nSaying 'Yes' here will switch off fsck checks \
for all ext filesystem mount points in /etc/fstab \
\n
\n\nWould you like to switch off fsck checks?\
\n\nRecommendation: Yes" 17 69

#However, JFS filesystems will ONLY mount after a clean report from fsck - so \
#if you have any JFS filesystems, it is recommended to choose 'No'. \

if [ $? = 0 ]; then
   # Switch off fsck checks & produce nice output format
   # consistent with Slackware.
#   printf "%-16s %-16s %-11s %-16s %-3s %s\n" $( awk "/^ *$(sed -e's?/?\\/?g' <<<"${1:-/dev}")/{\$6=0};{print \$0}" $FSTAB ) > $FSTAB
   ( grep -E '^/|^LABEL=' $FSTAB | awk '$3~/ext[0-9]/ {print $1}' | while read fs ; do
        echo "${fs}..." ; tune2fs -i0 ${fs}
     done ) > /dev/null 2>&1 # > /tmp/armedslack-tune2fs.debug
fi

# Time for the user to see:
sleep 3
clear
exit 0
