#!/bin/bash
#
# This file is part of util-linux.
#
# This file 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 file 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.
#
TS_TOPDIR="${0%/*}/../.."
TS_DESC="fsck images"

. $TS_TOPDIR/functions.sh
ts_init "$*"

# inode contains UID and GID, use always UID=0 to get the same checksum
ts_skip_nonroot

ts_check_test_command "$TS_CMD_FSCKMINIX"
ts_check_test_command "$TS_HELPER_MKFS_MINIX"

export MKFS_MINIX_TEST_SECOND_SINCE_EPOCH='1438460212'

check_minix_fs_type() {
	ts_init_subtest $1
	img=${TS_OUTPUT}.img
	dd if=/dev/zero bs=1024 count=16 of=$img >/dev/null 2>&1
	$TS_HELPER_MKFS_MINIX $2 $img >> $TS_OUTPUT 2>&1
	echo "mkfs return value: $?" >> $TS_OUTPUT
	$TS_CMD_FSCKMINIX $img >> $TS_OUTPUT 2>&1
	echo "fsck return value: $?" >> $TS_OUTPUT
	md5sum $img | awk '{print $1}' >> $TS_OUTPUT 2>&1
	rm -f $img
	ts_finalize_subtest
}

check_minix_fs_type 'v1c14' '-1 -n 14'
check_minix_fs_type 'v1c30' '-1 -n 30'
check_minix_fs_type 'v2c14' '-2 -n 14'
check_minix_fs_type 'v2c30' '-2 -n 30'
check_minix_fs_type 'v3c60' '-3 -n 60'

ts_init_subtest "auto-fix"
img=${TS_OUTPUT}.img
cp "$TS_SELF/broken-root" $img
"$TS_CMD_FSCKMINIX" -sav $img |
	sed 's/Filesystem on .* is dirty/Filesystem auto-fix is dirty/' > $TS_OUTPUT 2>&1
echo $? >> $TS_OUTPUT
md5sum $img | sed 's/  .*auto-fix/ auto-fix/' >> $TS_OUTPUT
ts_finalize_subtest

ts_init_subtest "bug.773892"
"$TS_CMD_FSCKMINIX" "$TS_SELF/debian.bug.773892" > $TS_OUTPUT 2>&1
echo $? >> $TS_OUTPUT
ts_finalize_subtest

ts_init_subtest "broken-root"
"$TS_CMD_FSCKMINIX" "$TS_SELF/broken-root" > $TS_OUTPUT 2>&1
echo $? >> $TS_OUTPUT
ts_finalize_subtest

img="$TS_OUTDIR/${TS_TESTNAME}.badlist"
echo "42" > $img
check_minix_fs_type 'check-blocks' "-l $img -c"
rm -f $img

ts_finalize
