#!/bin/sh

#
# This script allows Firefox as localapp to launch applications to view
# clicked-upon files automatically without having to install these applications
# to the chroot.  (Yes, you can always download files locally.)  It goes in
# the client chroot (ex: /usr/local/bin/remote-app.sh) and requires the server
# to enable remoteapps (lts.conf):
#
#    REMOTE_APPS = True
#
# With Firefox you specify this script the first time you try to view a file.
# Pointing to ltsp-remoteapps is supposed to work but it doesn't (in Ubuntu 10.04.1).
#
# Written by Peter Matulis
# Updated by datenkollektiv.net 2014 - jan kowalsky

# $1 is absolute path of file stored in client's local /tmp (firefox default)
# This is not easily available to server

# Set up new directory in user's home which *is* available to server
TMP_NEW=~/tmp

if ! [ -d $TMP_NEW ]; then

mkdir $TMP_NEW

fi

# Copy file to new directory
cp "$1" $TMP_NEW

# Extract basename, filename, and extension
BASE=$(basename "$1")
FILE=$(echo ${BASE%.*})
EXT=$(echo ${BASE#*.})

# Build path
ABS_PATH="$TMP_NEW"/"$BASE"

# Assign an app according to extension
{
case $EXT in

pdf|PDF) APP=atril
;;

doc|DOC) APP=soffice
;;

odt|ODT) APP=soffice
;;

xls|XLS) APP=soffice
;;

ods|ODS) APP=soffice
;;



esac
}

# Invoke app on server pointing to path
/usr/bin/ltsp-remoteapps $APP "$ABS_PATH"


