#!/bin/sh

### ensure, weare in the correct folder
mypath=`echo $0|sed 's/[^/]*$//'|sed 's/\/$//'`
if [ "$mypath" = "." ]; then
  mypath=`pwd`
fi
cd $mypath


DISTFOLDER=~/dotpup-wizard

mkdir $DISTFOLDER
HOME=~


#Result must be piped through tail, to avoid errormessages from erratic Gtk-themes
SRCDIR=`Xdialog --stdout --title "Please choose a folder to create the dutpup from" --dselect "$HOME/" 0 0 |tail -n 1`

#echo ----------------
#echo $SRCDIR
#echo ----------------
#exit

#xmessage $SRCDIR
#exit

if [ "$?" != "0" ];then
  Xdialog --title "info" --msgbox "Operation canceled, exiting..." 0 0
  exit 0
fi

SHORTDIR=`echo $SRCDIR | sed "s/$" | sed "s[^/]*/g"`


if [ "$SHORTDIR" = "" ];then
  Xdialog --title "info" --msgbox "Operation canceled, exiting..." 0 0
  exit 0
fi

#xmessage $SRCDIR
#xmessage $HOME
if [ "$SRCDIR" = "$HOME/" ];then
  Xdialog --title "info" --msgbox "You cannot package $HOME, exiting..." 0 0
  exit 0
fi



Xdialog --title "info" --yesno "Create a dotpup from \n$SHORTDIR ?" 0 0

if [ "$?" != "0" ];then
  Xdialog --title "info" --msgbox "Operation canceled, exiting..." 0 0
  exit 0
fi

if [ -d "$DISTFOLDER/$SHORTDIR" ];then

  MSG="A Project $SHORTDIR already exists.\nDelete it?"
  Xdialog --title "info" --yesno "$MSG" 0 0

  if [ "$?" != "0" ];then
    Xdialog --title "info" --msgbox "Operation canceled, exiting..." 0 0
    exit 0
  fi
  rm -rf "$DISTFOLDER/$SHORTDIR"
fi

mkdir "$DISTFOLDER/$SHORTDIR"

#xmessage "$DISTFOLDER/$SHORTDIR/"
#xmessage  "$SRCDIR"
#exit

cp -r "$SRCDIR" "$DISTFOLDER/$SHORTDIR/"



### test, if there is an icon

ICONDIR="/usr/share/icons"
if [ ! -d "$ICONDIR" ];then

  ICONDIR="/usr/share/mini-icons"

fi


if [ ! -f "$DISTFOLDER/$SHORTDIR/icon.xpm" ];then
  Xdialog --title "info" --yesno "You Application has no Icon for a menue, select one?" 0 0
  if [ "$?" = "0" ];then
     ICONFILE=`Xdialog --stdout --title "Please choose the Icon (*.xpm) to add to wxB-Menue" --fselect "$ICONDIR/*.xpm" 0 0 |tail -n 1`
     if [ "$?" = "0" ];then
  
          cp "$ICONFILE" "$DISTFOLDER/$SHORTDIR/$SHORTDIR/icon.xpm"

     else
       Xdialog --title "info" --msgbox "You canceled,continue with no icon..." 0 0
     fi
  fi
fi

### allow to add configuration-entries (e.g. /home/user/.myapp.rc)
MSG="If your App uses (hidden) configurationfiles in another folder, you must add their location now.\n\
If you do not do this, they will not be added when remastering a personal Puppy-CD.\n\n\
Do you want to create this list now?\
"
Xdialog --title "info" --yesno "$MSG" 0 0
if [ "$?" = "0" ];then
     CFGFILE=`Xdialog --stdout --title "Please choose a configuration-file" --fselect "$HOME/" 0 0 |tail -n 1`
     if [ "$?" = "0" ];then
  
          echo "$CFGFILE" >"$DISTFOLDER/$SHORTDIR/config.files"

          RESULT=0
          while [ "$RESULT" = "0" ];do

            Xdialog --title "info" --yesno "add another config-file?" 0 0
            RESULT=$?

            if [ "$RESULT" = "0" ];then
               CFGFILE=`Xdialog --stdout --title "Please choose another configuration-file" --fselect "$HOME/" 0 0 |tail -n 1`
               if [ "$?" = "0" ];then
                    echo "$CFGFILE" >>"$DISTFOLDER/$SHORTDIR/config.files"
               fi
            fi
          done

     else
       Xdialog --title "info" --msgbox "You canceled,continue with no config-file..." 0 0
     fi
fi


### copy dotpup-maker-script and additional files there
cp "$mypath/make.sh" "$DISTFOLDER/$SHORTDIR/make.sh"
cp "$mypath/dotpup.sh-template" "$DISTFOLDER/$SHORTDIR/dotpup.sh-template"


### now run the dotpup-maker-script

cd "$DISTFOLDER/$SHORTDIR"
Xdialog --title "info" --msgbox "Creating DotPup, this may take a while..." 0 0

./make.sh


rm -rf "$DISTFOLDER/$SHORTDIR/make.sh"
rm -rf "$DISTFOLDER/$SHORTDIR/dotpup.sh-template"
rm -rf "$DISTFOLDER/$SHORTDIR/config.files"
rm -rf "$DISTFOLDER/$SHORTDIR/$SHORTDIR"

Xdialog --title "info" --msgbox "finished!" 0 0

rox  "$DISTFOLDER/$SHORTDIR"
