src/mulle-bootstrap-copy.sh
c81c8721
 #! /usr/bin/env bash
8f951e19
 #
 #   Copyright (c) 2017 Nat! - Mulle kybernetiK
 #   All rights reserved.
 #
 #   Redistribution and use in source and binary forms, with or without
 #   modification, are permitted provided that the following conditions are met:
 #
 #   Redistributions of source code must retain the above copyright notice, this
 #   list of conditions and the following disclaimer.
 #
 #   Redistributions in binary form must reproduce the above copyright notice,
 #   this list of conditions and the following disclaimer in the documentation
 #   and/or other materials provided with the distribution.
 #
 #   Neither the name of Mulle kybernetiK nor the names of its contributors
 #   may be used to endorse or promote products derived from this software
 #   without specific prior written permission.
 #
 #   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 #   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 #   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 #   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 #   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 #   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 #   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 #   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 #   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 #   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 #   POSSIBILITY OF SUCH DAMAGE.
 #
 MULLE_BOOTSTRAP_COPY_SH="included"
 
 
 tar_remove_extension()
 {
b8b33348
    local ext="$1"
8f951e19
 
    if [ ! -z "${ext}" ]
    then
       case "${UNAME}" in
          darwin|freebsd)
83ce7c54
             echo "-s/\.$1\$//"
b8b33348
          ;;
 
57935531
          linux|mingw)
b8b33348
             echo "--transform s/\.$1\$//"
8f951e19
          ;;
 
          *)
83ce7c54
             echo "--transform /\.$1\$//"
8f951e19
          ;;
       esac
    fi
 }
cf2eebc9
 
 
8f951e19
 #
 # dstdir need not exist
 # srcdir must exist
 # ext can be empty
151c6f06
 # noclobber can be empty=NO,NO or YES
8f951e19
 #
151c6f06
 _archive_files()
 {
    local srcdir="$1"
    local ext="$2"
1ca4c8bc
    local taroptions="$3"
151c6f06
 
    (
       exekutor cd "${srcdir}" ;
       if [ -z "${ext}" ]
       then
          exekutor find . \( -type f -a ! -name "*.*" \) -print
       else
          exekutor find . \( -type f -a -name "*.${ext}" \) -print
       fi |
          exekutor tar -c ${taroptions} -f - -T -
    ) || exit 1
 }
 
 
 
 _unarchive_files()
 {
    local dstdir="$1"
    local noclobber="$2"
 
1ca4c8bc
    [ -d "${dstdir}" ] || fail "${dstdir} does not exist"
c81c8721
 
151c6f06
    (
       exekutor cd "${dstdir}" ;
       if [ "${noclobber}" = "NO" ]
       then
          exekutor tar -x ${TARFLAGS} -f -
       else
          exekutor tar -x ${TARFLAGS} -k -f -
1ca4c8bc
       fi
c81c8721
       :  # ignore trashy tar rval
    )  2> /dev/null
151c6f06
 }
 
 
83ce7c54
 _copy_files()
cf2eebc9
 {
83ce7c54
    local taroptions="$1" ; shift
    local dstdir="$1" ; shift
    local srcdir="$1" ; shift
    local ext="$1" ; shift
151c6f06
    local noclobber="${1:-YES}" ; shift
8f951e19
 
    [ -d "${srcdir}" ] || internal_fail "${srcdir} does not exist"
    [ -d "${dstdir}" ] || internal_fail "${dstdir} does not exist"
 
    if [ -z "${ext}" ]
    then
       log_fluff "Copying extensionless files from \"${srcdir}\" to \"${dstdir}\""
    else
       log_fluff "Copying .${ext} files from \"${srcdir}\" to \"${dstdir}\""
    fi
 
    #
    # copy over files only, let tar remove extension
    #
1ca4c8bc
    _archive_files "${srcdir}" "${ext}" "${taroptions}" | _unarchive_files "${dstdir}" "${noclobber}"
8f951e19
 }
cf2eebc9
 
 
83ce7c54
 copy_files_stripping_last_extension()
 {
    local ext="$3"
    local lastext="`echo "${ext}" | sed 's/\.[^.]*$//'`"
 
    _copy_files "`tar_remove_extension "${lastext}"`" "$@"
 }
 
 
 copy_files_keeping_extension()
 {
    _copy_files "" "$@"
 }
 
 
 
8f951e19
 #
 # dstdir need not exist
 # srcdir must exist
 #
 inherit_files()
 {
151c6f06
    local dstdir="$1"
    # local srcdir="$2"
    # local ext="$3"
    # local noclobber="$4"
cf2eebc9
 
8f951e19
    dstdir="$1"
cf2eebc9
 
83ce7c54
    [ $# -eq 2 ] || internal_fail "parameter error"
 
8f951e19
    mkdir_if_missing "${dstdir}"
cf2eebc9
 
8f951e19
    # prefer to copy os-specific first, "-k" won't overwrite
83ce7c54
    copy_files_stripping_last_extension "$@" "${UNAME}" "YES" || fail "copy"
cf2eebc9
 
8f951e19
    # then to copy generic, again "-k" won't overwrite
83ce7c54
    copy_files_keeping_extension "$@" "" "YES" || fail "copy"
151c6f06
 }
 
 
 inherit_scripts()
 {
    local dstdir="$1"
    # local srcdir="$2"
    # local ext="$3"
    # local noclobber="$4" default YES
 
    dstdir="$1"
 
    [ $# -eq 2 ] || internal_fail "parameter error"
 
    mkdir_if_missing "${dstdir}"
 
    # prefer to copy os-specific first, "-k" won't overwrite
    copy_files_stripping_last_extension "$@" "sh.${UNAME}" "YES" || fail "copy"
 
    # then to copy generic, again "-k" won't overwrite
83ce7c54
    copy_files_keeping_extension "$@" "sh" "YES" || fail "copy"
cf2eebc9
 }
 
 
8f951e19
 #
 # dstdir need not exist
 # srcdir must exist
 #
 override_files()
cf2eebc9
 {
151c6f06
    local dstdir="$1"
    local srcdir="$2"
    local ext="$3"
    # local noclobber="$4"
cf2eebc9
 
151c6f06
    [ $# -eq 2 ] || internal_fail "parameter error"
 
    mkdir_if_missing "${dstdir}"
 
    # first copy generic, clobber what's there
    copy_files_keeping_extension "${dstdir}" "${srcdir}" "" "NO"      || fail "copy"
 
    # then copy os-specific to clobber generics
    copy_files_stripping_last_extension "${dstdir}" "${srcdir}" "${UNAME}" "NO" || fail "copy"
 }
 
 
 override_scripts()
 {
    local dstdir="$1"
    # local srcdir="$2"
    # local ext="$3"
    # local noclobber="$4"
cf2eebc9
 
83ce7c54
    [ $# -eq 2 ] || internal_fail "parameter error"
 
8f951e19
    mkdir_if_missing "${dstdir}"
cf2eebc9
 
8f951e19
    # first copy generic, clobber what's there
83ce7c54
    copy_files_keeping_extension "$@" "sh" || fail "copy"
8f951e19
 
    # then copy os-specific to clobber generics
83ce7c54
    copy_files_stripping_last_extension "$@" "sh.${UNAME}" || fail "copy"
cf2eebc9
 }
 
8f951e19
 # make sure functions are present
 
 copy_initialize()
cf2eebc9
 {
8f951e19
    [ -z "${MULLE_BOOTSTRAP_LOGGING_SH}" ] && . mulle-bootstrap-logging.sh
cf2eebc9
 
9339a58a
    log_debug ":copy_initialize:"
cf2eebc9
 
8f951e19
    [ -z "${MULLE_BOOTSTRAP_FUNCTIONS_SH}" ] && . mulle-bootstrap-functions.sh
cf2eebc9
 }
8f951e19
 
 copy_initialize