rlm@1: #!/bin/bash
rlm@1: # Copyright (c) 2000-2006 Matthias S. Benkmann <article AT winterdrache DOT de>
rlm@1: # You may do everything with this code except misrepresent its origin.
rlm@1: # PROVIDED `AS IS' WITH ABSOLUTELY NO WARRANTY OF ANY KIND!
rlm@1: 
rlm@1: # This script will build a package based on the commands in $HOME/build.conf
rlm@1: # It can be called with the following parameters that
rlm@1: # will cause it to execute the respective *_commands() functions. If it is
rlm@1: # called with no parameter, that is equivalent to
rlm@1: # build unpack patch configure make check install clean
rlm@1: #
rlm@1: # It will create 8 log files in the $HOME directory:
rlm@1: #   configure.log: All messages output during configure
rlm@1: #   configure.err: Just the errors output during configure
rlm@1: #   check.log: All messages output during checking
rlm@1: #   check.err: Just the errors output during checking
rlm@1: #   make.log: All messages output during make
rlm@1: #   make.err: Just the errors output during make
rlm@1: #   install.log: All messages output during make install
rlm@1: #   install.err: Just the errors output during make install
rlm@1: #
rlm@1: # After running the script you should check the *.err files to see
rlm@1: # if any problems have occurred. If that is the case, use the corresponding
rlm@1: # *.log files to see the error messages in context.
rlm@1: 
rlm@1: build_script="$(readlink -f "$0")"
rlm@1: 
rlm@1: cd  # go HOME
rlm@1: 
rlm@1: source "$HOME"/build.conf
rlm@1: 
rlm@1: if [ "_$(whoami)" != _root ]; then
rlm@1:   export PACKAGE_OWNER="$(whoami)"
rlm@1: fi
rlm@1: 
rlm@1: 
rlm@1:  # This function auto-extracts tarballs based on PATTERNS (see build.conf) inside
rlm@1:  # the directory $HOME/xxxbuild and
rlm@1:  # cds into the fist directory created by the first tarball. This is also
rlm@1:  # stored in the variable $srcdir.
rlm@1: unpack_commands()
rlm@1: { :
rlm@1:   export srcdir=""
rlm@1:   rm -rf "$HOME/xxxbuild"
rlm@1:   mkdir -p "$HOME/xxxbuild" 
rlm@1:   cd "$HOME/xxxbuild" || return 1
rlm@1:   
rlm@1:   for p in $PATTERNS ; do
rlm@1:     for archive in "$HOME"/*"$p"* ; do
rlm@1:       dir=""
rlm@1:       if [ -f "$archive" ]; then
rlm@1:         case z"$archive" in
rlm@1:           z*.tar.bz2) dir=$(tar tjf "$archive" | grep / | head -n 1) ; tar xjf "$archive"  ;;
rlm@1:           z*.tar.gz)  dir=$(tar tzf "$archive" | grep / | head -n 1) ; tar xzf "$archive"  ;;
rlm@1:         esac
rlm@1:       fi
rlm@1:       dir=${dir##./}
rlm@1:       test -z "$dir" && echo 1>&2 "Error extracting $archive"
rlm@1:       test -z "$srcdir" && srcdir=${dir%%/*}
rlm@1:     done
rlm@1:   done
rlm@1:   
rlm@1:   test -z "$srcdir" && { echo 1>&2 "Source directory not found" ; return 1 ; }
rlm@1:   ln -s "$srcdir" yyysrc
rlm@1: }
rlm@1: 
rlm@1: clean_commands()
rlm@1: {
rlm@1:   rm -rf "$HOME/xxxbuild"
rlm@1: }
rlm@1: 
rlm@1: test_pipe()
rlm@1: {
rlm@1:   for i in "${PIPESTATUS[@]}" 
rlm@1:   do
rlm@1:     test $i != 0 && { echo FAILED! ; exit 1 ; }
rlm@1:   done
rlm@1:   echo successful!
rlm@1:   return 0
rlm@1: }
rlm@1: 
rlm@1: if [ $# -eq 0 ]; then
rlm@1:   set -- unpack patch configure make check root_pre_install install root_post_install clean
rlm@1: fi
rlm@1: 
rlm@1: while [ -n "$1" ]; do
rlm@1:   case "_$1" in
rlm@1:     _all)
rlm@1:         shift 1
rlm@1:         set -- dummy unpack patch configure make check root_pre_install install root_post_install clean "$@"
rlm@1:         ;;
rlm@1:         
rlm@1:     _unpack)
rlm@1:         echo -n Unpacking...
rlm@1: 
rlm@1:         unpack_commands # no logging for unpack necessary 
rlm@1:         test_pipe
rlm@1:         ;;
rlm@1:    
rlm@1:     _patch)
rlm@1:         cd "$HOME/xxxbuild/yyysrc" && srcdir="$(pwd)" || exit 1
rlm@1:         patch_commands # no logging for patch necessary 
rlm@1:         #test_pipe
rlm@1:         ;;
rlm@1: 
rlm@1:     _configure)
rlm@1:         cd "$HOME/xxxbuild/yyysrc" && srcdir="$(pwd)" || exit 1
rlm@1:         echo -n Configuring...
rlm@1: 
rlm@1:         { configure_commands 3>&1 1>&2 2>&3 | tee "$HOME/configure.err" ;} &>"$HOME/configure.log"
rlm@1:         test_pipe
rlm@1:         # NOTE: Simply using && instead of test_pipe would not work, because &&
rlm@1:         # only tests the exit status of the last command in the pipe, which is tee.
rlm@1:         ;;
rlm@1: 
rlm@1:     _make)
rlm@1:         cd "$HOME/xxxbuild/yyysrc" && srcdir="$(pwd)" || exit 1
rlm@1:         echo -n Building...
rlm@1: 
rlm@1:         { make_commands 3>&1 1>&2 2>&3 | tee "$HOME/make.err" ;} &>"$HOME/make.log"
rlm@1:         test_pipe
rlm@1:         ;;
rlm@1: 
rlm@1:     _check)
rlm@1:         cd "$HOME/xxxbuild/yyysrc" && srcdir="$(pwd)" || exit 1
rlm@1:         echo -n Checking...
rlm@1: 
rlm@1:         { check_commands 3>&1 1>&2 2>&3 | tee "$HOME/check.err" ;} &>"$HOME/check.log"
rlm@1:         test_pipe
rlm@1:         ;;
rlm@1:     
rlm@1:     _root_pre_install)
rlm@1:         if type root_pre_install_commands &>/dev/null ; then
rlm@1:           if [ _$(whoami) != _root ]; then
rlm@1:             su --preserve-environment root -c "HOME='$HOME' '$build_script' root_pre_install" || exit 1
rlm@1:           else  
rlm@1:             echo -n "Preparing for install(root)..."
rlm@1:   
rlm@1:             { root_pre_install_commands 3>&1 1>&2 2>&3 | tee "$HOME/preinstall.err" ;} &>"$HOME/preinstall.log"
rlm@1:             test_pipe
rlm@1:           fi  
rlm@1:         fi
rlm@1:         ;;
rlm@1:         
rlm@1:     _install)
rlm@1:         cd "$HOME/xxxbuild/yyysrc" && srcdir="$(pwd)" || exit 1
rlm@1:         echo -n Installing...
rlm@1: 
rlm@1:         { install_commands 3>&1 1>&2 2>&3 | tee "$HOME/install.err" ;} &>"$HOME/install.log"
rlm@1:         test_pipe
rlm@1:         ;;
rlm@1:     
rlm@1:     _root_post_install)
rlm@1:         if type root_post_install_commands &>/dev/null ; then
rlm@1:           if [ _$(whoami) != _root ]; then
rlm@1:             su --preserve-environment root -c "HOME='$HOME' '$build_script' root_post_install" || exit 1
rlm@1:           else  
rlm@1:             echo -n "Finishing install(root)..."
rlm@1:   
rlm@1:             { root_post_install_commands 3>&1 1>&2 2>&3 | tee "$HOME/postinstall.err" ;} &>"$HOME/postinstall.log"
rlm@1:             test_pipe
rlm@1:           fi  
rlm@1:         fi  
rlm@1:         ;;
rlm@1:         
rlm@1:     _clean)
rlm@1:         cd "$HOME"
rlm@1:         echo -n Cleaning...
rlm@1:         clean_commands
rlm@1:         echo done!
rlm@1:         ;;
rlm@1:     *)
rlm@1:         echo 1>&2 "Unknown command '$1'"
rlm@1:         exit 1
rlm@1:         ;;
rlm@1:   esac       
rlm@1:   shift 1
rlm@1: done