#!/bin/sh
# vv Enter user defaults below this line
XLXDIR='/opt/Xilinx/12.1/ISE_DS'
CPUARCH='32'
PDFREADER='evince'
# ^^ Enter user defaults above this line

echo "Creating wrapper script on /usr/local/bin/xilinx..."
cat >/usr/local/bin/xilinx <<EOF
#!/bin/bash

XLXDIR='$XLXDIR'
SETTINGS='settings$CPUARCH.sh'

xilinx_help () {
    echo "\\\`xilinx' will run a command under a Xilinx environment."
    echo "Usage:"
    echo "  xilinx <program> [<arguments>]"
    echo "Examples:"
    echo "  xilinx ise"
    echo "  xilinx xps"
    echo "Xilinx is installed on \$XLXDIR"
}

if [ \$# -lt 1 ]
then
    xilinx_help  # if no argument is given, display help and exit
    exit 0
fi

export PATH="\$XLXDIR/alternatives:\$PATH"  # user defaults

. "\$XLXDIR/\$SETTINGS" >&2  # run Xilinx settings scripts

export LC_NUMERIC=C  # there's a problem with locales using 0,0 instead of 0.0
export DISPLAY="\${DISPLAY%.*}"  # there's a problem with \$DISPLAY being :0.0 instead of :0
#export XIL_IMPACT_USE_LIBUSB=1  # libusb-dev drivers (by default now)
"\$@"  # run command on Xilinx env
EOF
chmod +x /usr/local/bin/xilinx

# TODO: Create launchers on /usr/local/share/applications/xilinx-*.desktop"

echo "Creating rules for bash completion..."
cat >/etc/bash_completion.d/xilinx <<EOF
# bash completion for xilinx

XLXDIR='$XLXDIR'

have xilinx && {
_xilinx() {
	local cur list
    cur=\$(_get_cword)
    if [ "\$COMP_CWORD" != 1 ]; then
        _filedir
        return 0
    fi
    list="\$(
        xilinx bash -c 'echo "\$PATH"' 2>/dev/null |\\
        while read -rd: i
        do
            if [[ \$i = \$XLXDIR* ]]; then
                cd "\$i"
                echo -n * ''
            fi
        done
    ))"
    COMPREPLY=(\$(compgen -W "\$list" -- "\$cur"))
    return 0
}
complete -F _xilinx xilinx
}
EOF

echo "Installing drivers..."
if   which apt-get >/dev/null; then apt-get install libusb-dev   fxload
#elif which yum     >/dev/null; then yum     install libusb-devel fxload
else echo "Please install libusb-dev and fxload (or equivalent)"
fi
# TODO: make cross-distribution
# (I'm not sure which packages are needed on distros other than Ubuntu)

echo "Creating rules for USB controller..."
cp "$XLXDIR/ISE/bin/lin/xusbdfwu.rules" /etc/udev/rules.d/90-xusbdfwu.rules
sed -i "s:/usr/share/:$XLXDIR/ISE/bin/lin/:g; s:BUS:SUBSYSTEMS:g; s:TEMPNODE:tempnode:g" /etc/udev/rules.d/90-xusbdfwu.rules
if which service >/dev/null
then service     udev restart
else /etc/init.d/udev restart
fi

echo "Installing required libraries..."
if   which apt-get >/dev/null; then apt-get install libmotif3
else echo "Please install libmotif3 (or equivalent)"
fi
# motif (libXm.so.3) is needed for fpga-editor

[ -e "$XLXDIR/ISE/lib/lin/libstdc++.so.5" ] || ln -s "$XLXDIR/PlanAhead/lib/lnx32.o/libstdc++.so.5" "$XLXDIR/ISE/lib/lin/libstdc++.so.5"
# for some reason, this file isn't where it should.
# Don't know if this will work for 64-bit architectures.

echo "Creating command symlinks..."
mkdir "$XLXDIR/alternatives"
ln -s "$(which make)"         "$XLXDIR/alternatives/gmake"
ln -s "$(which "$PDFREADER")" "$XLXDIR/alternatives/acroread"

# Xilinx assumes that bash is the only and default shell.
# They should use /bin/bash instead of just /bin/sh.
# For now, replacing sh with bash is the only way around.
if [ -h /bin/sh -a bash '!=' "$(readlink /bin/sh)" ]
then
    echo "Replacing /bin/sh with a link to /bin/bash"
    rm /bin/sh
    ln -s bash /bin/sh
fi

if [ 64 = "$CPUARCH" ]
then
    echo "Fixing $XLXDIR/PlanAhead/bin/rdiArgs.sh to use 64-bit by default"
    sed -i 's/RDI_PLATFORM=lnx32/RDI_PLATFORM=lnx64/; s/RDI_JAVA_PLATFORM=i386/RDI_JAVA_PLATFORM=amd64/' "$XLXDIR/PlanAhead/bin/rdiArgs.sh"
fi
# although it would be better to run the script with -m64


echo "Everything done! :)"
echo
xilinx  # Display usage
