#!/bin/bash

ICON="/usr/share/icons/hicolor/48x48/apps/installed-info.png"
# Warn if apt is running
RUN=$(ps -ef | grep "apt" | grep -v grep | wc -l)
if [ "$RUN" -gt 1 ]; then
yad --center --on-top --text="\nApt is running this application will now close!" \
--title="Installed Packages" \
--image="$ICON" --window-icon="$ICON" \
--width=400 --height=75 --fixed \
--button=Close!gtk-cancel 
else
if [ `id -u` -eq 0 ]; then
exec $@
else
yad --title="Installed Info" --width=400 \
--height=100 --fixed --image=gtk-cancel \
--window-icon=gtk-cancel --center \
--text=$"\n\You need to be root to run Installed Info!" \
--button=Close!gtk-cancel:"1"
if [ "$?" = "1" ] ; then
exit 
 fi 	
fi
CREATED=(`df -h| grep /dev/[sda-z]|sed -n '1p'`)
bash -c "tune2fs -l $CREATED | grep 'Filesystem created:'"|sed -e 's/  */ /g' -e 's/^ *\(.*\) *$/\1/' > /tmp/timestamp
bash -c "tune2fs -l $CREATED | grep 'Filesystem created:'"|sed -e 's/  */ /g' -e 's/^ *\(.*\) *$/\1/'|tail -n +2 > /tmp/installed-list
RESULTS=$(cat /tmp/installed-list |awk '{print}')
rpm -qa|sort >> /tmp/installed-list
TIMESTAMP=$(head -n 1 /tmp/timestamp)
INSTALLED="/tmp/installed-list"
COUNT=$(cat $INSTALLED | wc -l)
$(yad --center --on-top --text-info --text="$TIMESTAMP\n$COUNT packages installed" --filename="$INSTALLED" \
--title="Installed Packages" \
--image="$ICON" --window-icon="$ICON" \
--width=500 --height=400 \
--button=Save!gtk-ok:"0" \
--button=Close!gtk-cancel:"3") 
ret=$?
[[ $ret -eq "3" ]] &&
exit
USERS=$(who |sed 's/ .*//'|head -n1)
ENTRY=$(yad --width=400 --height=125 --window-icon=$ICON --image=$ICON --center \
        --form --title="Installed Info" \
        --field="Save in:DIR" /home/$USERS/Desktop \
--text='\nSave Package Information' \
--button="Cancel"!gtk-cancel:1 --button="Ok"!gtk-ok:0)
ret=$?
[[ $ret -eq 1 ]] &&
exit
if [[ $ret -eq 0 ]]; then 
RET=$?
    OIFS=$IFS 
    IFS="|" 
    i=0
    retChoice=()

    for ret in $ENTRY;do 
        retChoice[$i]="$ret"
        i=$(($i+1))
    done

    IFS=$OIFS 
    SAVEDIR=${retChoice[0]} # directory path
cat /tmp/installed-list |awk '{print}' > $SAVEDIR/installed-rpms.txt
chown -R $USERS:$USERS $SAVEDIR/installed-rpms.txt
rm -f /tmp/installed-list > /dev/null 2>&1
rm -f /tmp/timestamp > /dev/null 2>&1
exit 0
fi
 fi





