#! /bin/sh

DONOTDO="install bashrc solarized xkb-jeyzu.patch"

function link ()
{
    dst="$2"
    ff=$(readlink -f "$1")
    bf=$(readlink -e "$dst")
    if [ "$bf" != "$ff" ]; then
        echo "fix $bf"
        rm "$dst" 2>/dev/null
    fi
    if [ ! -e "$dst" ]; then
        ln -s "$ff" "$dst"
        ls -l "$dst"
    fi
}

DIR=${0%/*}
DEST_DIR=$HOME/bin
pushd $DIR || exit 1

rm *~ 2>/dev/null
for f in *; do
    echo $DONOTDO | grep -q $f 2>/dev/null
    [ $? -eq 0 ] && continue
    link "$f" "$DEST_DIR/$f"
done

for f in bashrc; do
    link "$f" "$HOME/.$f"
done

ff=$(readlink -f solarized/dircolors.ansi-dark)
dst=$HOME/.dir_colors
if [ ! -e "$dst" ]; then
    echo "symlink $dst" && ln -s "$ff" "$dst"
else
    bf=$(readlink -e "$dst")
    if [ "$bf" != "$ff" ]; then
        echo "$bf is not OK"
    fi
fi