summaryrefslogtreecommitdiff
path: root/setup
blob: 1c60151ccdbc090ac49c294dab665939cff25f0e (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/bin/bash

TEMPLATE_DIR="templates"

TMP_DIR_PREFIX="$(pwd)/tmp"
rm -rf "${TMP_DIR_PREFIX}"/* # ?? Could be vulnerable

if [ -z "${DISTRO_TC}" ]
then
	DISTRO_TC=x86_64-x221e-linux-gnu
fi

if [ -z "${CROSS_TC}" ]
then
	CROSS_TC=${HOME}/opt/cross
fi

if [ -z "${CC}" ]
then
	CC=x86_64-x221e-linux-gnu-gcc
fi

if [ -z "${MASTERDIR}" ]
then
	MASTERDIR="$(pwd)/bootstrap"
fi

mkdir -p "${MASTERDIR}"
touch "${MASTERDIR}/packages"

if [ -z "${CROSS_ENV_ROOT}" ]
then
	CROSS_ENV_ROOT="${HOME}/opt/cross/"
fi

MIRROR_GNU_FTP="https://ftp.gnu.org/gnu/"

function download_source {
	upstream="$1"
	out_name="$2"
	
	curl -L --output-dir=${TMP_DIR_PREFIX} "${upstream}" -o "${out_name}"
	
	if [ $? -ne 0 ]
	then
		echo -e "error: failed to download '${out_name}' from '${upstream}'"
	fi
}

function unpack_source {
	file="$1"

	currdir="$(pwd)"
	
	cd "${TMP_DIR_PREFIX}"

	if [[ ! -e "${file}" || ! -f "${file}" ]]
	then
		echo -e "error: unpack_source: file '${file}' does not exist."
		exit 1
	fi

	tar -xf "${file}"
	
	if [ $? -ne 0 ]
	then
		echo -e "error: unpack_source: tar failed with error code: ${?}"
	fi
	
	cd "${currdir}"
}

function handle_configure {
	pkg_dir="$1"

	if [[ "$(type -t 'do_configure')" == "function" ]]
	then
		if [[ ! -e "${pkg_dir}" || ! -d "${pkg_dir}" ]]
		then
			echo -e "error: pkg_install: could not find: '${pkg_dir}'"
			exit 1
		fi
		
		cd "${pkg_dir}"
		
		do_configure

		cd "${currdir}"
	fi	

}

function handle_build {
	pkg_dir="$1"

	if [[ "$(type -t 'do_build')" == "function" ]]
	then
		if [[ ! -e "${pkg_dir}" || ! -d "${pkg_dir}" ]]
		then
			echo -e "error: pkg_install: could not find: '${pkg_dir}'"
			exit 1
		fi
		
		cd "${pkg_dir}"

		do_build

		cd "${currdir}"
	fi

}

function handle_install {
	pkg_dir="$1"

	if [[ "$(type -t 'do_install')" == "function" ]]
	then
		if [[ ! -e "${pkg_dir}" || ! -d "${pkg_dir}" ]]
		then
			echo -e "error: pkg_install: could not find: '${pkg_dir}'"
			exit 1
		fi
		
		cd "${pkg_dir}"

		do_install	

		cd "${currdir}"
	fi
}

function pkg_install {
	pkg="$1"

	pkg_file="${TEMPLATE_DIR}/${pkg}/template"

	if [[ ! -e "${pkg_file}" || ! -f "${pkg_file}" ]]
	then
		echo -e "error: template '${pkg_file}' does not exist!"
		exit 1
	fi

	currdir=$(pwd)
	source "${pkg_file}"

	if [ "${#dependson[@]}" -gt 0 ]
	then
		for i in "${dependson[@]}"
		do
			if [[ "$(sed -n /${i}/p ${MASTERDIR}/packages)" != "$i" ]]
			then
				echo -e "'${i}' must be installed before you can install '${pkgname}'"
				exit 1
			fi
		done
	fi

	pkg_dir_name="${pkgname}-${version}"
	archive_name="${pkg_dir_name}.tar"

	download_source "${upstream}" "${archive_name}"
	unpack_source "${archive_name}"

	handle_configure "${TMP_DIR_PREFIX}/${pkg_dir_name}"
	handle_build "${TMP_DIR_PREFIX}/${pkg_dir_name}"
	handle_install "${TMP_DIR_PREFIX}/${pkg_dir_name}"

	echo "${pkgname}=${version}" >> "${MASTERDIR}/packages"
}

function bootstrap {
	for i in $(ls templates/)
	do
		pkg_install "${i}"
	done
}

function requirements {
	ncurses_lib_loc="${CROSS_TC}/usr/lib/libncursesw_g.a"
	echo -e "Checking the existence of ncursesw in the cross-toolchain environment."
	if [[ ! -e "${ncurses_lib_loc}" || ! -f "${ncurses_lib_loc}" ]]
	then
		MASTERDIR="${CROSS_TC}"		
		pkg_install "ncurses"
	fi
	echo -e "requirement met: ncurses"
}

case "$1" in
	"pkg") pkg_install "${@:2}" ;;
	"bootstrap") bootstrap "${@:2}" ;;
	"requirements") requirements "${@:2}" ;;
	*) echo -e "error: invalid command" &&  exit 1 ;;
esac