src/mulle-bootstrap-zombify.sh
c81c8721
 #! /usr/bin/env bash
8f951e19
 #
 #   Copyright (c) 2015 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.
c81c8721
 #
8f951e19
 MULLE_BOOTSTRAP_ZOMBIFY_SH="included"
 
 
 # What refresh does:
 #
 # 1. remove .bootstrap.auto
 # 2. recreate .bootstrap.auto (without .repos/<name>/.bootstrap)
 # 3. augment .bootstrap.auto/repositories file with contents of
 #     .repos/<name>/.bootstrap/repositories files
 # 4. augment .bootstrap.auto with other contents of .repos/<name>/.bootstrap
 #
 refresh_usage()
 {
    cat <<EOF >&2
4eff2e57
 Usage:
    ${MULLE_EXECUTABLE} <command>
8f951e19
 
4eff2e57
 Commands:
8f951e19
    refresh      : update settings, remove unused repositories (default)
    nonrecursive : ignore .bootstrap folders of fetched repositories
 EOF
    exit 1
 }
 
 
 # ----------------
 
 #
 # used to do this with chmod -h, alas Linux can't do that
 # So we create a special directory .zombies
 # and create files there
 #
 #
 # ###
 #
83ce7c54
 _zombify_stashes()
8f951e19
 {
    local reposdir
 
    reposdir="$1"
 
    [ -z "${reposdir}" ] && internal_fail "reposdir"
 
    local zombiepath
 
f1f83418
    zombiepath="${reposdir}/.zombies"
    rmdir_safer "${zombiepath}"
8f951e19
 
    if dir_has_files "${reposdir}"
    then
       mkdir_if_missing "${zombiepath}"
 
ea74a237
       exekutor cp ${COPYMOVEFLAGS} "${reposdir}/"* "${zombiepath}/" >&2
8f951e19
    fi
 }
 
 
83ce7c54
 mark_stash_as_alive()
8f951e19
 {
    local reposdir
 
    reposdir="$1"
83ce7c54
    name="$2"
8f951e19
 
83ce7c54
    [ $# -eq 2 ] || internal_fail "parameter error"
8f951e19
 
83ce7c54
    zombie="${reposdir}/.zombies/${name}"
    if [ -e "${zombie}" ]
8f951e19
    then
83ce7c54
       log_fluff "Marking \"${name}\" as alive"
8f951e19
 
ea74a237
       exekutor rm -f ${COPYMOVEFLAGS} "${zombie}" >&2 || fail "failed to delete zombie ${zombie}"
83ce7c54
    else
f1f83418
       log_fluff "\"${name}\" is alive as `absolutepath "${zombie}"` is not present"
83ce7c54
    fi
 }
8f951e19
 
 
 #
 #
 #
4805e856
 
0fc9abbe
 bury_stash()
8f951e19
 {
4805e856
    local reposdir="$1"
    local name="$2"
    local stashdir="$3"
8f951e19
 
4805e856
    local gravepath
8f951e19
 
4805e856
    gravepath="${reposdir}/.graveyard/${name}"
 
e3244671
    if [ -L "${stashdir}" ]
    then
       log_verbose "Removing old symlink \"${stashdir}\""
       exekutor rm -f ${COPYMOVEFLAGS} "${stashdir}" >&2
       return
    fi
 
4805e856
    if [ -e "${gravepath}" ]
    then
       log_fluff "Repurposing old grave \"${gravepath}\""
ea74a237
       exekutor rm -rf ${COPYMOVEFLAGS}  "${gravepath}" >&2
4805e856
    else
       mkdir_if_missing "${reposdir}/.graveyard"
    fi
 
    log_info "Burying \"${stashdir}\" in grave \"${gravepath}\""
ea74a237
    exekutor mv ${COPYMOVEFLAGS} "${stashdir}" "${gravepath}" >&2
4805e856
 }
 
 
 _bury_zombie()
 {
    local reposdir="$1"
    local zombie="$2"
8f951e19
 
    local name
    local stashdir
    local gravepath
 
4805e856
    name="`basename -- "${zombie}"`"
    stashdir="`_stash_of_reposdir_file "${reposdir}/${name}"`"
 
    if [ -L "${stashdir}"  ]
    then
       log_info "Removing unused symlink ${C_MAGENTA}${C_BOLD}${stashdir}${C_INFO}"
ea74a237
       exekutor rm ${COPYMOVEFLAGS}  "${stashdir}" >&2
4805e856
       return
    fi
 
    if [ -d "${stashdir}" ]
    then
0b9e9e49
       if ! is_minion_bootstrap_project "${stashdir}"
       then
0fc9abbe
          bury_stash "${reposdir}" "${name}" "${stashdir}"
0b9e9e49
 
          exekutor rm ${COPYMOVEFLAGS} "${zombie}" >&2
          exekutor rm ${COPYMOVEFLAGS} "${reposdir}/${name}" >&2
       else
          log_fluff "Do not bury minion \"${stashdir}\""
       fi
4805e856
    else
       log_fluff "Zombie \"${stashdir}\" vanished or never existed ($PWD)"
    fi
 }
 
 
 _bury_zombies()
 {
    local reposdir="$1"
 
    [ -z "${reposdir}" ] && internal_fail "reposdir"
 
    local zombie
    local zombiepath
 
8f951e19
    zombiepath="${reposdir}/.zombies"
 
    if dir_has_files "${zombiepath}"
    then
83ce7c54
       log_fluff "Moving zombies into graveyard"
8f951e19
 
4805e856
       for zombie in `ls -1 "${zombiepath}/"* 2> /dev/null`
8f951e19
       do
f67f6857
          if [ -f "${zombie}" ]
8f951e19
          then
4805e856
             _bury_zombie "${reposdir}" "${zombie}"
8f951e19
          fi
       done
    fi
 
    if [ -d "${zombiepath}" ]
    then
ea74a237
       exekutor rm -rf ${COPYMOVEFLAGS} "${zombiepath}" >&2
8f951e19
    fi
 }
 
 
 #
 #
 #
83ce7c54
 zombify_embedded_repository_stashes()
8f951e19
 {
    log_fluff "Marking all embedded repositories as zombies for now"
 
f1f83418
    _zombify_stashes "${EMBEDDED_REPOS_DIR}"
8f951e19
 }
 
 
83ce7c54
 zombify_repository_stashes()
8f951e19
 {
    log_fluff "Marking all repositories as zombies for now"
 
83ce7c54
    _zombify_stashes "${REPOS_DIR}"
8f951e19
 }
 
 
83ce7c54
 zombify_deep_embedded_repository_stashes()
8f951e19
 {
    local stashes
    local stash
f1f83418
    local name
8f951e19
 
    log_fluff "Marking all deep embedded repositories as zombies for now"
 
    IFS="
 "
    stashes="`all_repository_stashes "${REPOS_DIR}"`"
    for stash in ${stashes}
    do
       IFS="${DEFAULT_IFS}"
 
f1f83418
       name="`basename -- "${stash}"`"
       _zombify_stashes "${REPOS_DIR}/.deep/${name}.d"
8f951e19
    done
 
    IFS="${DEFAULT_IFS}"
 }
 
 
 #
 #
 #
 bury_embedded_repository_zombies()
 {
    log_fluff "Burying embedded zombie repositories"
 
f1f83418
    _bury_zombies "${EMBEDDED_REPOS_DIR}"
8f951e19
 }
 
 
 bury_repository_zombies()
 {
    log_fluff "Burying zombie repositories"
 
    _bury_zombies "${REPOS_DIR}"
 }
 
 
 bury_deep_embedded_repository_zombies()
 {
    local stashes
    local stash
 
    log_fluff "Burying deep embedded zombie repositories"
 
    IFS="
 "
    stashes="`all_repository_stashes "${REPOS_DIR}"`"
    for stash in ${stashes}
    do
       IFS="${DEFAULT_IFS}"
 
f1f83418
       name="`basename -- "${stash}"`"
       _bury_zombies "${REPOS_DIR}/.deep/${name}.d"
8f951e19
    done
 
    IFS="${DEFAULT_IFS}"
 }