#!/bin/sh -euf

# Copyright 2013-2014 Intel Corporation
# Author: Artem Bityutskiy
# License: GPLv2

PROG="setup-scripts-fstab"
VER="1.0"

srcdir="$(readlink -ev -- ${0%/*})"
PATH="/usr/share/setup-scripts:$srcdir:$PATH"

if [ -f "$srcdir/setup-scripts-sh-functions" ]; then
	. "$srcdir/setup-scripts-sh-functions"
	. "$srcdir/installerfw-sh-functions"
else
	.  /usr/share/setup-scripts/setup-scripts-sh-functions
	.  /usr/share/setup-scripts/installerfw-sh-functions
fi

# This is a small trick which I use to make sure my scripts are portable -
# check if 'dash' is present, and if yes - use it.
if can_switch_to_dash; then
	exec dash -euf -- "$srcdir/$PROG" "$@"
	exit $?
fi

show_usage()
{
	cat <<-EOF
Usage: $PROG [options]

Create the default Tizen /etc/fstab file.

Options:
  -f, --force    re-write /etc/fstab if it exists
  -v, --verbose  be verbose
  --version      show the program version and exit
  -h, --help     show this text and exit
EOF
}

show_usage_fail()
{
	IFS= printf "%s\n\n" "$PROG: error: $*" >&2
	show_usage >&2
	exit 1
}

# Parse the options
tmp=`getopt -n $PROG -o f,v,h --long force,verbose,version,help -- "$@"` ||
	show_usage_fail "cannot parse command-line options"
eval set -- "$tmp"

force=
verbose=
while true; do
	case "$1" in
	-f|--force)
		force="-f"
		;;
	-v|--verbose)
		verbose="-v"
		;;
	--version)
		printf "%s\n" "$VER"
		exit 0
		;;
	-h|--help)
		show_usage
		exit 0
		;;
	--) shift; break
		;;
	*) show_usage_fail "unrecognized option \"$1\""
		;;
	esac
	shift
done

if ! installerfw_available; then
	installerfw_restore_env
fi

fstab="$(installerfw_mnt_prefix "/etc/fstab")"

if [ -s "$fstab" ] && [ -z "$force" ]; then
	fatal "\"$fstab\" already exists, use -f to re-write it"
fi

installerfw_verify_defined "INSTALLERFW_PART_COUNT"

contents="# Generated by $PROG
devpts  /dev/pts  devpts  gid=5,mode=620  0 0
tmpfs   /dev/shm  tmpfs   defaults        0 0
proc    /proc     proc    defaults        0 0
sysfs   /sys      sysfs   defaults        0 0"

pnum=0
while [ "$pnum" -lt "$INSTALLERFW_PART_COUNT" ]; do
	installerfw_verify_defined "INSTALLERFW_PART${pnum}_UUID"
	installerfw_verify_defined "INSTALLERFW_PART${pnum}_MOUNTPOINT"
	installerfw_verify_defined "INSTALLERFW_PART${pnum}_FSTYPE"

	eval uuid="\${INSTALLERFW_PART${pnum}_UUID:-}"
	eval mountpoint="\${INSTALLERFW_PART${pnum}_MOUNTPOINT:-}"
	eval fsopts="\${INSTALLERFW_PART${pnum}_FSOPTS:-defaults}"
	eval fstype="\${INSTALLERFW_PART${pnum}_FSTYPE:-}"

	if [ "$fstype" = "vfat" ]; then
		# FAT FS's short UUID has to be uppercase
		uuid="$(printf "%s" "$uuid" | tr "[:lower:]" "[:upper:]")"
	else
		# Normal UUID has to be lowercase
		uuid="$(printf "%s" "$uuid" | tr "[:upper:]" "[:lower:]")"
	fi

	contents="${contents}${br}UUID=$uuid  $mountpoint  $fstype  $fsopts  0 0"

	pnum="$((pnum+1))"
done

verbose "writing the followint to \"$fstab\": $contents"

printf "%s" "$contents" > "$fstab"
