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

CFLAGS="gcc -flto -fuse-linker-plugin -mtune=generic -Os -pipe"
CXX="gcc -flto -fuse-linker-plugin -mtune=generic -Os -pipe -fno-exceptions -fno-rtti"
P=grabber
SRC=squashfs-root
USER=`cat /etc/sysconfig/tcuser`
LIST="compiletc submitqc libX11-dev libXfixes-dev imlib2-dev"
for Z in $LIST 
    do 
    su -c "tce-load -i $Z" $USER
done
 
cd /tmp
# download png c h files
su -c "/usr/local/bin/wget -nc --no-check-certificate \
https://www.dropbox.com/s/dkjfvyp7htukoko/gsource.tcz " $USER
unsquashfs gsource.tcz
# if you run submitqc ...rm -rf gsource.tcz
cd $SRC

# compile exe
############
$CFLAGS -Wall -Wextra -fwhole-program -c $P.c -g
$CXX -I. -L. $P.o -o exe -lImlib2 -lX11 -lXfixes

# strip
strip --strip-unneeded exe
cd /tmp

mkdir -p $P/usr/local/bin/
mkdir -p $P/usr/local/share/pixmaps
mkdir -p $P/usr/local/share/applications
mkdir -p $P/usr/local/etc
mkdir -p $P/usr/local/share/doc/$P
mkdir -p $P/usr/local/tce.installed

# exe
#####
cp -f $SRC/exe $P/usr/local/bin/$P

# pixmaps
########
cp -f $SRC/$P.png $P/usr/local/share/pixmaps

# applications
##############
cat >> $P/usr/local/share/applications/$P.desktop <<'EOF'
[Desktop Entry]
Comment=Select a section of the screen with your mouse and save as .png file
Name=Selective Screenshot
Exec=/usr/local/bin/grabber
Icon=grabber
Terminal=false
X-FullPathIcon=/usr/local/share/pixmaps/grabber.png
Type=Application
Categories=Utility;
EOF

# config
########
cat >> $P/usr/local/etc/$P.conf <<'EOF'
# Config file for grabber. Lines starting with #, a space, or newline character are ignored.

# XY location for the coordinate display. Set coordsx or coordsy to -100 to disable the display.
coordsx=20
coordsy=20

# Font to use for the coordinate display. You can have multiple cfont= entries. The first valid
# font name found on your system will be used. If none of the cfont entries are valid, the
# program defaults to fixed font.
cfont=10x20
cfont=12x24
cfont=helvetica-r12

# Directory to save screengrabs to, must already exist. Use a full path including the trailing slash.
savepath=/home/tc/

# Prefix to use for saved filename. Filenames are saved as saveprefixYYYYmonthDD-HHMMSS.png
saveprefix=grabber-
EOF

# License
##########
cat >> $P/usr/local/share/doc/$P/LICENSE <<'EOF'
/* grabber was written by and is Copyright (C) Richard A. Rost  May 10,2015.
 * 
 * This program allows you to draw an outline on the screen and save the
 * outlined area as a .png file.
 * 
 * My Copyright terms are simple. You may copy, modify, and use this
 * program as you see fit provided you meet the following terms:
 * 
 * 1. You leave my name as the author and do not take credit for my work.
 * 2. While I hope this program will be useful it is offered WITHOUT ANY
 *    WARRANTY and you agree not to hold me liable for any damages.
 * 3. You leave this header in it's entirety at the top of all source
 *    files for this program and any forks. You may append notes, list
 *    changes, and add the authors of any changes to the end of this header.
 * 4. You do not collect any fee for this program as is or modified.
 * 5. You do not collect any fee for any changes you make to this program.
 * 6. You do not include or package it with any software for which any fee
 *    is collected.
 * 7. You do not include it on media (CD, DVD, etc.) which is sold or any
 *    fee is collected for the cost of the media, shipping, handling, etc.
 * 8. Items 4, 5, 6, and 7 apply to the source code as well as the executable.
 * 9. If you modify this program, you must make the full source code with a
 *    functioning compile script available in one of the following ways.
 *   A. Packaged with the executable when you distribute it.
 *   B. As a separate package available from where you distribute the
 *      executable. If distributed on a CD a web link to the source package
 *      is acceptable.
 * 
 * 
 * This program was written using the Geany 0.20 fast and lightweight IDE.
 * Tabs are set to 4.
 * -----------------------End of original header----------------------------
 */
EOF

# tce.installed
###############
cat >> $P/usr/local/tce.installed/$P <<'EOF'
#!/bin/sh
read USER < /etc/sysconfig/tcuser
[ -d /home/"$USER"/.config/grabber ] || mkdir -p /home/"$USER"/.config/grabber
[ -f /home/"$USER"/.config/grabber/grabber.conf ] || cp /usr/local/etc/grabber.conf /home/"$USER"/.config/grabber
chown -R "$USER".staff /home/"$USER"/.config/grabber
EOF
chown -R root:staff $P/usr/local/tce.installed
chmod -R 775 $P/usr/local/tce.installed

# create TCZ files
###################
LIST="$P "
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

cat >> $P.tcz.info <<'EOF'
Title:          grabber.tcz
Description:    Use your mouse to select an area of your screen for a screenshot in png format.
Version:        1.2
Author:         Richard Rost
Original-site:  http://forum.tinycorelinux.net
Copying-policy: See LICENSE
Size:           16K         
Extension_by:   aus9
Tags:           screenshot png mouse selection
Comments:       Start the program from the command line by entering  grabber  or click on the
                grabber icon or click the SelectiveScreenshot entry on the desktop menu.

                Move the mouse with the left button depressed to draw an outline around the
                section of the screen to save. Everything inside the outline will be saved as
                a  .png  file. The outline will not be included in the saved file.

                Clicking the left button again clears the outline. Moving the mouse with the
                right button depressed moves the outline.

                The readout in the top left corner displays the origin and size of the area
                that will be saved. Click and drag the readout to move it if it is in your
                way, or use the v key to toggle whether it is visible or not. There is a
                setting in the config file for the default location or to disable it. It
                also has a setting for the font to use.
                The readout will not be included in the saved file.

                Keyboard functions

                        X or x    Make the outline one pixel larger or smalle
                        Y or y    Make the outline one pixel taller or shorter

                        l or r    Move the outline one pixel left or right
                        u or d    Move the outline one pixel up or down
                                  The arrow keys will also move the outline

                        s         Save the outlined area to a file. If an outline 
                                  is not defined, the whole screen is saved. The config
                                  file has settings for default path and file name.

                        c         Same as s except include the mouse cursor in the image.

                        v         Toggle the visibility of the coordinates readout.

                        n         Start a new instance of the program. Lets you
                                  capture the outline should you wish to.

                        q         Quits the current instance of the program.
			
                        k         Kills all instances of the program.
			
                        Any other key brings up a help window.

                Configuration settings can be found in /home/$USER/.config/grabber/grabber.conf

                32 maintainer is Rich the creator
                compiled for 9X
Change-log:     2018/08/09 first version
Current:        2018/08/09
EOF

echo 'libXfixes.tcz ' > $P.tcz.dep

rm -rf $SRC
