#!/bin/sh
# Copyright (C) 2015 - 2018 FARGOS Development, LLC
# Convenience script to replace object tree and/or result tree with
# a temporary filesystem
# $Id$
enable=1
doObj=1
doArch=0

while test $# -gt 0
do
	case $1 in
	-h | -help | --help)
		printf "usage: %s [{--enable | --disable}] --[no]arch --[no]obj\n" "${0}" >&2
		printf "  --enable = do mount (default); --disable = request unmount\n" >&2
		printf '  --arch = affect $ST/$SB_TARGET_ARCH\n' >&2
		printf '  --obj = affect $STO (default)\n' >&2
		exit 1
		;;
	-enable | --enable)
		enable=1
		;;
	-disable | --disable)
		enable=0
		;;
	-arch | --arch)
		doArch=1
		;;
	-noarch | --noarch)
		doArch=0
		;;
	-obj | --obj)
		doObj=1
		;;
	-noobj | --noobj)
		doObj=0
		;;
	-*)
		printf "%s: unrecognized option \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	*)
		printf "%s: unrecognized argument \"%s\"\n" "${0}" "${1}" >&2
		exit 1
		;;
	esac
	shift
done

if test -z "${STO}"
then
	printf "%s: the STO environment variable is not set -- did you forget to do a workon?" "${0}" >&2
	exit 1
fi

if test ${enable} -eq 0
then
	if test ${doObj} -eq 1
	then
		printf "Request unmount of ${STO}\n"
		printf "Requires root privileges, you will be prompted for your password now..\n"
		sudo umount "${STO}"
	fi
	if test ${doArch} -eq 1
	then
		printf "Request unmount of ${ST}/${SB_TARGET_ARCH}"
		printf "Requires root privileges, you will be prompted for your password now..\n"
		sudo umount "${ST}/${SB_TARGET_ARCH}"
	fi
	exit
fi
# has to be enabled

myID=`id -u`

if test ${doObj} -eq 1
then
	desiredSize="7G"
	printf "Request tmpfs mounted at ${STO}\n"
	printf "Requires root privileges, you will be prompted for your password now..\n"
	mkdir -p "${STO}"
	sudo mount -t tmpfs -o size=${desiredSize},uid=${myID} tmpfs "${STO}"
fi

if test ${doArch} -eq 1
then
	desiredSize="5G"
	printf "Request tmpfs mounted at ${ST}/${SB_TARGET_ARCH}\n"
	printf "Requires root privileges, you will be prompted for your password now..\n"
	mkdir -p "${ST}/${SB_TARGET_ARCH}"
	sudo mount -t tmpfs -o size=${desiredSize},uid=${myID} tmpfs "${ST}/${SB_TARGET_ARCH}"
fi
