galeb

Једноставна статичка дистрибуција заснована на musl-у
git clone https://git.sr.ht/~strahinja/galeb
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

fun.sh (10883B)


      1 #!/bin/sh
      2 
      3 norm=$(printf     '\033[0m')
      4 bold=$(printf     '\033[1m')
      5 faint=$(printf    '\033[2m')
      6 black=$(printf    '\033[30m')
      7 green=$(printf    '\033[32m')
      8 gray=$(printf     '\033[37m')
      9 bgred=$(printf    '\033[41m')
     10 lred=$(printf     '\033[91m')
     11 yellow=$(printf   '\033[93m')
     12 lblue=$(printf    '\033[94m')
     13 white=$(printf    '\033[97m')
     14 bgyellow=$(printf '\033[103m')
     15 bgwhite=$(printf  '\033[107m')
     16 
     17 
     18 g_find()
     19 {
     20 	if [ "${HOST_FIND}" -eq 1 ]; then
     21 		find $*
     22 	else
     23 		/${TLD}/bin/find $*
     24 	fi
     25 }
     26 
     27 g_ln()
     28 {
     29 	if [ "${HOST_COREUTILS}" -eq 1 ]; then
     30 		ln $*
     31 	else
     32 		/${TLD}/bin/ln $*
     33 	fi
     34 }
     35 
     36 g_shasum()
     37 {
     38 	if [ "${HOST_SHASUM}" -eq 1 ]; then
     39 		sha512sum $*
     40 	else
     41 		/${TLD}/bin/sha512sum $*
     42 	fi
     43 }
     44 
     45 g_mv()
     46 {
     47 	if [ "${HOST_COREUTILS}" -eq 1 ]; then
     48 		mv $*
     49 	else
     50 		/${TLD}/bin/mv $*
     51 	fi
     52 }
     53 
     54 g_rm()
     55 {
     56 	if [ "${HOST_COREUTILS}" -eq 1 ]; then
     57 		rm $*
     58 	else
     59 		/${TLD}/bin/rm $*
     60 	fi
     61 }
     62 
     63 g_sed()
     64 {
     65 	if [ "${HOST_SED}" -eq 1 ]; then
     66 		sed $*
     67 	else
     68 		/${TLD}/bin/sed $*
     69 	fi
     70 }
     71 
     72 g_tar()
     73 {
     74 	if [ "${HOST_TAR}" -eq 1 ]; then
     75 		tar $*
     76 	else
     77 		/${TLD}/bin/tar $*
     78 	fi
     79 }
     80 
     81 apply_patch()
     82 {
     83 	local substepid pat
     84 
     85 	substepid=$1
     86 	shift
     87 	eval pat=\$$#
     88 
     89 	if [ -f "${G_PAT}/${pat##*/}.${substepid}.applied" ]; then
     90 		msg info "Patch %s applied" ${pat}
     91 		return
     92 	fi
     93 
     94 	msg info "Patch %s" "$*"
     95 	setup_fifo
     96 	if patch $* >${G_FIFO} 2>&1; then
     97 		: > ${G_PAT}/${pat##*/}.${substepid}.applied
     98 	else
     99 		msg err "Patch %s failed" "$*"
    100 		sh
    101 		exit 1
    102 	fi
    103 	rm ${G_FIFO}
    104 }
    105 
    106 begin_step()
    107 {
    108 	stepname=$1
    109 	msg info "${bgwhite}${black}[%d/%d]: %s" \
    110 		${step} ${totalsteps} "${stepname}"
    111 	printf "[%d/%d]: %s\n" ${step} ${totalsteps} "${stepname}" \
    112 		>>${G_SHORTLOG}
    113 }
    114 
    115 begin_substep()
    116 {
    117 	pkg=$1
    118 	shift
    119 	substepname="$*"
    120 	if [ -n "${substepname}" ]; then
    121 		msg info "${white}[%d/%d]-[%d/%d]: %s" \
    122 			${step} ${totalsteps} \
    123 			${substep} ${totalsubsteps} "${substepname}"
    124 		printf "[%d/%d]-[%d/%d]: %s\n" \
    125 			${step} ${totalsteps} \
    126 			${substep} ${totalsubsteps} "${substepname}" \
    127 			>>${G_SHORTLOG}
    128 	else
    129 		msg info "${white}[%d/%d]-[%d/%d]: %s" \
    130 			${step} ${totalsteps} \
    131 			${substep} ${totalsubsteps} ${pkg}
    132 		printf "[%d/%d]-[%d/%d]: %s\n" \
    133 			${step} ${totalsteps} \
    134 			${substep} ${totalsubsteps} ${pkg} >>${G_SHORTLOG}
    135 	fi
    136 }
    137 
    138 cd_or_fail()
    139 {
    140 	local dir=$1
    141 
    142 	msg info "cd %s" "${dir}"
    143 	if ! cd ${dir}; then
    144 		msg err "Cannot cd to %s" "${dir}"
    145 		exit 1
    146 	fi
    147 }
    148 
    149 check_dir()
    150 {
    151 	if [ -d "$1" ]; then
    152 		#msg info "Directory %s exists" "$1"
    153 		return
    154 	fi
    155 	msg info "Creating %s" "$1"
    156 	mkdir -p "$1"
    157 }
    158 
    159 check_mknod()
    160 {
    161 	local name type major minor
    162 
    163 	name=$1
    164 	shift
    165 	type=$1
    166 	shift
    167 	major=$1
    168 	shift
    169 	minor=$1
    170 	shift
    171 	if [ -e "${name}" ]; then
    172 		#msg info "Node %s exists" "${name}"
    173 		return
    174 	fi
    175 	msg info "Creating %s" "${name}"
    176 	mknod $* ${name} ${type} ${major} ${minor}
    177 }
    178 
    179 cp_or_fail()
    180 {
    181 	msg info "cp %s" "$*"
    182 	if ! cp $*; then
    183 		msg err "Cannot cp %s" "$*"
    184 		exit 1
    185 	fi
    186 }
    187 
    188 end_step()
    189 {
    190 	msg info "${bgwhite}${black}[%d/%d]: Done %s" \
    191 		${step} ${totalsteps} "${stepname}"
    192 	step=$((step+1))
    193 	#printf "[%d/%d]: Done %s\n" ${step} ${totalsteps} "${stepname}" \
    194 	#	>>${G_SHORTLOG}
    195 }
    196 
    197 end_substep()
    198 {
    199 	if [ -n "${substepname}" ]; then
    200 		msg info "${white}[%d/%d]-[%d/%d]: Done %s" \
    201 			${step} ${totalsteps} \
    202 			${substep} ${totalsubsteps} "${substepname}"
    203 		#printf "[%d/%d]-[%d/%d]: Done %s\n" \
    204 		#	${step} ${totalsteps} \
    205 		#	${substep} ${totalsubsteps} "${substepname}" \
    206 		#	>>${G_SHORTLOG}
    207 		unset substepname
    208 	else
    209 		msg info "${white}[%d/%d]-[%d/%d]: Done %s" \
    210 			${step} ${totalsteps} \
    211 			${substep} ${totalsubsteps} ${pkg}
    212 		#printf "[%d/%d]-[%d/%d]: Done %s\n" \
    213 		#	${step} ${totalsteps} \
    214 		#	${substep} ${totalsubsteps} ${pkg} >>${G_SHORTLOG}
    215 	fi
    216 	substep=$((substep+1))
    217 }
    218 
    219 exit_cleanup()
    220 {
    221 	[ -p ${G_FIFO} ] && rm ${G_FIFO}
    222 	unset CT_TRI GR_TRI GHOME GUID GGID GR CTD FLD PTD TLD CT TL G_ARCH
    223 	unset G_ARCH_EXT G_ARCH_EXT_ALT G_FIFO G_LGL G_LOG G_FIL G_LIB G_LOGFILE
    224 	unset G_PAT G_PKG G_SHORTLOG G_SPKG G_SRC G_TARGET G_VER JN
    225 }
    226 
    227 extract_pkg()
    228 {
    229 	local prog parent_dir=$1 dir=$2 tarball=$3 realdir=$4 touch=$5
    230 	local mopt
    231 
    232 	cd_or_fail ${parent_dir}
    233 	if [ -n "${realdir}" ] && [ -d "${realdir}" ]; then
    234 		#msg info "Directory %s exists" "${realdir}"
    235 		return
    236 	elif [ -z "${realdir}" ] && [ -n "${dir}" ] && [ -d "${dir}" ]; then
    237 		#msg info "Directory %s exists" "${dir}"
    238 		return
    239 	fi
    240 	msg info "Extract %s" "${tarball}"
    241 	case "${tarball}" in
    242 		*.tar.gz|*.tgz)		prog='gunzip -dc';;
    243 		*.tar.bz2|*.tbz2)	prog='bzip2 -dc';;
    244 		*.tar.xz|*.txz)		prog='xz -dc';;
    245 		*)			msg err "Unsupported archive format"
    246 					exit 1;;
    247 	esac
    248 	mopt=''
    249 	if [ "${touch}" = "touch" ]; then
    250 		mopt=-m
    251 	fi
    252 	if ! ${prog} "${G_PKG}/${tarball}" | g_tar -x -v ${mopt} -f -
    253 	then
    254 		msg err "Extracting %s failed" "${tarball}"
    255 		exit 1
    256 	fi
    257 	if [ -n "${realdir}" ]; then
    258 		mv_or_fail "${dir}" "${realdir}"
    259 	fi
    260 }
    261 
    262 fetch_pkg()
    263 {
    264 	local pkg=${1##*/} realpkg=$2 delay=$3
    265 
    266 	if [ -n "${realpkg}" ] && [ -f "${G_PKG}/${realpkg}" ]; then
    267 		#msg info "Package %s exists" "${realpkg}"
    268 		return
    269 	elif [ -f "${G_PKG}/${pkg}" ]; then
    270 		#msg info "Package %s exists" "${pkg}"
    271 		return
    272 	fi
    273 	cd_or_fail ${G_PKG}
    274 	msg info "wget %s" "$1"
    275 	if ! wget -c $1; then
    276 		msg err "Fetching package %s failed" "${pkg}"
    277 		exit 1
    278 	fi
    279 	if [ -n "${realpkg}" ]; then
    280 		mv_or_fail "${pkg}" "${realpkg}"
    281 	fi
    282 	msg info "Rate limiting delay"
    283 	sleep ${delay}
    284 }
    285 
    286 git_clone()
    287 {
    288 	local dir=$1 url=$2 delay=$3 repo
    289 
    290 	repo=${url##*/}
    291 
    292 	if [ -d "${G_SRC}/${dir}" ]; then
    293 		#msg info "Directory %s exists" "${dir}"
    294 		return
    295 	fi
    296 	cd_or_fail ${G_SRC}
    297 	msg info "git clone %s" "${url}"
    298 	setup_fifo
    299 	if ! git clone ${url} >${G_FIFO} 2>&1; then
    300 		msg err "git clone %s failed" "${url}"
    301 		exit 1
    302 	fi
    303 	mv_or_fail "${repo%%.git}" "${dir}"
    304 	rm ${G_FIFO}
    305 	msg info "Rate limiting delay"
    306 	sleep ${delay}
    307 }
    308 
    309 link_if_ne()
    310 {
    311 	eval dest=\$$#
    312 	if [ -e "${dest}" ]; then
    313 		#msg info "Symlink %s exists" "${dest}"
    314 		return
    315 	fi
    316 	msg info "Symlinking %s" "${dest}"
    317 	if ! g_ln -s $*; then
    318 		msg err "Symlinking %s failed" "${dest}"
    319 		exit 1
    320 	fi
    321 }
    322 
    323 mkpk()
    324 {
    325 	local pkg=$1
    326 # MKPK should be a script which should define two functions:
    327 #
    328 # 1. mkpk_name, which should print out the versioned name of the archive to be
    329 #    created. This function is called in the subdirectory "build" under the
    330 #    parent directory of the source tree.
    331 #
    332 # 2. mkpk_install, which should contain the commands to configure, build and
    333 #    install the program. This function is called in the base directory of the
    334 #    source tree.
    335 #
    336 #    Examples of commands to be used in mkpk_install:
    337 #
    338 #       make --prefix=../build/usr install
    339 #       PREFIX=../build/usr/local make install
    340 
    341 	(
    342 	local fail=0
    343 
    344 	if test ! -f MKPK; then
    345 		msg err "mkpk called without MKPK"
    346 		exit 1
    347 	fi
    348 
    349 	cd ..
    350 	mkdir -p bin build dev etc include libexec local plan9 share sucks
    351 	mkdir -p tmp tools
    352 
    353 	bm()
    354 	{
    355 		local dir=$1
    356 		mount -B /${dir} ./${dir}
    357 	}
    358 
    359 	rbm()
    360 	{
    361 		local dir=$1
    362 		mount -B /${dir} ./${dir}
    363 		mount -o remount,bind,ro /${dir} ./${dir}
    364 	}
    365 
    366 	msg info "mkpk mounting..."
    367 	rbm bin
    368 	rbm dev
    369 	rbm etc
    370 	rbm include
    371 	rbm lib
    372 	rbm libexec
    373 	rbm local
    374 	rbm plan9
    375 	rbm share
    376 	rbm sucks
    377 	bm tmp
    378 	rbm tools
    379 	g_ln -s . usr
    380 	cd ${pkg}
    381 	:>../timestamp
    382 
    383 	msg info "mkpk chrooting..."
    384 	. ./MKPK
    385 	chroot .. sh -c "cd ${pkg}; . ./MKPK; mkpk_install"
    386 	fail=$?
    387 	if [ ${fail} -ne 0 ]; then
    388 		echo Failed
    389 		sh
    390 	else
    391 		legalfiles=
    392 		for path in \
    393 			LEGAL LICENSE License Artistic \
    394 			COPYING Copying COPYRIGHT \
    395 			doc/LEGAL doc/LICENSE doc/License doc/Artistic \
    396 			doc/COPYING doc/Copying doc/COPYRIGHT
    397 		do
    398 			if [ $(find . -path "./${path}*"  2>/dev/null | \
    399 				wc -l) -gt 0 ]
    400 			then
    401 				legalfiles+=" ${path}"
    402 			fi
    403 		done
    404 
    405 		if [ -n "${legalfiles}" ]; then
    406 			check_dir ../build/${G_LGL}/${pkg}
    407 			for lfile in ${legalfiles}
    408 			do
    409 				if [ -n "${lfile}" ]; then
    410 				cp_or_fail -r "${lfile}*" \
    411 					../build/${G_LGL}/${pkg}/
    412 				fi
    413 			done
    414 		fi
    415 	fi
    416 
    417 	msg info "mkpk umounting..."
    418 	umount ../tools
    419 	umount ../tmp
    420 	umount ../sucks
    421 	umount ../share
    422 	umount ../plan9
    423 	umount ../local
    424 	umount ../libexec
    425 	umount ../lib
    426 	umount ../include
    427 	umount ../etc
    428 	umount ../dev
    429 	umount ../bin
    430 
    431 	if [ ${fail} -eq 0 ]; then
    432 		msg info "mkpk packing..."
    433 		cd ../build
    434 		g_find . \( -type b -o -type c -o -type f -o -type l \) \
    435 			-cnewer ../timestamp -printf "%p\n" \
    436 			| sort | uniq | g_tar -c -T - \
    437 			| xz -9 > ${G_SPKG}/$(mkpk_name).tar.xz \
    438 		&& cd ${G_SPKG} \
    439 		&& g_shasum $(mkpk_name).tar.xz >$(mkpk_name).sha512 \
    440 		&& { cd - >/dev/null 2>&1; }
    441 		rm ../timestamp
    442 	fi
    443 
    444 	cd ..
    445 
    446 	msg info "Cleaning up in %s" $(pwd)
    447 	rm -fr build
    448 	rm -fr sucks
    449 	rm -fr share
    450 	rm usr
    451 	rmdir bin dev etc include libexec local plan9 tmp tools 
    452 	exit ${fail}
    453 	) || {
    454 		msg err "mkpk failed"
    455 		exit 1
    456 	}
    457 }
    458 
    459 mount_or_fail()
    460 {
    461 	msg info "mount %s" "$*"
    462 	if ! mount $*; then
    463 		msg err "mount %s failed" "$*"
    464 		exit 1
    465 	fi
    466 }
    467 
    468 msg()
    469 {
    470 	local fmt icon icon_plain kind log newline tstamp
    471 
    472 	icon=
    473 	icon_plain=
    474 	kind=$1
    475 	shift
    476 	fmt=$1
    477 	shift
    478 	tstamp=$(date +%T)
    479 
    480 	log=1
    481 	newline=1
    482 	case ${kind} in
    483 		err)   icon_plain="=E="
    484 		       icon="${bold}${white}${bgred}${icon_plain}";;
    485 		info)  icon_plain="=I="
    486 		       icon="${green}${icon_plain}";;
    487 		query) icon_plain="=Q="
    488 		       icon="${bold}${yellow}${icon_plain}"
    489 		       log=0
    490 		       newline=0;;
    491 		warn)  icon_plain="=W="
    492 		       icon="${bgyellow}${black}${icon_plain}";;
    493 		*)     ;;
    494 	esac
    495 	printf "%s %s " "${icon}" ${tstamp}
    496 	if [ -e "${G_LOGFILE}" ]; then
    497 		printf "%s %s " "${icon_plain}" ${tstamp} >>${G_LOGFILE}
    498 	fi
    499 	printf "${fmt}${norm}" "$@"
    500 	if [ ${newline} -eq 1 ]; then
    501 		echo
    502 	else
    503 		printf ' '
    504 	fi
    505 	if [ -e "${G_LOGFILE}" ] && \
    506 		[ ${log} -eq 1 ]; then
    507 		printf "$(echo "${fmt}" | \
    508 			sed 's/'$(printf '\033')'\[[0-9]\+m//g')\n" \
    509 				"$@" >>${G_LOGFILE}
    510 	fi
    511 }
    512 
    513 mv_or_fail()
    514 {
    515 	msg info "mv %s" "$*"
    516 	if ! mv $*; then
    517 		msg err "Cannot mv"
    518 		exit 1
    519 	fi
    520 }
    521 
    522 remove_dir()
    523 {
    524 	msg info "Removing directory %s" "$1"
    525 	if [ -d "$1" ]; then
    526 		g_rm -fr "$1"
    527 	fi
    528 }
    529 
    530 remove_file()
    531 {
    532 	msg info "Removing %s" "$1"
    533 	if [ -e "$1" ] || [ -L "$1" ]; then
    534 		g_rm -f "$1"
    535 	fi
    536 }
    537 
    538 run_configure()
    539 {
    540 	local location=$1
    541 	shift
    542 	msg info "%s/configure %s" "${location}" "$*"
    543 	setup_fifo
    544 	if ! ${location}/configure $* >${G_FIFO} 2>&1; then
    545 		msg err "%s/configure %s failed" "${location}" "$*"
    546 		exit 1
    547 	fi
    548 	rm ${G_FIFO}
    549 }
    550 
    551 run_do()
    552 {
    553 	msg info "do %s" "$*"
    554 	setup_fifo
    555 	if ! ./do $* >${G_FIFO} 2>&1; then
    556 		msg err "do %s failed" "$*"
    557 		exit 1
    558 	fi
    559 	rm ${G_FIFO}
    560 }
    561 
    562 run_make()
    563 {
    564 	msg info "make %s" "$*"
    565 	setup_fifo
    566 	if ! make $* >${G_FIFO} 2>&1; then
    567 		msg err "make %s failed" "$*"
    568 		exit 1
    569 	fi
    570 	rm ${G_FIFO}
    571 }
    572 
    573 setup_fifo()
    574 {
    575 	mkfifo ${G_FIFO}
    576 	tee -a ${G_LOGFILE} < ${G_FIFO} &
    577 }
    578 
    579 unpack_pkg()
    580 {
    581 	local pkg=$1
    582 	shift
    583 	dir=${G_SPKG}
    584 	case "${pkg}" in
    585 		*/*)	dir=${dir}/${pkg%/*};;
    586 		*)	;;
    587 	esac
    588 	cd_or_fail ${dir}
    589 	if ! g_shasum -c ${pkg##*/}.sha512 >/dev/null 2>&1; then
    590 		msg err "sha512sum -c %s failed" ${dir}/${pkg##*/}.tar.xz
    591 		exit 1
    592 	fi
    593 	cd_or_fail $1
    594 	msg info "Unpacking %s" ${G_SPKG}/${pkg}.tar.xz
    595 	xz -dc ${G_SPKG}/${pkg}.tar.xz | tar -xf -
    596 }