#!/bin/sh

#----------------------------------------------------------------------------------------------
# Initialize
#----------------------------------------------------------------------------------------------
##### ADDED BY DAI #####
cleartmp(){
    if [ -n "$FSTMPDIR" ]; then
        if [ -d "$FSTMPDIR" ]; then
            rm -rf "$FSTMPDIR"
        fi
    fi
}
##### END OF EDIT #####
LANG="C"
CFGFILE="$1"
#CFGFILE="$(kdialog --getopenfilename $HOME/Documents *.cfg)"
if [ -z "$CFGFILE" ] ; then ##### MOVED BY DAI #####
 kdialog --title "Error !" --icon "package_settings" --error "Need configuration file, exiting ..." & ##### MOVED BY DAI #####
 exit 1 ##### MOVED BY DAI #####
fi ##### MOVED BY DAI #####
CFGFILE="$(readlink -f $CFGFILE)"
CFGFILE_DIR=$(dirname $CFGFILE)
cd $CFGFILE_DIR
if [ ! -f "$CFGFILE" ] ; then
 kdialog --title "Error !" --icon "package_settings" --error "Configuration file not exist, exiting ..." &
 exit 1
fi
CFGHEADER=$(head -1 $CFGFILE)
if [ "$CFGHEADER" != "#***q4os*setup*config*header*do*not*delete*it***#" ] ; then
 kdialog --title "Error !" --icon "package_settings" --error "Bad configuration file, exiting ..." &
 exit 1
fi
echo "Config file ... OK"

TEMPLATES_DIR_0=""
TEMPLATES_DIR="/program_files/q4os-devpack/appsetup/templates"
OUT_DIR="setup"
PRECREATE_SH="0"
PREPACK_SH="0"
POSTPACK_SH="0"
CHK_INET="0"
CHK_PASSW="0"
Q4VER_MIN="0"
Q4VER_MAX="0"
APPLNK_ENTRY="1"
DESKTOP_ENTRY="1"
MENU_ENTRY="1"
REG_ENTRY="1"
UNINSTALL_ENTRY="1"
APP_ICON="application-x-executable"
PK_NAME="application"
APP_LINK="/program_files/$PK_NAME/usr/bin/$PK_NAME.exu"
PK_VERS="0.0.1"
PK_RELEASE="1"
PK_DEPENDS="libc6"
PK_REPLACES="none"
PK_BREAKS="none"
PK_CONFLICTS="none"
PK_MAINTAINER="maintainer@maintainer.org"
PK_ARCH=$(dpkg-architecture -qDEB_HOST_ARCH)
LANGUAGE_PACKAGE=""
SETUP_TYPE="1"
APPSETUP2_EXE="/usr/share/apps/q4os_system/bin/appsetup2.exu"
APTKEY1_FILE=""
APTKEY2_FILE=""
CUSTOMHOOK_PRERUN_SCRIPT=""
CUSTOMHOOK_POSTSETUP_SCRIPT=""
APP_ECONFIG=""
RPRP_PROCESS="0"
RPRP_CODENAME="oscodename"
RPRP_DBLOCATION="/tmp/local_repository"

. $CFGFILE
##### MODIFIED BY DAI #####
if [ -z "$CHKINSTALL_COMMAND" ] ; then
 if [ ! -f "$CFGFILE_DIR/Makefile" ] ; then
  kdialog --title "Error !" --icon "package_settings" --error "<p>No <b>Makefile</b> present in \"$CFGFILE_DIR\" directory, you need configure and build this project before installer operations.</p><p>Exiting now ...</p>" &
  exit 1
 else
  if [ ! `cat Makefile | grep "install:"` ]; then
   kdialog --title "Error !" --icon "package_settings" --error "<p><b>Makefile</b> present in \"$CFGFILE_DIR\" directory, but you need configure and build this project before installer operations.</p><p>Exiting now ...</p>" &
   exit 1
  fi
  CHKINSTALL_COMMAND="make install"
 fi
fi
##### END OF EDIT #####
#todo: check if APTKEY1_FILE, APTKEY2_FILE exist
if [ -z "$INSTALL_PREFIX" ] ; then
 INSTALL_PREFIX="/program_files/$PK_NAME/"
fi
if [ -z "$APPNAME_DESC" ] ; then
 APPNAME_DESC="$( echo $PK_NAME | awk '{ print substr(toupper($0),1,1) substr($0,2) }' )" #first letter convert to uppercase
fi
UNINST_LINK="/usr/share/apps/q4os_system/uninst_q4inf/uninstall_$PK_NAME.esh"

FSTMPDIR=$(mktemp -d --tmpdir=/tmp --suffix=-installbuilder)
cp -a /program_files/q4os-devpack/appsetup/templates/* $FSTMPDIR/
if [ -d "$TEMPLATES_DIR_0" ] ; then
 cp -a $TEMPLATES_DIR_0/* $FSTMPDIR/
fi
cp $FSTMPDIR/app1.desktop.template $FSTMPDIR/app2.desktop.template #copy template for desktop shortcut too, it can be overwriten by user templates, see next
if [ -d "$TEMPLATES_DIR" ] ; then
 cp -a $TEMPLATES_DIR/* $FSTMPDIR/
fi
cd $FSTMPDIR

sed -i "s/substitute_architecture/$PK_ARCH/" appsetup1.sh.template
sed -i "s:substitute_q4os_setup:$APPSETUP2_EXE:" appsetup1.sh.template
sed -i "s/substitute_langpack/$LANGUAGE_PACKAGE/" config_1.template
sed -i "s/substitute_application_name/$PK_NAME/" config_1.template
sed -i "s/substitute_application_desc_name/$APPNAME_DESC/" config_1.template
sed -i "s/substitute_setup_type/$SETUP_TYPE/" config_1.template
sed -i "s/substitute_aptkey1_pub_file/$APTKEY1_FILE/" config_1.template
sed -i "s/substitute_aptkey2_pub_file/$APTKEY2_FILE/" config_1.template
sed -i "s/substitute_check_internet/$CHK_INET/" config_1.template
sed -i "s/substitute_q4os_version_minimal/$Q4VER_MIN/" config_1.template
sed -i "s/substitute_q4os_version_maximal/$Q4VER_MAX/" config_1.template
if [ "$CHK_PASSW" = "1" ] ; then
 sed -i "s:substitute_without_password:0:" config_1.template
else
 sed -i "s:substitute_without_password:1:" config_1.template
fi
sed -i "s/substitute_application_name_desc/$APPNAME_DESC/" app.menu.template
sed -i "s/substitute_application_name/$PK_NAME/" app.menu.template
sed -i "s/substitute_application_name_desc/$APPNAME_DESC/" app1.desktop.template
sed -i "s/substitute_application_name/$PK_NAME/" app1.desktop.template
sed -i "s:substitute_application_icon:$APP_ICON:" app1.desktop.template
sed -i "s#substitute_application_link#$APP_LINK#" app1.desktop.template
sed -i "s/substitute_application_name_desc/$APPNAME_DESC/" app2.desktop.template
sed -i "s/substitute_application_name/$PK_NAME/" app2.desktop.template
sed -i "s:substitute_application_icon:$APP_ICON:" app2.desktop.template
sed -i "s#substitute_application_link#$APP_LINK#" app2.desktop.template
sed -i "s/substitute_application_name_desc/$APPNAME_DESC/" uninstall.desktop.template
sed -i "s:substitute_uninstall_link:$UNINST_LINK:" uninstall.desktop.template
sed -i "s/substitute_application_name_desc/$APPNAME_DESC/" uninstall.esh.template
sed -i "s/substitute_application_name/$PK_NAME/" uninstall.esh.template
sed -i "s:substitute_application_name:$PK_NAME:" app.qrg.template
sed -i "s:substitute_application_version:$PK_VERS:" app.qrg.template
sed -i "s:substitute_maintaner_web::" app.qrg.template
sed -i "s:substitute_maintaner_email:$PK_MAINTAINER:" app.qrg.template
sed -i "s:substitute_uninstall_info:$UNINST_LINK:" app.qrg.template

mv appsetup1.sh.template appsetup1.sh
cat config_1.template $APP_ECONFIG > config_1_rc
rm -f config_1.template

# todo: check SETUP_TYPE value
# if [ $SETUP_TYPE < 0 and $SETUP_TYPE > 2 ] ; then
#  kdialog --title "Error !" --icon "package_settings" --error "Bad value of SETUP_TYPE config variable, please review config file." &
#  clean temporary dir
#  exit 1
# fi

if [ -n "${SETUP_PIC_01_FILE+x}" ] ; then
 if [ -f "$CFGFILE_DIR/$SETUP_PIC_01_FILE" ] ; then
  cp $CFGFILE_DIR/$SETUP_PIC_01_FILE $FSTMPDIR/setup_pic01.png ##### MODIFIED BY DAI #####
  SETUP_PIC_01_FILE="setup_pic01.png"
 else
  kdialog --title "Error !" --icon "package_settings" --error "Setup picture is defined as \"$SETUP_PIC_01_FILE\", but not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
fi
if [ -n "${SETUP_PIC_02_FILE+x}" ] ; then
 if [ -f "$CFGFILE_DIR/$SETUP_PIC_02_FILE" ] ; then ##### MODIFIED BY DAI #####
  cp $CFGFILE_DIR/$SETUP_PIC_02_FILE $FSTMPDIR/setup_pic02.png
  SETUP_PIC_02_FILE="setup_pic02.png"
 else
  kdialog --title "Error !" --icon "package_settings" --error "Setup picture is defined as \"$SETUP_PIC_02_FILE\", but not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
fi
if [ -n "${SETUP_DESCRIPTION_FILE+x}" ] ; then
 if [ -f "$CFGFILE_DIR/$SETUP_DESCRIPTION_FILE" ] ; then ##### MODIFIED BY DAI #####
  cp $CFGFILE_DIR/$SETUP_DESCRIPTION_FILE $FSTMPDIR/description.htm
  SETUP_DESCRIPTION_FILE="description.htm"
 else
  kdialog --title "Error !" --icon "package_settings" --error "Description file is defined as \"$SETUP_DESCRIPTION_FILE\", but not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
fi
if [ -n "${SETUP_DESCRIPTION_FILE_FINISH+x}" ] ; then
 if [ -f "$CFGFILE_DIR/$SETUP_DESCRIPTION_FILE_FINISH" ] ; then ##### MODIFIED BY DAI #####
  cp $CFGFILE_DIR/$SETUP_DESCRIPTION_FILE_FINISH $FSTMPDIR/description_finish.htm
  SETUP_DESCRIPTION_FILE_FINISH="description_finish.htm"
 else
  kdialog --title "Error !" --icon "package_settings" --error "Description file is defined as \"$SETUP_DESCRIPTION_FILE_FINISH\", but not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
fi
if [ -n "$CUSTOMHOOK_PRERUN_SCRIPT" ] ; then
 if [ ! -f "customhook_prerun.esh" ] ; then
  kdialog --title "Error !" --icon "package_settings" --error "CUSTOMHOOK_PRERUN_SCRIPT variable is set, but file customhook_prerun.esh not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
 CUSTOMHOOK_PRERUN_SCRIPT="customhook_prerun.esh"
fi
if [ -n "$CUSTOMHOOK_POSTSETUP_SCRIPT" ] ; then
 if [ ! -f "customhook_postsetup.esh" ] ; then
  kdialog --title "Error !" --icon "package_settings" --error "CUSTOMHOOK_POSTSETUP_SCRIPT variable is set, but file customhook_postsetup.esh not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
 CUSTOMHOOK_POSTSETUP_SCRIPT="customhook_postsetup.esh"
fi
if [ -n "$CUSTOMHOOK_PREAPT_R_SCRIPT" ] ; then
 if [ ! -f "customhook_preapt_r.esh" ] ; then
  kdialog --title "Error !" --icon "package_settings" --error "CUSTOMHOOK_PREAPT_R_SCRIPT variable is set, but file customhook_preapt_r.esh not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
 CUSTOMHOOK_PREAPT_R_SCRIPT="customhook_preapt_r.esh"
fi
if [ -n "$CUSTOMHOOK_POSTSETUP_R_SCRIPT" ] ; then
 if [ ! -f "customhook_postsetup_r.esh" ] ; then
  kdialog --title "Error !" --icon "package_settings" --error "CUSTOMHOOK_POSTSETUP_R_SCRIPT variable is set, but file customhook_postsetup_r.esh not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
 CUSTOMHOOK_POSTSETUP_R_SCRIPT="customhook_postsetup_r.esh"
fi
if [ -n "${SETUP_LICENSE_FILE+x}" ] ; then
 if [ -f "$FSTMPDIR/$SETUP_LICENSE_FILE" ] ; then
  mv $FSTMPDIR/$SETUP_LICENSE_FILE $FSTMPDIR/license.htm
  LICFILE_EXIST="1"
 fi
 if [ -f "$CFGFILE_DIR/$SETUP_LICENSE_FILE" ] ; then
  cp $CFGFILE_DIR/$SETUP_LICENSE_FILE $FSTMPDIR/license.htm
  LICFILE_EXIST="1"
 fi
 if [ -f "$SETUP_LICENSE_FILE" ] ; then
  cp $SETUP_LICENSE_FILE $FSTMPDIR/license.htm
  LICFILE_EXIST="1"
 fi
 if [ "$LICFILE_EXIST" = "1" ] ; then
  SETUP_LICENSE_FILE="license.htm"
 else
  kdialog --title "Error !" --icon "package_settings" --error "License file is defined as \"$SETUP_LICENSE_FILE\", but not found, please review config file.\n\nCancelling ..." &
  cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
  exit 1
 fi
fi
if [ -z "$APPSETUP2_EXE" ] ; then
 if [ "$PK_ARCH" != "all" ] ; then
   SETUP_EXECUTABLE="appsetup2_$PK_ARCH.exu"
 else
   SETUP_EXECUTABLE="appsetup2_i386.exu appsetup2_amd64.exu"
 fi
fi

#----------------------------------------------------------------------------------------------
# Source user's pre-create script
#----------------------------------------------------------------------------------------------
if [ $PRECREATE_SH = "1" ]
then
 . $CFGFILE_DIR/sourced.precreate
fi

#----------------------------------------------------------------------------------------------
# Check sudo rights
#----------------------------------------------------------------------------------------------
sudo -n dash /program_files/q4os-devpack/bin/sudo_devpack_helper.sh test_arg
RCANSWER=$?
if [ "${RCANSWER}" != "0" ] ; then
 echo "No sudo rights, exiting ..."
 kdialog --title "Error !" --icon "package_settings" --error "You don't have sudo admin permissions, exiting ..." &
 cleartmp # todo: clean temporary dir ##### ADDED BY DAI #####
 exit 1
fi
echo "Sudo password ... OK"

#----------------------------------------------------------------------------------------------
# Create deb file
#----------------------------------------------------------------------------------------------
cd $CFGFILE_DIR
echo "$PK_NAME for Q4OS" > description-pak

# --reset-uids=yes
# --breaks "$PK_BREAKS" \
# >& /dev/null
checkinstall \
    --default \
    --install=no \
    --deldesc=yes \
    --strip=yes \
    --stripso=yes \
    --backup=no \
    --nodoc \
    --requires "$PK_DEPENDS" \
    --replaces "$PK_REPLACES" \
    --conflicts "$PK_CONFLICTS" \
    --maintainer "$PK_MAINTAINER" \
    --pkgname "$PK_NAME" \
    --pkgarch "$PK_ARCH" \
    --pkgversion "$PK_VERS" \
    --pkgrelease "$PK_RELEASE" \
    --pkglicense none \
    --pkggroup none \
    --pkgsource none \
    --pakdir "$FSTMPDIR" \
    $CHKINSTALL_COMMAND

rm -f description-pak
cd $FSTMPDIR

#----------------------------------------------------------------------------------------------
# Extract and modify deb file/s, menu, desktop entries, registration
#----------------------------------------------------------------------------------------------
WKDIR01="wkdir"
mkdir -p $WKDIR01/$INSTALL_PREFIX
dpkg-deb -x ${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.deb $WKDIR01/$INSTALL_PREFIX
dpkg-deb -e ${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.deb $WKDIR01/DEBIAN
rm -f ${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.deb

APPSLNKSDIR="$WKDIR01/usr/share/applications"

if [ $APPLNK_ENTRY = "1" ] ; then
 mkdir -p $APPSLNKSDIR
 cp app1.desktop.template $APPSLNKSDIR/${PK_NAME}_q4app.desktop
fi

if [ $DESKTOP_ENTRY = "1" ] ; then
 mkdir -p $WKDIR01/opt/trinity/share/apps/kdesktop/Desktop
 cp app2.desktop.template $WKDIR01/opt/trinity/share/apps/kdesktop/Desktop/${PK_NAME}_q4dsk.desktop
fi

if [ $MENU_ENTRY = "1" ] ; then
 mkdir -p $WKDIR01/usr/share/apps/q4os_system/q4os_menus_applications_merged
 cp app.menu.template $WKDIR01/usr/share/apps/q4os_system/q4os_menus_applications_merged/${PK_NAME}_q4app.menu
 if [ -f "app.mnudir.template" ] ; then
   mkdir -p $WKDIR01/usr/share/desktop-directories
   cp app.mnudir.template $WKDIR01/usr/share/desktop-directories/${PK_NAME}_q4app.directory
 fi
fi

if [ $REG_ENTRY = "1" ] ; then
 mkdir -p $WKDIR01/usr/share/apps/q4os_system/q4_regs
 cp app.qrg.template $WKDIR01/usr/share/apps/q4os_system/q4_regs/$PK_NAME.qrg
fi

if [ "$UNINSTALL_ENTRY" = "1" ] ; then
 mkdir -p $WKDIR01/$(dirname $UNINST_LINK)
 cp uninstall.esh.template $WKDIR01/$UNINST_LINK
 if [ $MENU_ENTRY = "1" ] ; then
  mkdir -p $APPSLNKSDIR
  cp uninstall.desktop.template $APPSLNKSDIR/${PK_NAME}_uninstall.desktop
 fi
fi

# todo - copy and run pre-installation script ...
# if [ $PREINST_SCR = "1" ] ; then
#   cp preinst $WKDIR01/DEBIAN/
# fi

# todo - copy and run post-installation script ...
# if [ $POSTINST_SCR = "1" ] ; then
#   cp postinst $WKDIR01/DEBIAN/
# fi

#----------------------------------------------------------------------------------------------
# Source user's pre-pack script
#----------------------------------------------------------------------------------------------
if [ $PREPACK_SH = "1" ]
then
 . $CFGFILE_DIR/sourced.prepack
fi

#----------------------------------------------------------------------------------------------
# Check if deb is ok, set ownership and permissions, pack deb again
#----------------------------------------------------------------------------------------------
sudo dash /program_files/q4os-devpack/bin/sudo_devpack_helper.sh chown_wkdir # chown -R root:root $WKDIR01
dpkg-deb -b $WKDIR01 ${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.deb
sudo dash /program_files/q4os-devpack/bin/sudo_devpack_helper.sh rm_wkdir # rm -rf $WKDIR01

#----------------------------------------------------------------------------------------------
# Create installer repository
#----------------------------------------------------------------------------------------------
TMP_SETUP_REPO="tmp-setup-repo"
rm -rf $TMP_SETUP_REPO

mkdir -p $TMP_SETUP_REPO
cp *.deb $TMP_SETUP_REPO/
if [ -d "extra_debs" ] ; then
 cp extra_debs/*.deb $TMP_SETUP_REPO/
fi
dpkg-scanpackages -m $TMP_SETUP_REPO | gzip -9c > $TMP_SETUP_REPO/Packages.gz

# OR
# mkdir -p $TMP_SETUP_REPO/conf
# cat > $TMP_SETUP_REPO/conf/distributions <<EOF
# Label: local repository
# Codename: q4localsetup
# Version: $PK_VERS
# Architectures: amd64 armel i386 ia64 source
# Components: main
# Description: local repository
# #SignWith: yes
# EOF
#
# reprepro -V --ask-passphrase -b $TMP_SETUP_REPO includedeb q4localsetup *.deb
# if [ -d "extra_debs" ] ; then
#  reprepro -V --ask-passphrase -b $TMP_SETUP_REPO includedeb q4localsetup extra_debs/*.deb
# fi
# rm -rf $TMP_SETUP_REPO/conf $TMP_SETUP_REPO/db

#----------------------------------------------------------------------------------------------
# Add custom apt repositories and pinning
#----------------------------------------------------------------------------------------------
if [ -f "$FSTMPDIR/tmpsetup.list" ] ; then
 TMPSETUP_LIST="tmpsetup.list"
fi
if [ -f "$FSTMPDIR/pin-tmpsetup" ] ; then
 TMPSETUP_PIN="pin-tmpsetup"
fi

#----------------------------------------------------------------------------------------------
# Pack Q4OS application setup files
#----------------------------------------------------------------------------------------------
tar -c --numeric-owner -f wkfile1.tar $SETUP_EXECUTABLE config_1_rc $SETUP_PIC_01_FILE $SETUP_PIC_02_FILE $SETUP_DESCRIPTION_FILE $SETUP_DESCRIPTION_FILE_FINISH $SETUP_LICENSE_FILE $CUSTOMHOOK_PRERUN_SCRIPT
PACK_SIZE_1=$(stat -c%s wkfile1.tar)
sed -i "s/substitute_pack_size_1/$PACK_SIZE_1/" appsetup1.sh

tar -c -z --numeric-owner -f wkfile2.tar.gz $TMP_SETUP_REPO $TMPSETUP_LIST $TMPSETUP_PIN $APTKEY1_FILE $APTKEY2_FILE $CUSTOMHOOK_POSTSETUP_SCRIPT $CUSTOMHOOK_PREAPT_R_SCRIPT $CUSTOMHOOK_POSTSETUP_R_SCRIPT
PACK_SIZE_2=$(stat -c%s wkfile2.tar.gz)
sed -i "s/substitute_pack_size_2/$PACK_SIZE_2/" appsetup1.sh

cat appsetup1.sh wkfile2.tar.gz wkfile1.tar >> setup_${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.esh

#----------------------------------------------------------------------------------------------
# Source user's post-pack script
#----------------------------------------------------------------------------------------------
if [ $POSTPACK_SH = "1" ]
then
 . $CFGFILE_DIR/sourced.postpack
fi

#----------------------------------------------------------------------------------------------
# Add created packages to repository
#----------------------------------------------------------------------------------------------
if [ "$RPRP_PROCESS" = "1" ]
then
 read -t5 -p "Press 'r' + 'Enter' to run reprepro to local repository ... " RDVAL
 if [ "$RDVAL" = "r" ] ; then
  reprepro --ask-passphrase -V -b $RPRP_DBLOCATION remove $RPRP_CODENAME ${PK_NAME}
  reprepro --ask-passphrase -V -b $RPRP_DBLOCATION includedeb $RPRP_CODENAME ${PK_NAME}_${PK_VERS}-${PK_RELEASE}_$PK_ARCH.deb
 fi
fi

#----------------------------------------------------------------------------------------------
# Clean, finish
#----------------------------------------------------------------------------------------------
cd $CFGFILE_DIR
#rm -rf $OUT_DIR
mkdir -p $OUT_DIR
mv $FSTMPDIR/setup_*.esh $OUT_DIR
mv $FSTMPDIR/${PK_NAME}_${PK_VERS}-${PK_RELEASE}_$PK_ARCH.deb $OUT_DIR
# todo - if error kdialog it
# INSTNOW=$(kdialog --title "Done" --icon "package_settings" --yesno "Done. Application installer has been built:\n$(readlink -f $OUT_DIR/setup_${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.esh)\nRun it now ?")
# if [ "$?" = "0" ] ; then
#  ( sh $(readlink -f $OUT_DIR/setup_${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.esh) 2>&1 ) > /dev/null &
# fi
SHOWNOW=$(kdialog --title "Done" --icon "package_settings" --yesno "Done. Application installer has been built:\n$(readlink -f $OUT_DIR/setup_${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.esh)\nOpen installer location ?")
if [ "$?" = "0" ] ; then
 ( konqueror $(dirname $(readlink -f $OUT_DIR/setup_${PK_NAME}_${PK_VERS}-${PK_RELEASE}_${PK_ARCH}.esh)) 2>&1 ) > /dev/null &
fi
cleartmp #rm -rf $FSTMPDIR ##### ADDED BY DAI #####
