#!/bin/sh
#
# Photo Image Print System Lite
# Copyright (C) Seiko Epson Corporation 2008.
#
#  This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#

pkglibdir="/usr/lib64/pipslite"
pkgstr="Photo Image Print System Lite 1.4.0"
cmd=`basename $0`
switchuser=root
pipslite_install_bin="$pkglibdir/$cmd"


is_ubuntu () {

    if type lsb_release >/dev/null 2>&1 ; then
        distname=`lsb_release -i -s`
        if test "xUbuntu" = "x$distname" ; then
            return 0
        fi
    fi

    if type apt-cache >/dev/null 2>&1 ; then
        distname=`apt-cache policy | \
            grep -e "c=main" | \
            grep -ve "-Security" -ve "-updates" | \
            sed -e 's/.*,o=\([[:alnum:]]*\),.*/\1/' | \
            head -1`
        if test "xUbuntu" = "x$distname" ; then
            return 0
        fi
    fi

    return 1
}

print_version () {
    echo "* $cmd is a part of $pkgstr"
    return 0
}

print_usage () {
    echo "Usage: \$ $cmd"
    echo "    no option"
    return 0
}


#
# main
#

if test $# -gt 0 ; then
    for i in $@ ; do
        if test "x-v" = "x$1" -o "x--version" = "x$1" ; then
            print_version
            exit 0
        elif test "x-u" = "x$1" -o "x--usage" = "x$1" ; then
            print_usage
            exit 0
        elif test "x-h" = "x$1" -o "x--help" = "x$1" ; then
            print_version
            print_usage
            exit 0
        fi
    done
fi

if test "x" = "x$DISPLAY" -o "x" = "x`xauth list \"$DISPLAY\" 2>/dev/null`" ; then
    echo "failed: cannot access X server" 2>/dev/null
    exit 1
fi

# if root user, execute command as is
if test 0 -eq `id -u 2>/dev/null` ; then
    exec "$pipslite_install_bin" $*
fi

# if ubuntu, use sudo
if is_ubuntu ; then
    echo "enter your password"
    exec sudo "$pipslite_install_bin" $*
fi

# use su with xauth authorization
echo "enter root password"
# FD 4 is temp for swap
exec 4>&0
xauth list "$DISPLAY" | sed -e 's/^/add /' | {
    # FD 3 is input from pips
    exec 3>&0
    # FD 0 is stdin for su (password input)
    exec 0>&4
    exec 4>&-
    exec su - "$switchuser" -c \
        "xauth -q <&3
         exec env DISPLAY='$DISPLAY' \
         LANG='$LANG' \
         LC_ADDRESS='$LC_ADDRESS' \
         LC_COLLATE='$LC_COLLATE' \
         LC_CTYPE='$LC_CTYPE' \
         LC_IDENTIFICATION='$LC_IDENTIFICATION' \
         LC_MEASUREMENT='$LC_MEASUREMENT' \
         LC_MESSAGES='$LC_MESSAGES' \
         LC_MONETARY='$LC_MONETARY' \
         LC_NAME='$LC_NAME' \
         LC_NUMERIC='$LC_NUMERIC' \
         LC_PAPER='$LC_PAPER' \
         LC_TELEPHONE='$LC_TELEPHONE' \
         LC_TIME='$LC_TIME' \
         LC_ALL='$LC_ALL' \
         "'"$SHELL"'" -c '$pipslite_install_bin $*' 3>&-"
}
