#!/bin/sh -v

OUT="$1"
IP=root@192.168.0.204

#NIXSTORE="LD_LIBRARY_PATH=/nixbase /nixbase/ld.so.1 /nixbase/nix-store"
NIXSTORE="nix-store"

NEEDEDPATHS=`nix-store -qR $OUT`
COMMONPATHS="`(echo "$NEEDEDPATHS"; ssh $IP ". /etc/bashrc; find /nix/store -maxdepth 1 -type d ") | sort | uniq -d`"

NEWPATHS=
for a in $NEEDEDPATHS; do
    needed=1
    for b in $COMMONPATHS; do
        if [ "$a" == "$b" ]; then
            needed=0
        fi
    done
    if [ "$needed" == "1" ]; then
        NEWPATHS="$NEWPATHS $a"
    fi
done

echo common: "$NEWPATHS"
echo new: "$NEWPATHS"

nix-store --export $NEWPATHS | pv |
    ssh $IP ". /etc/bashrc; $NIXSTORE --import"
