summaryrefslogtreecommitdiff
path: root/functions/stagefile.sh
blob: 40a1e2518d7b65f722e9738968dd03e23660c5cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/bin/sh

## live-build(7) - System Build Scripts
## Copyright (C) 2016-2020 The Debian Live team
## Copyright (C) 2006-2015 Daniel Baumann <mail@daniel-baumann.ch>
##
## This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING.
## This is free software, and you are welcome to redistribute it
## under certain conditions; see COPYING for details.


# Get the default filename for a script's stagefile (the name of the script
# file itself). A suffix can be appended via providing as a param.
Stagefile_name ()
{
	local SUFFIX="${1}"
	local FILENAME
	FILENAME="$(basename $0)"
	echo ${FILENAME}${SUFFIX:+.$SUFFIX}
}

Check_stagefile ()
{
	local FILE
	local NAME
	FILE=".build/${1:-$(Stagefile_name)}"
	NAME="$(basename ${FILE})"

	# Checking stage file
	if [ -f "${FILE}" ]
	then
		if [ "${_FORCE}" != "true" ]
		then
			# Skip execution
			Echo_warning "Skipping %s, already done" "${NAME}"
			exit 0
		else
			# Force execution
			Echo_message "Forcing %s" "${NAME}"
			rm -f "${FILE}"
		fi
	fi
}

Create_stagefile ()
{
	local FILE
	local DIRECTORY
	FILE=".build/${1:-$(Stagefile_name)}"
	DIRECTORY="$(dirname ${FILE})"

	# Creating stage directory
	mkdir -p "${DIRECTORY}"

	# Creating stage file
	touch "${FILE}"
}

Remove_stagefile ()
{
	local FILE
	FILE=".build/${1:-$(Stagefile_name)}"
	rm -f "${FILE}"
}

# Ensure that all specified stagefiles exist (and thus that all associated stages are complete)
Require_stagefiles ()
{
	if [ $# -eq 0 ]; then
		Echo_warning "Bad 'Require_stagefiles' usage, no params were supplied"
		return 0
	fi

	local FILE
	local MISSING=""
	local MISSING_COUNT=0
	for FILE in ${@}; do
		if [ ! -f ".build/${FILE}" ]; then
			MISSING_COUNT=$(( $MISSING_COUNT + 1 ))
			MISSING="${MISSING:+$MISSING }${FILE}"
		fi
	done
	if [ $MISSING_COUNT -eq 0 ]; then
		return 0
	elif [ $MISSING_COUNT -eq 1 ]; then
		Echo_error "the following stage is required to be done first: %s" "${MISSING}"
	else
		Echo_error "the following stages are required to be completed first: %s" "${MISSING}"
	fi

	exit 1
}