#!/bin/bash

if [ $(id -u) != 0 ]; then
    echo "Install must be run as root" >&2
    exit 1
fi

func1() {
	echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	echo "xx                            xx"
	echo "xx  Install driver failed     xx"
	echo "xx                            xx"
	echo "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
	exit 1
}

MODEL_DIR=/usr/share/cups/model/ShippingPrinter
FILTER_DIR=/usr/lib/cups/filter

sys=$(uname)
arch=$(uname -m)
bit=$(getconf LONG_BIT)
curdir=$(dirname $(readlink -f "$0"))

if [ $arch = x86_64 ]; then
    cp $curdir/Filter/amd64/rastertolabel $FILTER_DIR/shippingprinter_printer_filter

elif [ $arch = aarch64 ]; then
    cp $curdir/Filter/arm64/rastertolabel $FILTER_DIR/shippingprinter_printer_filter

elif [ $arch = loongarch64 ]; then
    cp $curdir/Filter/loongarch64/rastertolabel $FILTER_DIR/shippingprinter_printer_filter

elif [ $arch = mips64 ]; then
    cp $curdir/Filter/mips64el/rastertolabel $FILTER_DIR/shippingprinter_printer_filter

elif [ $arch = sw_64 ]; then
    cp $curdir/Filter/sw64/rastertolabel $FILTER_DIR/shippingprinter_printer_filter

elif [ $arch = i386 ] || [ $arch = i486 ] || [ $arch = i586 ] || [ $arch = i686 ]; then
    cp $curdir/Filter/x86/rastertolabel $FILTER_DIR/shippingprinter_printer_filter

fi

if [ $? -ne 0 ]; then
func1
fi


mkdir -p $MODEL_DIR 
cp -rf $curdir/PPDs/* $MODEL_DIR
if [ $? -ne 0 ]; then
func1
fi


chmod -R 755  $FILTER_DIR/shippingprinter_printer_filter
if [ $? -ne 0 ]; then
func1
fi

chown -R root $FILTER_DIR/shippingprinter_printer_filter
if [ $? -ne 0 ]; then
func1
fi

chgrp -R root $FILTER_DIR/shippingprinter_printer_filter
if [ $? -ne 0 ]; then
func1
fi


chmod 644  $MODEL_DIR/* 
if [ $? -ne 0 ]; then
func1
fi

chown -R root $MODEL_DIR/* 
if [ $? -ne 0 ]; then
func1
fi

chgrp -R root $MODEL_DIR/* 
if [ $? -ne 0 ]; then
func1
fi


if test -f /etc/init.d/cups
then
  /etc/init.d/cups restart
else
  if test -f /etc/init.d/cupsys
  then
    /etc/init.d/cupsys restart
  fi
fi

echo "********************************"
echo "**                            **"
echo "**  Install driver completed  **"
echo "**                            **"
echo "********************************"

exit 0
