#!/bin/sh
set -eu

cd `dirname $0`/files

echo Testing binary...
./0install --version
echo OK
echo

# This file gets copied into the build/static_dist directory as "install.sh". It can be run
# from there to install everything.

if [ $# != 1 ]; then
	echo "To install to /usr/local:"
	echo "./install.sh local"
	echo
	echo "To install to your home directory:"
	echo "./install.sh home"
	echo
	echo "You can also just run \"./files/0install\" directly, but"
	echo "some features won't work unless \"0install\" is in \$PATH."
	exit 1
fi

DOCS="../README.md ../COPYING"
MANPAGES="0launch.1 0store-secure-add.1 0store.1 0desktop.1 0install.1"
PLUGINS=`find . \( -name 'gui_gtk.cma' -o -name 'gui_gtk.cmxs' \) -print`

# Use "share/zsh/vendor-completions" on Debian
ZSHFUNCTIONS="share/zsh/site-functions"

case "$1" in
home)
	# Note sure about PREFIX_MAN, but this is what the old Python version did
	PREFIX="${HOME}"
	ZSHFUNCTIONS=".zsh"
	PREFIX_SHARE="${HOME}/.local/share"
	PREFIX_MAN="${HOME}/.local/man"
	;;
system)
	PREFIX="/usr"
	PREFIX_SHARE="${PREFIX}/share"
	PREFIX_MAN="${PREFIX_SHARE}/man"
	;;
local)
	PREFIX="/usr/local"
	PREFIX_SHARE="${PREFIX}/share"
	PREFIX_MAN="${PREFIX}/man"
	;;
/*)
	PREFIX="$1"
	PREFIX_SHARE="${PREFIX}/share"
	PREFIX_MAN="${PREFIX_SHARE}/man"
	;;
*)
	echo "Use: \"./install.sh local\" to install"
	exit 1
	;;
esac

echo "Installing to ${DESTDIR:=""}$PREFIX/..."

install -d "$DESTDIR${PREFIX}/bin"
install 0install "$DESTDIR${PREFIX}/bin/0install"
ln -f "$DESTDIR${PREFIX}/bin/0install" "$DESTDIR${PREFIX}/bin/0launch"
ln -f "$DESTDIR${PREFIX}/bin/0install" "$DESTDIR${PREFIX}/bin/0store"
ln -f "$DESTDIR${PREFIX}/bin/0install" "$DESTDIR${PREFIX}/bin/0store-secure-add"
ln -f "$DESTDIR${PREFIX}/bin/0install" "$DESTDIR${PREFIX}/bin/0desktop"
ln -f "$DESTDIR${PREFIX}/bin/0install" "$DESTDIR${PREFIX}/bin/0alias"
install -d "$DESTDIR${PREFIX_MAN}/man1/"
install -d "$DESTDIR${PREFIX_SHARE}/0install.net/"
install -d "$DESTDIR${PREFIX_SHARE}/applications/"
install -d "$DESTDIR${PREFIX_SHARE}/metainfo/"
install -d "$DESTDIR${PREFIX_SHARE}/bash-completion/completions/"
install -d "$DESTDIR${PREFIX_SHARE}/fish/completions"
install -d "$DESTDIR${PREFIX_SHARE}/doc/0install"
install -d "$DESTDIR${PREFIX}/${ZSHFUNCTIONS}"
install -d "$DESTDIR${PREFIX}/lib/0install.net"

install -m 0644 ${DOCS} "$DESTDIR${PREFIX_SHARE}/doc/0install/"
install -m 0644 ${MANPAGES} "$DESTDIR${PREFIX_MAN}/man1/"
install -m 0644 share/applications/0install.desktop "$DESTDIR${PREFIX_SHARE}/applications/"
install -m 0644 share/metainfo/0install.appdata.xml "$DESTDIR${PREFIX_SHARE}/metainfo/"

install -m 0644 share/bash-completion/completions/0install "$DESTDIR${PREFIX_SHARE}/bash-completion/completions/"
install -m 0644 share/bash-completion/completions/0launch "$DESTDIR${PREFIX_SHARE}/bash-completion/completions/"
install -m 0644 share/fish/completions/0install.fish "$DESTDIR${PREFIX_SHARE}/fish/completions/"
install -m 0644 share/fish/completions/0launch.fish "$DESTDIR${PREFIX_SHARE}/fish/completions/"
install -m 0644 share/zsh/site-functions/_0install "$DESTDIR${PREFIX}/${ZSHFUNCTIONS}/"
install -m 0644 share/zsh/site-functions/_0launch "$DESTDIR${PREFIX}/${ZSHFUNCTIONS}/"
install -m 0755 share/0install.net/unlzma "$DESTDIR${PREFIX_SHARE}/0install.net/"
install -m 0755 share/0install.net/unxz "$DESTDIR${PREFIX_SHARE}/0install.net/"

if [ -n "$PLUGINS" ]; then
  install -m 0644 $PLUGINS "$DESTDIR${PREFIX}/lib/0install.net/"
fi

for size in 24x24 48x48 128x128 scalable; do \
  install -d "$DESTDIR${PREFIX_SHARE}/icons/hicolor/${size}/apps/" && \
  install -m 0644 share/icons/${size}/* "$DESTDIR${PREFIX_SHARE}/icons/hicolor/${size}/apps/"; done

echo "OK. Now run \"$DESTDIR${PREFIX}/bin/0install --help\" for help."
