#!/bin/bash

##############################################################################
##
##  Canon Laser Printer Driver for Linux
##  Copyright CANON INC. 2015
##
##  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
##
##############################################################################


#-------------------------------------------------#
# install package list define
#-------------------------------------------------#
INSTALL_PACKAGE_RPM_32="
pangox-compat
beecrypt
beecrypt-devel"

INSTALL_PACKAGE_RPM_64="
pangox-compat
libxml2
libxml2.i686
glibc glibc.i686
libstdc++
libstdc++.i686
libjpeg-turbo
libjpeg-turbo.i686
beecrypt.i686
beecrypt-devel.i686"

INSTALL_PACKAGE_DEB_32="
libglade2-0
libjpeg62
libbeecrypt7
libbeecrypt-dev"

INSTALL_PACKAGE_DEB_64="
libglade2-0
libstdc++6:i386
libxml2:i386
libjpeg62:i386
libbeecrypt7:i386
libbeecrypt-dev:i386"

INSTALL_PACKAGE_DEB_64_IA32="
ia32-libs
libglade2-0
libjpeg62:i386
libbeecrypt7:i386
libbeecrypt-dev:i386"


#-------------------------------------------------#
# install message
#-------------------------------------------------#
INST_ERR_01_01="The current user is "
INST_ERR_01_02="Change user to root, and then perform installation again."

INST_COM_01_01="#----------------------------------------------------#"

INST_MSG_01_01="This installer is recommended for Fedora or Ubuntu distributions that are currently supported as of the release of this installer."
INST_MSG_01_04=""
INST_MSG_01_05="If this installer is run under earlier or later distributions, the installation of additional system libraries may be necessary after driver installation is complete."
INST_MSG_01_06="Do you want to continue with installation? (y/n) "

INST_MSG_02_01="Some system libraries could not be installed."
INST_MSG_02_02="Refer to the online manual for more information."
INST_MSG_02_03="Do you want to continue with installation? (y/n) "


#-------------------------------------------------#
# etc. define
#-------------------------------------------------#
ERROR_CHECK=0
MACHINE_TYPE=""
PACKAGE_TYPE=""
RELEASE_DIR=""
DRIVER_PACKAGE=""
INSTALL_PACKAGE=""
INSTALL_CMD=""
INSTALL_OPT=""
INSTALL_PACKAGE_CMD=""

COLOR_K='\033[1;30m'
COLOR_R='\033[1;31m'
COLOR_G='\033[1;32m'
COLOR_Y='\033[1;33m'
COLOR_B=''
COLOR_M='\033[1;35m'
COLOR_C='\033[1;36m'
COLOR_OFF='\033[m'


#-------------------------------------------------#
# common function
#-------------------------------------------------#
C_output_log()
{
	echo -e -n $COLOR_B
	echo -e $INST_COM_01_01
	echo -e "# $1"
	echo -e $INST_COM_01_01
	echo -e -n $COLOR_OFF
}


C_output_message()
{
	echo -e -n $COLOR_B
	echo -e "$1"
	echo -e -n $COLOR_OFF
}


C_output_error_message()
{
	echo -e -n $COLOR_R
	echo -e "$1"
	echo -e -n $COLOR_OFF
}


C_check_distribution()
{
	C_output_message "$INST_MSG_01_01"
	C_output_message "$INST_MSG_01_04"
	C_output_message "$INST_MSG_01_05"
	read -p "$INST_MSG_01_06" ans
	if [ "$ans" != "y" -a "$ans" != "Y" ]; then
		exit 1
	fi
	echo
}
	

C_check_directory()
{
	echo "${0}" | grep '/' >/dev/null  2>&1
	if [ "${?}" -eq 0 ]; then
		shell_dir="${0%/*}"
		cd "${shell_dir}"
	fi
}


C_init_cups()
{
	C_output_log "cups $1"

	if [ -f /etc/init.d/cups ]
	then
		CMD="/etc/init.d/cups $1"
		echo $CMD
		$CMD
	elif [ -f /etc/init.d/cupsys ]
	then
		CMD="/etc/init.d/cupsys $1"
		echo $CMD
		$CMD
	else
		CMD="service cups $1"
		echo $CMD
		$CMD
	fi
	echo
}


C_install_package()
{
	case $PACKAGE_TYPE in
	'deb')
		C_output_log "apt-get update"
		apt-get update
		echo
		;;
	'rpm')
		C_output_log "$INSTALL_PACKAGE_CMD upgrade"
		for upgrade_pkg in $INSTALL_PACKAGE
		do
			echo "$upgrade_pkg"
			$INSTALL_PACKAGE_CMD -y upgrade $upgrade_pkg
			echo
		done
		;;
	esac

	C_output_log "$INSTALL_PACKAGE_CMD install"

	$INSTALL_PACKAGE_CMD -y install $INSTALL_PACKAGE

	echo
}


C_install_package_check()
{
	C_output_log "Install Package Check"

	for inst_pkg in $INSTALL_PACKAGE
	do
		if which rpm > /dev/null 2>&1;
		then
			echo $inst_pkg | grep '\.'  > /dev/null 2>&1
			if [ $? -eq 0 ]; then
				LIB_NAME=`echo $inst_pkg | cut -d '.' -f1`
				ARCH=`echo $inst_pkg | cut -d '.' -f2`
				rpm -qa | grep -i $LIB_NAME | grep -i $ARCH > /dev/null 2>&1
			else
				rpm -qa | grep -i $inst_pkg > /dev/null 2>&1
			fi
		else
			echo $inst_pkg | grep ":" > /dev/null 2>&1
			if [ $? -eq 0 ]; then
				LIB_NAME=`echo $inst_pkg | cut -d ':' -f1`
				ARCH=`echo $inst_pkg | cut -d ':' -f2`
				dpkg -l | grep -i $LIB_NAME | grep -i $ARCH > /dev/null 2>&1
			else
				dpkg -l | grep -i $inst_pkg > /dev/null 2>&1
			fi
		fi

		if [ $? -eq 0 ]; then
			C_output_message " OK: $inst_pkg"
		else
			C_output_error_message   " NG: $inst_pkg"
			ERROR_CHECK=1
		fi
	done

	echo
	
	if [ $ERROR_CHECK -eq 1 ]; then
		C_output_message "$INST_MSG_02_01"
		C_output_message "$INST_MSG_02_02"
		read -p "$INST_MSG_02_03" ans
		if [ "$ans" != "y" -a "$ans" != "Y" ]; then
			exit 1
		fi
		echo
	fi
}


C_install_printer_driver()
{
	C_output_log "Install Printer Driver ($INSTALL_CMD $INSTALL_OPT)" 

	unset DRIVER_PACKAGE
	DRIVER_PACKAGE=`find . -name "*$MACHINE_TYPE.$PACKAGE_TYPE"  | grep -i common`
	$INSTALL_CMD $INSTALL_OPT $DRIVER_PACKAGE

	unset DRIVER_PACKAGE
	DRIVER_PACKAGE=`find . -name "*$MACHINE_TYPE.$PACKAGE_TYPE" | grep -v common`
	$INSTALL_CMD $INSTALL_OPT $DRIVER_PACKAGE
	echo
}


#-------------------------------------------------#
# deb function
#-------------------------------------------------#
check_install_ia32()
{
	#-------------------------------------
	# Ubuntu only
	#-------------------------------------
	grep -i -e ubuntu /etc/issue > /dev/null 2>&1
	if [ $? -ne 0 ]; then
		return 0
	fi

	if [ ! -f /etc/lsb-release ]; then
		return 0
	fi

	VER=`grep -i release /etc/lsb-release | sed -e 's/[^0-9]//g'`
	if [ $VER -le 1304 ]; then
		INSTALL_PACKAGE=$INSTALL_PACKAGE_DEB_64_IA32
	fi
}


#-------------------------------------------------#
# start install.sh
#-------------------------------------------------#
main()
{
	#---------------
	# check root
	#---------------
	if test `id -un` != "root"; then
		C_output_error_message "$INST_ERR_01_01`id -un`."
		C_output_error_message "$INST_ERR_01_02"
		exit 1
	fi
	
	#---------------------
	# check directory 
	#---------------------
	C_check_directory
	
	#---------------------
	# check distribution
	#---------------------
	C_check_distribution
	
	#------------------------
	# get distribution data
	#------------------------
	case `uname` in
	'SunOS')
		EXE_PATH='/opt/sfw/cups/sbin'
		;;
	'HP-UX')
		EXE_PATH='/usr/sbin:/usr/bin'
		;;
	'AIX')
		EXE_PATH='/usr/sbin:/usr/bin'
		;;
	'Linux')
		EXE_PATH='/usr/sbin:/usr/bin'
		;;
	esac
	
	export PATH=$EXE_PATH:$PATH
	
	if which rpm > /dev/null 2>&1;
	then
		PACKAGE_TYPE="rpm"
		INSTALL_CMD="rpm"
		INSTALL_OPT="-Uvh"
		if which yum > /dev/null 2>&1;
		then
			INSTALL_PACKAGE_CMD="yum"
		else
			INSTALL_PACKAGE_CMD="dnf"
		fi

		case `uname -m` in 	 
		'i386'|'i686') 	 
			MACHINE_TYPE="i386" 	 
			INSTALL_PACKAGE=$INSTALL_PACKAGE_RPM_32
			;; 	 
		'x86_64') 	 
			MACHINE_TYPE="x86_64" 	 
			INSTALL_PACKAGE=$INSTALL_PACKAGE_RPM_64
			;; 	 
		esac
	else
		PACKAGE_TYPE="deb"
		INSTALL_CMD="dpkg"
		INSTALL_OPT="-i --force-overwrite"
		INSTALL_PACKAGE_CMD="apt-get"
	
		case `uname -m` in 	 
		'i386'|'i686') 	 
			MACHINE_TYPE="i386" 	 
			INSTALL_PACKAGE=$INSTALL_PACKAGE_DEB_32
			;; 	 
		'x86_64') 	 
			MACHINE_TYPE="amd64" 	 
			INSTALL_PACKAGE=$INSTALL_PACKAGE_DEB_64
			check_install_ia32
			;; 	 
		esac
	fi
	
	#------------------------
	# install start
	#------------------------
	C_output_log "Install Start"
	C_output_message "Machine Type = $MACHINE_TYPE"
	C_output_message "Package Type = $PACKAGE_TYPE"
	
	DRIVER_PACKAGE=`find . -name "*$MACHINE_TYPE.$PACKAGE_TYPE" | sort 2> /dev/null`
	
	C_output_message "Package list = "
	
	for list in $DRIVER_PACKAGE
	do
		C_output_message "    $list"
	done
	echo
	
	C_install_package
	C_install_package_check
	C_install_printer_driver
	C_init_cups restart
}

main $*

C_output_message "Complete"

exit 0

