pthbs

Packaging Through Hashed Build Scripts
git clone https://ccx.te2000.cz/git/pthbs
Log | Files | Refs | Submodules | README

pthbs-banginstall (2635B)


      1 #!/command/zsh
      2 setopt no_unset extended_glob
      3 
      4 typeset -g vd  # versions dir, eg: /home/ccx/versions
      5 typeset -g pvd # package versions dir, eg:
      6 # /home/ccx/versions/pthbs-banginstall.d76..99a 
      7 typeset -g pdd # package destdir, eg:
      8 # /home/ccx/git/pthbs/work/builddir.4253/destdir/home/ccx/versions/pthbs-banginstall.d76...99a 
      9 
     10 typeset -g hl_fatal hl_reset
     11 if (( ${terminfo[colors]:-0} >= 8 )); then
     12 	hl_fatal='%F{red}%B'; hl_fatal=${(%)hl_fatal}
     13 	hl_reset='%b%f'; hl_reset=${(%)hl_reset}
     14 fi
     15 
     16 # helper that prints out stack, error message and exits
     17 die_ret() {
     18 	set +x
     19         local ret n
     20         ret=$1
     21         shift
     22         print -r - >&2 "${hl_fatal}Fatal$hl_reset error occurend in:"
     23         for n in {${#funcfiletrace}..1}; do
     24                 printf >&2 '%d> %s (%s)\n' $n "$funcfiletrace[$n]" "$functrace[$n]"
     25         done
     26         printf >&2 '%s\n' "${hl_fatal}*$hl_reset $^@"
     27         exit $ret
     28 }
     29 
     30 die100() {  # 100: wrong usage
     31 	set +x
     32         die_ret 100 "$@"
     33 }
     34 die111() {  # 111: system call failed
     35 	set +x
     36         die_ret 111 "$@"
     37 }
     38 
     39 main() {
     40 	pdd="$@[-1]"  # package destdir
     41 	[[ -d $pdd ]] || die111 "Not a valid destdir: ${(qqq)pdd}"
     42 	(( $+pthbs_destdir )) || die100 "Variable pthbs_destdir is not defined"
     43 	pdd=${${pdd%%/}//\/\//\/}
     44 	[[ $pdd == $pthbs_destdir/* ]] || die100 "Package destdir ${(qqq)pdd} not in pthbs_destdir ${(qqq)pthbs_destdir}"
     45 	pvd=${pdd##$pthbs_destdir} # package versioned dir
     46 	[[ $pdd == $pthbs_destdir$pvd ]] || die_ret 3 "Internal error"
     47 	vd=${pvd:h}
     48 	local cmd
     49 	for cmd in "$@[1,-2]"; do
     50 		write_command $cmd
     51 	done
     52 }
     53 
     54 write_command() {
     55 	(( $# == 1 )) || exit 2
     56 	local script_file shebang interpreter rest
     57 	script_file=$1
     58 	[[ -x $script_file ]] || die111 "${(qqq)script_file} is not an executable script"
     59 	read <$script_file shebang rest
     60 	[[ $shebang == '#!/'* ]] || die111 "${(qqq)script_file} does not start with '#!/'"
     61 	if ! (($+commands[${shebang:t}])); then
     62 		die111 "Could not find executable for ${(qqq)shebang:t} in ${(qqq)script_file}"
     63 	fi
     64 	interpreter=${commands[${shebang:t}]:P}
     65 	[[ $interpreter == ${pvd:h}/* ]] || \
     66 		die111 "The executable for ${(qqq)${shebang:t}} is at ${(qqq)commands[${shebang:t}]:P} which is outside ${(qqq)vd}"
     67 	mkdir -p $pdd/command $pdd/deps/command || exit $?
     68 	ln -sf $interpreter $pdd/deps/command/${shebang:t} || exit $?
     69 	[[ -e $pdd/command/${script_file:t} ]] && \
     70 		die111 "Target file already exists: $pdd/command/${script_file:t}"
     71 	cp -a $script_file $pdd/command/${script_file:t} || exit $?
     72 	new_sb='#!'$pvd/deps/command/${shebang:t} awk '
     73 	NR==1 { sub("^[^ \t]+", ENVIRON["new_sb"]) } 
     74 	1
     75 	' >$pdd/command/${script_file:t} <$script_file
     76 }
     77 
     78 main "$@"