#!/bin/bash
#
# install-turboprint - TurboPrint installation
# subroutine which is also called by rpm package
#
# must have root permissions
#
# (c) 2000-2011 ZEDOnet

eval $(cat system.cfg)

if [ "$1" == "--rpm" -o "$1" == "--deb" ] ; then
	# additional setup if called for rpm creation
	RBR="$RPM_BUILD_ROOT"
else
	RBR=""
fi

# create dirs

mkdir -p "$RBR$TPPATH_CONFIG"
chmod 755 "$RBR$TPPATH_CONFIG"
mkdir -p "$RBR$TPPATH_SHARE/printers"
chmod 755 "$RBR$TPPATH_SHARE"
chmod 755 "$RBR$TPPATH_SHARE/printers"
mkdir -p "$RBR$TPPATH_SHARE/profiles"
chmod 755 "$RBR$TPPATH_SHARE/profiles"
mkdir -p "$RBR$TPPATH_SHARE/installprofiles"
chmod 755 "$RBR$TPPATH_SHARE/installprofiles"
mkdir -p "$RBR$TPPATH_SHARE/gplsoftware"
chmod 755 "$RBR$TPPATH_SHARE/gplsoftware"
mkdir -p "$RBR$TPPATH_SHARE/colors"
chmod 755 "$RBR$TPPATH_SHARE/colors"
mkdir -p "$RBR$TPPATH_SHARE/img"
chmod 755 "$RBR$TPPATH_SHARE/img"
mkdir -p "$RBR$TPPATH_SHARE/dump"
chmod 755 "$RBR$TPPATH_SHARE/dump"
mkdir -p "$RBR$TPPATH_SHARE/locale"
chmod 755 "$RBR$TPPATH_SHARE/locale"
mkdir -p "$RBR$TPPATH_DOC"
chmod 755 "$RBR$TPPATH_DOC"
mkdir -p "$RBR$TPPATH_LOG"
chmod 755 "$RBR$TPPATH_LOG"
mkdir -p "$RBR$TPPATH_VAR"
chmod 755 "$RBR$TPPATH_VAR"
mkdir -p "$RBR$TPPATH_FILTERS"
chmod 755 "$RBR$TPPATH_FILTERS"

mkdir -p "$RBR$TPPATH_BIN"
mkdir -p "$RBR$TPPATH_MAN/man1"
chmod 755 "$RBR$TPPATH_MAN/man1"
mkdir -p "$RBR$TPPATH_MAN/man7"
chmod 755 "$RBR$TPPATH_MAN/man7"

# copy shared files

install --mode=644 system.cfg "$RBR$TPPATH_CONFIG"
cp printers/* "$RBR$TPPATH_SHARE/printers"
chmod 644 $RBR$TPPATH_SHARE/printers/*
cp profiles/* "$RBR$TPPATH_SHARE/profiles"
chmod 644 $RBR$TPPATH_SHARE/profiles/*
cp installprofiles/* "$RBR$TPPATH_SHARE/installprofiles"
chmod 644 $RBR$TPPATH_SHARE/installprofiles/*
cp gplsoftware/* "$RBR$TPPATH_SHARE/gplsoftware"
chmod 644 $RBR$TPPATH_SHARE/gplsoftware/*
cp colors/* "$RBR$TPPATH_SHARE/colors"
chmod 644 $RBR$TPPATH_SHARE/colors/*
cp img/* "$RBR$TPPATH_SHARE/img"
chmod 644 $RBR$TPPATH_SHARE/img/*
cp dump/* "$RBR$TPPATH_SHARE/dump"
chmod 644 $RBR$TPPATH_SHARE/dump/*
install --mode=644 test*.ps "$RBR$TPPATH_SHARE/dump"
cp locale/* "$RBR$TPPATH_SHARE/locale"
chmod 644 $RBR$TPPATH_SHARE/locale/*

# copy optional Qt libraries
# copying would break turboprint --setup
#cp -r gplsoftware/qt515zedo /opt

# copy filter + support programs, make executable

install --mode=755 lib/* "$RBR$TPPATH_FILTERS"
cp -r lib/kde3applet "$RBR$TPPATH_FILTERS"
cp -r lib/kde41applet "$RBR$TPPATH_FILTERS"
cp -r lib/kde42applet "$RBR$TPPATH_FILTERS"
cp -r lib/kde5applet "$RBR$TPPATH_FILTERS"
cp -r lib/kde6applet "$RBR$TPPATH_FILTERS"
cp -r lib/gnomeapplet "$RBR$TPPATH_FILTERS"
cp -r lib/gimpplugin "$RBR$TPPATH_FILTERS"
cp -r lib/gimp3plugin "$RBR$TPPATH_FILTERS"
cp -r lib/appindicator "$RBR$TPPATH_FILTERS"
cp -r lib/appindicator3 "$RBR$TPPATH_FILTERS"

# pre and post install scripts: create a copy in the
# previously used location /usr/share/turboprint/lib

mkdir -p "$RBR$TPPATH_SHARE/lib"
cp "$RBR$TPPATH_FILTERS/install-post" "$RBR$TPPATH_SHARE/lib"
cp "$RBR$TPPATH_FILTERS/install-post-user" "$RBR$TPPATH_SHARE/lib"
cp "$RBR$TPPATH_FILTERS/uninstall-pre-user" "$RBR$TPPATH_SHARE/lib"
cp "$RBR$TPPATH_FILTERS/uninstall-pre" "$RBR$TPPATH_SHARE/lib"
if [ "$1" == "--rpm" ] ; then
	cp "$RBR$TPPATH_FILTERS/pipeutility" "$RBR$TPPATH_SHARE/lib"
fi

# create configuration file

## touch $RBR$TPPATH_CONFIG/turboprint.cfg

# create logfiles with write permissions

mkdir -p "$RBR$TPPATH_LOG/turboprint"
touch "$RBR$TPPATH_LOG/turboprint/turboprint_cups.log"
touch "$RBR$TPPATH_LOG/turboprint/turboprint_monitor.log"
touch "$RBR$TPPATH_LOG/turboprint/turboprint_composer.log"
mkdir -p "$RBR$TPPATH_VAR/turboprint/ink"
touch "$RBR$TPPATH_VAR/turboprint/tpstatus"

# copy executable files, make executable

install --mode=755  bin/* "$RBR$TPPATH_BIN"

# copy documents

install --mode=644 BUGREPORT "$RBR$TPPATH_DOC"
install --mode=644  INSTALLATION "$RBR$TPPATH_DOC"
install --mode=644  README "$RBR$TPPATH_DOC"
install --mode=644  CHANGES "$RBR$TPPATH_DOC"
install --mode=644 "TurboPrint_Manual.pdf" "$RBR$TPPATH_DOC"
install --mode=644 "TurboPrint_Handbuch.pdf" "$RBR$TPPATH_DOC"
install --mode=644 doc/* "$RBR$TPPATH_DOC" &> /dev/null

# copy manpages

install --mode=644  doc/*.1.gz "$RBR$TPPATH_MAN/man1"
install --mode=644  doc/*.7.gz "$RBR$TPPATH_MAN/man7"

# finished

exit 0
