#!/bin/sh
#
EXT=hplip-plugins
TCZDIR=$EXT
SDIR=plugin_tmp
CWD=$(pwd)
#
# This should download the version specific plugin and extract it
#
HPVER=$(grep '^version=' /usr/local/etc/hp/hplip.conf | cut -d= -f2)
PLUGIN=$(wget -O - http://hplip.sourceforge.net/plugin.conf | grep "http.*-$HPVER-" | cut -d\  -f3)
wget $PLUGIN
sh $(basename $PLUGIN) --keep --noexec

#
# make extensions for both 32 and 64 bit intel, could add arm too if desired
#
for a in x86_32 x86_64; do

while read DIR; do mkdir -p $TCZDIR/TCZ-$a/usr/local/$DIR; done <<EOF
lib
sane
share
share/hplip
share/hplip/fax
share/hplip/fax/plugins
share/hplip/prnt
share/hplip/prnt/__pycache__
share/hplip/prnt/plugins
share/hplip/scan
share/hplip/scan/plugins
share/hplip/data
share/hplip/data/firmware
share/hplip/data/plugins
etc
etc/udev
etc/udev/rules.d
EOF

while read FILE DIR; do cp $SDIR/$FILE $TCZDIR/TCZ-$a/usr/local/$DIR; done <<EOF
40-libsane.rules etc/udev/rules.d
S99-2000S1.rules etc/udev/rules.d
libjpeg-$a.so.9.2.0 lib
hpgt2500_ntdcmsdll-$a.so sane
libsane-hp2000S1-$a.so.1.0.25 sane
libsane-hpgt2500-$a.so.1.0.27 sane
hp_laserjet_1000.fw.gz share/hplip/data/firmware
hp_laserjet_1005_series.fw.gz share/hplip/data/firmware
hp_laserjet_1018.fw.gz share/hplip/data/firmware
hp_laserjet_1020.fw.gz share/hplip/data/firmware
hp_laserjet_p1005.fw.gz share/hplip/data/firmware
hp_laserjet_p1006.fw.gz share/hplip/data/firmware
hp_laserjet_p1007.fw.gz share/hplip/data/firmware
hp_laserjet_p1008.fw.gz share/hplip/data/firmware
hp_laserjet_p1009.fw.gz share/hplip/data/firmware
hp_laserjet_p1505.fw.gz share/hplip/data/firmware
hp_laserjet_professional_p1102.fw.gz share/hplip/data/firmware
hp_laserjet_professional_p1102w.fw.gz share/hplip/data/firmware
hp_laserjet_professional_p1566.fw.gz share/hplip/data/firmware
fax_marvell-$a.so share/hplip/fax/plugins
plugin.spec share/hplip
hbpl1-$a.so share/hplip/prnt/plugins
lj-$a.so share/hplip/prnt/plugins
bb_escl-$a.so share/hplip/scan/plugins
bb_marvell-$a.so share/hplip/scan/plugins
bb_orblite-$a.so share/hplip/scan/plugins
bb_soap-$a.so share/hplip/scan/plugins
bb_soapht-$a.so share/hplip/scan/plugins
EOF

while read FILE LINK DIR; do cd $TCZDIR/TCZ-$a/usr/local/$DIR; ln -s $FILE $LINK; cd $CWD; done <<EOF
hpgt2500_ntdcmsdll-$a.so hpgt2500_ntdcmsdll.so sane
libsane-hp2000S1-$a.so.1.0.25 libsane-hp2000S1.so sane
libsane-hp2000S1-$a.so.1.0.25 libsane-hp2000S1.so.1 sane
libsane-hpgt2500-$a.so.1.0.27 libsane-hpgt2500.so sane
libsane-hpgt2500-$a.so.1.0.27 libsane-hpgt2500.so.1 sane
fax_marvell-$a.so fax_marvell.so share/hplip/fax/plugins
hbpl1-$a.so hbpl1.so share/hplip/prnt/plugins
lj-$a.so lj.so share/hplip/prnt/plugins
bb_escl-$a.so bb_escl.so share/hplip/scan/plugins
bb_marvell-$a.so bb_marvell.so share/hplip/scan/plugins
bb_orblite-$a.so bb_orblite.so share/hplip/scan/plugins
bb_soap-$a.so bb_soap.so share/hplip/scan/plugins
bb_soapht-$a.so bb_soapht.so share/hplip/scan/plugins
libjpeg-$a.so.9.2.0 libjpeg.so.9 lib
EOF

cd $TCZDIR
sudo chown -R root.staff TCZ-$a
mksquashfs TCZ-$a $EXT-$a.tcz -noappend
cd $CWD

done
