#!/bin/bash
# A set of functions that can be called from any script running
# within porteus.

TITLE="Porteus script"
DMP=/tmp/.dmp
MENU=/tmp/.menu

# $1=text
message(){
Xdialog --title "$TITLE" --left --msgbox \
"$1" 0 0
}

# $1=text
yesno(){
Xdialog --title "$TITLE" --left --yesno "$1" 0 0
}

# $1=text  ANSW=answer
input(){
Xdialog --title "$TITLE" --inputbox "$1" "" 0 0 2> $DMP
ANSW=`cat $DMP`
rm $DMP
}

# $1=text $2=time
infobox(){
Xdialog --title "$TITLE" --no-buttons --left --infobox "$1" 0 0 "$2"
}

# $1=file $2=text
menu(){
for a in `cat $1`; do
  echo $a $a >> $MENU
done

Xdialog --title "$TITLE" --no-tags --menubox "$2" 0 0 0 `cat $MENU` 2> $DMP
ANSW=`cat $DMP`
rm $DMP $MENU
}

# $1=text ANSW=result password
password(){
Xdialog --title "$TITLE" --password --inputbox "$1" "" 0 0 2> $DMP
ANSW=`cat $DMP`
rm $DMP
}

# $1=start directory
get_directory(){
Xdialog --title "$TITLE" "$1" 0 0 2> $DMP
ANSW=`cat $DMP`
rm $DMP
}

# $1=start directory
get_file(){
Xdialog --title "$TITLE" "$1" 0 0 2> $DMP
ANSW=`cat $DMP`
rm $DMP
}

# $1=start directory
get_date(){
Xdialog --title "$1" 0 0 2> $DMP
ANSW=`cat $DMP`
rm $DMP
}

# $1=file $2=text
download() {
wget $1 -o $DMP &
(
while [ "$i" != "98" -a "$f" == "" ]; do
   echo $i
   i=$(cat $DMP | grep "%" |awk '{print $(NF-2)}'|sed 's/\(.*\)./\1/'|tail -n1)
   f=$(grep "100%" $DMP)
   sleep 0.1
done
) | Xdialog --title "$TITLE" --gauge "Downloading $2...." 10 60
rm -f $DMP
}