#!/bin/sh
# test we are root
if [ "$(id -u)" != "0" ]; then
   echo "run as root now exitting"
   exit 1
fi

# due to meson recreate missing la file
########################################
cat >> /usr/local/lib/libatk-1.0.la  <<'EOF'
# libatk-1.0.la - a libtool library file
# Generated by libtool (GNU libtool) 2.4.6 Debian-2.4.6-0.1
#
# Please DO NOT delete this file!
# It is necessary for linking the library.

# The name that we can dlopen(3).
dlname='libatk-1.0.so.0'

# Names of this library.
library_names='libatk-1.0.so.0.22810.1 libatk-1.0.so.0 libatk-1.0.so'

# The name of the static archive.
old_library=''

# Linker flags that cannot go in dependency_libs.
inherited_linker_flags=''

# Libraries that this one depends upon.
dependency_libs=' -L/usr/local/lib /usr/local/lib/libgobject-2.0.la /usr/local/lib/libffi.la /usr/local/lib/libglib-2.0.la /usr/local/lib/libpcre.la -lpthread'

# Names of additional weak libraries provided by this library
weak_library_names=''

# Version information for libatk-1.0.
current=22810
age=22810
revision=1

# Is this an already installed library?
installed=yes

# Should we warn about portability when linking against -modules?
shouldnotlink=no

# Files to dlopen/dlpreopen
dlopen=''
dlpreopen=''

# Directory that this library needs to be installed in:
libdir='/usr/local/lib'
EOF

CFLAGS="-mtune=generic -Os -pipe"
cXX="g++ -flto -fuse-linker-plugin -mtune=generic -Os -pipe "

P=xfce4-session
V=4.13.0
SRC=$P-$V
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc intltool libX11-dev libxfce4util-dev \
libxfce4ui-dev libwnck-dev consolekit-dev"
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done

cd /tmp
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://mirror.vinahost.vn/xfce/src/xfce/$P/4.13/$SRC.tar.bz2 " $USER
mkdir $P
tar jxvf $P*bz2
cd $SRC 
./configure --disable-debug --enable-polkit --with-xsession-prefix=/usr/local/share/
make check
make install-strip DESTDIR=/tmp/$P
cd /tmp

# locale
#########
mkdir -p $P-locale/usr/local/share
mv $P/usr/local/share/locale $P-locale/usr/local/share

# doc
#####
mkdir -p $P-doc/usr/local/share/doc/$P
mv $P/usr/local/share/man $P-doc/usr/local/share/
# we can't use its logout so remove man page
rm -rf $P-doc/usr/local/share/man/man1/xfce4-session-logout.1
cp $SRC/AUTHORS $P-doc/usr/local/share/doc/$P
cp $SRC/COPYING $P-doc/usr/local/share/doc/$P

# dev
#####
mkdir -p $P-dev/usr/local/lib/xfce4/session/splash-engines
mv $P/usr/local/include $P-dev/usr/local/
mv $P/usr/local/lib/*a $P-dev/usr/local/lib/
mv $P/usr/local/lib/pkgconfig $P-dev/usr/local/lib/
chmod 644 $P-dev/usr/local/lib/*a 
mv $P/usr/local/lib/xfce4/session/splash-engines/*a $P-dev/usr/local/lib/xfce4/session/splash-engines
chmod 644 $P-dev/usr/local/lib/xfce4/session/splash-engines/*a

# main
#####
mkdir -p $P/usr/local/share/doc/$P
echo 'see doc tcz for license' > $P/usr/local/share/doc/$P/COPYING
# remove the logout we use TC
rm -rf $P/usr/local/bin/*logout

# main -bin
###########
# OUR xscreensaver.tcz pathway is usr/local/bin 
sed 's|PATH=/bin:/usr/bin|PATH=/usr/local/bin|' -i $P/usr/local/bin/xflock4

# main  u lo etc xdg xfce4
##########################
# no idea of what to do about mention of xpg4 
# found inside xinitrc 

# desktops
###########
# desktops only appear in XFCE but we don't want its logout
APPS=$P/usr/local/share/applications
rm -rf $APPS/*logout.desktop 

# xsession template file put in main doc
#########################################
cat > $P/usr/local/share/doc/$P/template << "EOF"
/usr/local/bin/Xorg -nolisten tcp &
export XPID=$!
waitforX || ! echo failed in waitforX || exit
"$DESKTOP" 2>/tmp/wm_errors &
export WM_PID=$!
[ -d "$HOME/.X.d" ] && find "$HOME/.X.d" -type f -print | while read F; do . "$F"; done
/usr/local/bin/startxfce4 --with-ck-launch
EOF

# tce.installed
##############
mkdir -p $P/usr/local/tce.installed
cat > $P/usr/local/tce.installed/$P << 'EOF'
#!/bin/sh
# home dir stuff
#################
USER=`cat /etc/sysconfig/tcuser`
LOOP=/tmp/tcloop/xfce4-session/usr/local/share/doc/xfce4-session/template
# test file exists - add if not found
[ -f /home/$USER/Zsession ] || ( cp $LOOP /home/$USER/Zsession && chown $USER /home/$USER/Zsession )

# create XFCE xsession compatible file
cp -f /home/$USER/Zsession /home/$USER/.xsession
chown $USER /home/$USER/.xsession
chmod 755 /home/$USER/.xsession
# ^^ submitqc is changing these settings 

# start daemon
#############
/usr/local/etc/init.d/dbus restart

# refresh hicolor icons  
######################
sh /usr/local/tce.installed/hicolor-icon-theme
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 755 $P/usr/local/tce.installed

# it theoretically possible for someone to run this session without xfwm4
# which is why I have a quick hicolor refresh here.
# the real refresh is in the tce installed for xfwm4

# TCZ them
#######
LIST="$P $P-doc $P-dev $P-locale"
for Z in $LIST
do
	mksquashfs $Z $Z.tcz
	md5sum $Z.tcz > $Z.tcz.md5.txt
	cd $Z
	find usr -not -type d > /tmp/$Z.tcz.list
        sed 's|usr|/usr|g' -i /tmp/$Z.tcz.list
	cd /tmp
done

echo 'xfce4-session.tcz' > $P-locale.tcz.dep

echo 'xfce4-session.tcz
intltool.tcz 
libX11-dev.tcz 
libxfce4util-dev.tcz 
libxfce4ui-dev.tcz 
libwnck-dev.tcz
consolekit-dev.tcz' > $P-dev.tcz.dep

echo 'man-db.tcz'  $P-doc.tcz.dep

echo 'consolekit.tcz
xfce4-panel.tcz
xfce4-settings.tcz
Xprogs.tcz
Xorg-7.7.tcz
at-spi2-core.tcz
polkit.tcz' > $P.tcz.dep

echo 'Title:          xfce4-session.tcz BETA  
Description:    xfce session
Version:        4.13.0
Author:         see doc for authors  
Original-site:  xfce.org
Copying-policy: GPL v2
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           xfce 
Comments:       BETA XFCE session. Runs on Xorg. Install script will 
                use Zsession for its xsession template. If you want to make 
                changes please make them to that file not to .xsession
                Newbies should not attempt any changes without email or
                forum assistance please.

                Please check that xfsettingsd is enabled in settings.
                Applications -> Settings -> Session and Startup
                TAB=Application Autostart ensure box is ticked.
                (The other box is for Assistive Technologies)
                It should now be possible to effect mouse cursor themes,
                kb layouts and such.

                You can safely ignore the following 2 errors as dbus
                may not have been running when we loaded this TCZ 
                no /usr/local/bin/dbus-daemon found; none killed
                rm: cannot remove '/var/run/dbus/pid': No such file or directory

                This TCZ needs xfwm4 loaded and running to be truly effective
                please read its info file please. xfwm=XFCE window manager
                xfwm4 starts this TCZ

                Optional dependency
                ###################
                xscreensaver.tcz 
       
                Compiled for 64 9.x
Change-log:     2018/04/22  First version 
Current:        2018/05/24 rebuilt for gtk3 issue ' > $P.tcz.info

echo 'Title:          xfce4-session-doc.tcz
Description:    docs
Version:        4.13.0
Author:         see doc for authors 
Original-site:  xfce.org
Copying-policy: GPL v2
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           xfce 
Comments:       Some docs

                Compiled for 64 9.x
Change-log:     2018/04/22  First version 
Current:        22018/05/24 rebuilt for gtk3 issue' > $P-doc.tcz.info

echo 'Title:          xfce4-session-dev.tcz
Description:    dev files for xfce4-session
Version:        4.13.0
Author:         see doc for authors 
Original-site:  xfce.org
Copying-policy: GPL v2
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           xfce 
Comments:       Development files

                Compiled for 64 9.x
Change-log:     2018/04/22  First version 
Current:        2018/05/24 rebuilt for gtk3 issue' > $P-dev.tcz.info


echo 'Title:          xfce4-session-locale.tcz
Description:    locales
Version:        4.13.0
Author:         see doc for authors 
Original-site:  xfce.org
Copying-policy: GPL v2
Size:		
Extension_by:   aus9 at gmx dot com
Tags:           xfce 
Comments:       for non-english users untested

                Compiled for 64 9.x
Change-log:     2018/04/22  First version 
Current:        2018/05/24 rebuilt for gtk3 issue' > $P-locale.tcz.info

submitqc --libs
