#!/bin/sh

#     DESCRIPTION
#
# Copy binaries with necessary libs to outdir
# Copy other files as is


#     REQUIRES
#
# Define outdir
# File list at `/.mkimage/initrd-files'
# rsync, ldd, readlink


#     INFO
#
# Possible to use shared binaries without full packages installed
# Lookup for: /etc/xcat/alt/netboot-files

NAME="initrd-files"

verbose()
{
    if [ -n "$GLOBAL_VERBOSE" ]; then
        echo "HOOK: $NAME: $@"
    fi
}

list="/.mkimage/initrd-files"
outdir="/.mkimage/initrd"

# Get list libraries with full path
libs()
{
    local prog=$1 && shift
    [ -z "$prog" ] && return

    ldd -r "$prog" | sed -n -e '/0x/ s/^[[:space:]]\+\([^[:space:]]\+\) (0x.\+/\1/p'
    ldd -r "$prog" | sed -n -e '/0x/ s/^.\+ => \([^[:space:]]\+\) (0x.\+/\1/p'
}

# Copy necessary shared libraries for given binary file
cplibs()
{
    local prog=$1 && shift

    [ -z "$prog" ] && return


    libs "$prog" | while read lib; do
       dir="$(dirname $lib)"
       name="$(basename $lib)"
       newdir=

       [ "$dir" = "." ] && continue

       while [ -L "$dir/$name" ]; do
           mkdir -p "$outdir/$dir"
           cp -a "$dir/$name" "$outdir/$dir"

           lib="$(readlink "$dir/$name")"
           newdir="$(dirname "$lib")"
           if [ "$newdir" != "." ]; then
               dir="$dir/$newdir"
           fi
           name="$(basename "$lib")"
       done

       mkdir -p "$outdir/$dir"
       cp -a "$dir/$name" "$outdir/$dir"

    done
}

# Put file to initrd rootfs
put () {
    local file=$1 && shift
    local lib=
    local target=

    [ -z "$file" ] && return

    if [ "$GLOBAL_TARGET" = "x86_64" ]; then
        lib="lib64"
    else
        lib="lib"
    fi

    # Remove comments
    file="$(echo "$file" | sed -e 's/^[[:space:]]*//; s/[[:space:]]*$//; s/^#.*//')"
    [ -z "$file" ] && continue

    # Expand %_lib macross
    file="${file//\%_lib/$lib}"

    # Test file for existance
    if ! [ -e "$file" ]; then
        verbose "Can't find $file"
        continue
    fi

    # Was file already copied?
    if [ -e "$outdir/$file" ]; then
        verbose "Don't copy $file. Already exist."
        continue
    fi

    # Actual copy file to initrd image
    verbose "Put $file to initrd image"
    rsync -aR "$file" "$outdir"

    # Copy necessary libs for file
    if ldd "$file" >>/dev/null 2>&1; then
        cplibs "$file"
    fi

    # Also put where symlink point
    if [ -L "$file" ]; then
        target="$(readlink -f "$file")"
        if ! [ -e "$target" ]; then
            verbose "$file - is broken symlink"
            continue
        fi
        verbose "$file - is symlink, to $target"
        put "$target"
    fi
}

verbose "has started"

if ! [ -r "$list" ]; then
    verbose "Can't locate $list file"
    exit
fi

# Test for rsync availables
rsync="$(which rsync 2>/dev/null)"
if [ -z "$rsync" ]; then
      verbose "Can't find rsync"
      continue
fi

verbose "Copy files listed at $list"

cat "$list" | while read file; do
    put "$file"
done

verbose "finished"
