#! /bin/sh
# Various 'constants'
TARGETDIR=/opt
FRITZINGVERSION=0.9.3b
FRITZINGINSTALLTAR="/csciloc/ubuntu/Distributions/fritzing-${FRITZINGVERSION}.linux.AMD64.tar.bz2"
FRITZINGDIR="${TARGETDIR}/fritzing-${FRITZINGVERSION}.linux.AMD64"
FRITZINGEXEC="${FRITZINGDIR}/Fritzing"
APPLICATIONDIR="/opt/share/applications"
APPLICATIONDESKTOP="${APPLICATIONDIR}/fritzing.desktop"

# From http://askubuntu.com/questions/15853/how-can-a-script-check-if-its-being-run-as-root
if ! [ $(id -u) = 0 ]; then
   echo "This script must be run as root"
   exit 1
fi
# end of askubuntu stuff

# Be sure system directories are there
if [ ! -d "$TARGETDIR" ]; then
    echo "creating $TARGETDIR"
    mkdir -p "$TARGETDIR"
fi
if [ ! -d "$APPLICATIONDIR" ]; then
    echo "creating $APPLICATIONDIR"
    sudo mkdir -p "$APPLICATIONDIR"
fi

# extract and set permissions
tar --extract --verbose --directory "$TARGETDIR" \
  --file "${FRITZINGINSTALLTAR}"
# find "${FRITZINGDIR}" -exec chown root:root {} \;

rm -f /usr/local/bin/fritzing
rm -f /usr/local/bin/Fritzing
ln -s "$FRITZINGEXEC" /usr/local/bin/fritzing
ln -s "$FRITZINGEXEC" /usr/local/bin/Fritzing

echo Writing to "$APPLICATIONDESKTOP"
cat > "$APPLICATIONDESKTOP" <<DESKTOPEND
[Desktop Entry]
Version=0.9.3b
Name=Fritzing
GenericName=Fritzing
Comment=Electronic Design Automation software
Exec=${FRITZINGDIR}/Fritzing
Icon=${FRITZINGDIR}/icons/fritzing_icon.png
Terminal=false
Type=Application
Categories=Development;IDE;Electronics;EDA;
X-SuSE-translate=false
StartupNotify=true
Categories=PCB;
MimeType=application/x-fritzing-fz;application/x-fritzing-fzz;application/x-fritzing-fzp;application/x-fritzing-fzpz;application/x-fritzing-fzb;application/x-fritzing-fzbz;application/x-fritzing-fzm;
DESKTOPEND
chmod a+x "${APPLICATIONDESKTOP}"
