#! /usr/bin/env bash

# Do the solaris dance to get a real shell.
if [ ! -d ~root ]  ; then
    if exec /usr/xpg4/bin/sh $0 "$@" ; then
        echo "No POSIX shell available, call <person responsible>";
        exit
    fi
fi

KNOWNMD5SUMS=$(cat <<EOF
2b5d960c21b9fda56cc30d296da35ecc
006050225ddcf746105440fe2e239455
22bfb8c1dd94b5f3813a2b25da67463f
3a2cb73ec920744185de72cd55f19e21
40b31b69676f239da8a2c7f0b375b879
4f1b2088f8e15011ee1e0306c2d64cee
508db38f182e99be2ddbe78d2c5b92ca
57a81d1c93493a29c4c3eba3490c8d2a
694af1f3e59a85ea124dc04d6992a5dc
7455657be214e67f36009154d3653e1b
7d550185ecf13512f3cb6d82228a0480
914d3bb4d80f787f100a5f298343d7e0
ab95ebdcfc2acb958912ac1c9dc7350d
b5239e7dfbecaf832e18a5377b794b09
bcf280733af4838244de094bca4c0316
c5f1155761187900cbb3b6554c2b2533
cc44c6693e6313d26d665aac57f7b7f7
d1a8c44e7dd1bed2f3e75d1343b6e4e1
d8e9ecd0b80102c82966a045544bbf68
d96c12497684c56ea77d9d1f589b0634
de2b922ef0623ba36dda5fb6f7f7d9ea
e18d7cffe9023214e4768df0865299ce
e9d2739d6f3d7672222c07d901d46e21
ee190fd94cb7bfe8a663447386c065e8
ef6c1cd4a3d89a2c0f6b16d209d27ab7
f2a26bacd5cd1a2a5c294dbb2da8d58b
084a1a5de99b4e9ec2bc6b029d9a6b2a
3e8337f44f84b298a8a99869ae8ca76a
f119c865306c35e64eb00f65d7279664
06577ad194f3e671d6c5b88d6b925f10
60e17d4bf5a626a29b440fc9d3d71af6
5b1c0c305aac28b641200f6eadca3ffe
2afdd6c53990f2387a7ef9989af0bc07
ee35a240758f374832e809ae0ea4883a
EOF
)

DOTFILESDIR=$HOME/dotfiles
MD5SUM=`command -v md5sum`
if test -z "$MD5SUM" || ! test -x "$MD5SUM"; then
    MD5SUM=`command -v md5`
fi
if test -z "$MD5SUM" || ! test -x "$MD5SUM"; then
    MD5SUM=`command -v openssl`
    if test -x "$MD5SUM"; then
	MD5SUM="$MD5SUM md5"
    fi
fi

if [ -z "$MD5SUM" ]; then
    echo "Missing md5sum command."
    exit 1
fi

if test ! -d $DOTFILESDIR; then
    echo "$DOTFILESDIR does not exist!  exiting"
    exit 1
fi

if test -z "$MD5SUM"; then
    echo "Cannot find md5sum program"
    exit 1
fi

linkdir () {
    for file in $1/*; do
        file=`basename $file`
        if test "$file" = "CVS" || test "$file" = "`basename $0`" || test "`echo $file | sed 's/^.*\(.\)/\1/'`" = "~" || test -e "$file/.skiplink"; then
            continue
        elif test -d  "$1/$file"; then
            if test ! -d "$2/$DOT$file"; then
                mkdir $2/$DOT$file
            fi
            local OLDDOT=$DOT
            local DOT=
            linkdir $1/$file $2/$OLDDOT$file
            DOT=$OLDDOT
            continue
        elif test ! -f "$1/$file"; then
            continue
        fi
        if test -f $2/$DOT$file; then
            C_MD5SUM=`$MD5SUM < $2/$DOT$file | awk '{print $1}'`
            N_MD5SUM=`$MD5SUM < $1/$file | awk '{print $1}'`
            if test "$C_MD5SUM" = "$N_MD5SUM"; then
                rm -f $2/$DOT$file
            else
                found=0
                for md5 in $KNOWNMD5SUMS; do
                    if [ "$md5" = "$C_MD5SUM" ]; then
                        rm -f $2/$DOT$file
                        found=1
                        break
                    fi
                done
                if [ "$found" = "0" ]; then
                    echo "$2/$DOT$file different from $1/$file, not known ok, and not symlink: skipping"
                    continue
                fi
            fi
        fi
        ln -s $1/$file $2/$DOT$file
    done
}
DOT=.
linkdir $DOTFILESDIR $HOME

# Clean up old files / broken symlinks
for f in config/autostart/arbtt-capture.desktop; do
    fp="${HOME}/${DOT}${f}"
    if [ -L "$fp" ] && [ ! -e "$fp" ]; then
	rm $fp
    fi
done
