function mkhelp {

# make help text

# echo "Collecting data for 'Help to select drive'"
 hlptxt="/tmp/help-mkusb.txt"
 > "$hlptxt"
 echo "Collected data to help select target drive" >> "$hlptxt"
 echo "_______________________________________________" >> "$hlptxt"
 echo " " >> "$hlptxt"
 #echo "lshw -class disk #####" >> "$hlptxt"
 #lshw -class disk >> "$hlptxt"
 echo "list_drives     ##### function built into $prgnam" >> "$hlptxt"
 list_drives >> "$hlptxt"
 echo "-----------------------------------------------" >> "$hlptxt"
 echo "df|grep \"/dev/[^f]d\"|sort #####" >> "$hlptxt"
 df|grep "/dev/[^f]d"|sort >> "$hlptxt"
 echo "-----------------------------------------------" >> "$hlptxt"
 echo "ls -l /dev/disk/by-id| grep [a-z]$|tr -s ' ' ' ' \\" >> "$hlptxt"
 echo "|cut -d ' ' -f 9,11|sort -k2|grep -e \^a -e \^u \\" >> "$hlptxt"
 echo "|sed 's#../..#/dev#' ##### also empty mass storage devices" >> "$hlptxt"
 ls -l /dev/disk/by-id| grep [a-z]$|tr -s ' ' ' ' \
 |cut -d ' ' -f 9,11|sort -k2|grep -e \^a -e \^u \
 |sed 's#../..#/dev#' >> "$hlptxt"
 echo "-----------------------------------------------" >> "$hlptxt"
 echo "lsusb|sort -k 4,4 -k 1 #####" >> "$hlptxt"
 lsusb|sort -k 4,4 -k 1 >> "$hlptxt"
 echo "-----------------------------------------------" >> "$hlptxt"
 echo "fdisk -lu 2>/dev/null|grep \"/dev/[^f]d\"|sort #####" >> "$hlptxt"
 fdisk -lu 2>/dev/null|grep "/dev/[^f]d"|sort >> "$hlptxt"
 echo "-----------------------------------------------" >> "$hlptxt"
 echo "parted -ls #####" >> "$hlptxt"
 parted -ls  >> "$hlptxt"
 echo "-----------------------------------------------" >> "$hlptxt"
 echo "blkid|sort #####" >> "$hlptxt"
 blkid|sort >> "$hlptxt"
 echo "-----------------------------------------------" >> "$hlptxt"
 echo "lsblk -f #####" >> "$hlptxt"
 lsblk -f >> "$hlptxt"
 #echo "--------------------------------------------------------------------"
# echo "This help text is read with  less $hlptxt"
 #### xterm -geometry 120x40 -title "less $hlptxt" -e less "$hlptxt" &

 btitlh="$version - Help info to select drive"
 xtitle="Use Arrow PgUp PgDn Home End keys to scroll this help info"

 dialog --backtitle "$btitlh" --title "$xtitle"\
 --exit-label "OK" \
 --no-shadow --textbox "$hlptxt" 0 0
}

#######################################################################
#######################################################################

function list_drives {

# parameter $1 for usbonly (true/false)

cnt1=0
cnt2=0

tmpfil=$(mktemp)
ls -l /dev/disk/by-id| grep [a-z]$|tr -s ' ' ' '|cut -d ' ' -f 9,11 \
|sort -k2|grep -e \^a -e \^u|sed 's#../..#/dev#' > "$tmpfil"

#cat "$tmpfil"

while read name device
do
 cnt1=$(($cnt1 + 1))
 nam1[$cnt1]="${name%_*}"
nam1[$cnt1]=${nam1[$cnt1]:0:36}
 dev1[$cnt1]="$device"
# /bin/echo -e "${nam1[$cnt1]}\0011${dev1[$cnt1]}"
done < "$tmpfil"

lsblk -db|grep disk|tr -s ' ' ' '| cut -d ' ' -f1-4 |sort > "$tmpfil"

while read device dum1 dum2 bytesize
do
 cnt2=$(($cnt2 + 1))
 dev2[cnt2]="$device"
 mbsize=$((($bytesize+500000)/1000000))
 gbsize=$((($bytesize+500000000)/1000000000))
 if [ $mbsize -lt 32000 ]
 then
  prsize[$cnt2]="${mbsize}MB"
 else
  prsize[$cnt2]="${gbsize}GB"
 fi
 device[$cnt2]="/dev/$device"
# echo "${device[$cnt2]}" "${prsize[$cnt2]}"
done < "$tmpfil"

rm "$tmpfil"

if [ $cnt1 -ge $cnt2 ]
then
 count=$cnt1
else
 count=$cnt2
fi
strsize=0
for (( i=1; i<=$count ; i++ ))
do
 if [ ${#nam1[$i]} -gt $strsize ]
 then
  strsize=${#nam1[$i]}
 fi
done
#echo $strsize
for (( i=1; i<=$count ; i++ ))
do
 addsize=$(($strsize - ${#nam1[$i]}))
 blank='                                                    '
 spacer=${blank:0:$addsize}
# echo $addsize
# echo "xxx${blank:0:$addsize}xxx"

 printsize=
 for (( j=1; j<=$count ; j++ ))
 do
#  echo "${dev1[$i]} ?==?  /dev/${dev2[$j]}"
  if [ "${dev1[$i]}" ==  "/dev/${dev2[$j]}" ]
  then
   printsize="${prsize[$j]}"
#   echo "printsize=$printsize"
  fi
 done

 if [ "$printsize" != "" ]
 then
  string="Name: ${nam1[$i]}${spacer}  Dev: ${dev1[$i]}  Size: $printsize"
  if [ "$1" == "true" ]
  then
   usbstring="${string/Name\:\ usb\-/USB: }"
   if [ "$usbstring" != "$string" ]
   then
    /bin/echo -e "$usbstring"
   fi
  else
    /bin/echo -e "$string"
  fi
 fi
done
}

#######################################################################
#######################################################################

function check_packages {

# check pipe view and offer installation

if ! "$checked_pack"
then

 which pv > /dev/null
 if [ "$?" == "0" ]
 then
  pvplug=true
 else
  echo "The program 'pv' can show the progress during the installation."
  read -p "Do you want to install it? (y/N)" pvi
  if [ "$pvi" == "y" ]
  then
   echo "apt-get install pv"
   apt-get install pv
  fi
  which pv > /dev/null
  if [ "$?" == "0" ]
  then
   pvplug=true
  else
   pvplug=false
  fi
 fi
# echo $pvplug

# check menu packages and offer installation

 menu_package_installer needs dialog "mkusb version 7.4 works without it"
 menu_package_installer wants zenity "mkusb can work without it."
# menu_package_installer wants smartctl "mkusb can work without it." smartmontools

 zenity --info --text="test" --timeout=1 2> /dev/null
 if [ $? -eq 5 ]
 then
 zenityplug=true
 else
 zenityplug=false
 fi

 checked_pack=true
fi
}

#######################################################################
#######################################################################

function menu_package_installer {

# $1 "needs" or "wants"
# $2 program name
# $3 informative test string
# $4 package name (if different from program name)

# Example (shows full syntax but smartctl is *not* used by mkusb)
# menu_package_installer wants smartctl "mkusb can work without it." smartmontools


if [ "$4" == "" ]
then
 package="$2"
else
 package="$4"
fi
which "$2" > /dev/null
if [ "$?" != "0" ]
then
 echo "mkusb $1 the program '$2' for the menus."
 read -p "Do you want to install it? (y/N)" pvi
 if [ "$pvi" == "y" ]
 then
  echo "Prepare by making the system up to date ..."
  echo "apt-get update && apt-get upgrade"
  sleep 2
  apt-get update && apt-get upgrade
  echo "Install program $2 ... in package $package"
  echo "apt-get install --no-install-recommends $package"
  sleep 2
  apt-get install --no-install-recommends "$package"
 fi
 which "$2" > /dev/null
 if [ "$?" != "0" ]
 then
  echo "$version $1 $2 but it is not installed."
  echo "$3"
  if [ "$1" == "needs" ]
  then
   exit
  else
   read -t 3
  fi
 fi
fi
}

#######################################################################
#######################################################################

# --colors
#    Interpret embedded "\Z" sequences in the dialog text by the following
# character, which tells dialog to set colors or video attributes:
# 0 through 7 are the ANSI used in curses:
#   0     1     2      3      4       5      6         7
# black, red, green, yellow, blue, magenta, cyan and white respectively.
#
# Bold is set by 'b', reset by 'B'.
# Reverse is set by 'r', reset by 'R'.
# Underline is #set by 'u', reset by 'U'.
# The settings are cumulative, e.g., "\Zb\Z1" makes the following text
# bold (perhaps #bright) red. Restore normal settings with "\Zn". 

########################################################################
########################################################################

function init_dialog {

# create the file for run-time configuration, with text names for colours

dialog --create-rc $HOME/.dialogrc

# initiate colours

sed -i 's/use_colors.*/use_colors = ON/' $HOME/.dialogrc
sed -i 's/screen_color =.*/screen_color = (CYAN,BLUE,ON)/' $HOME/.dialogrc
}

########################################################################
########################################################################

function warning_dialog {

# set red warning screen background

sed -i 's/screen_color =.*/screen_color = (yellow,red,ON)/' $HOME/.dialogrc
}

########################################################################
########################################################################

function normal_dialog {

# set blue normal screen background

sed -i 's/screen_color =.*/screen_color = (CYAN,BLUE,ON)/' $HOME/.dialogrc
}

########################################################################
########################################################################



#######################################################################
#######################################################################

function select_device {

# version for mkusb (ported from OBI)

# 1 parameter:
#echo "in select_device \$1=$1"

p1="$1"

# make command and run command, 'here we go'

ans=-1
choice=1
target=
LC_ALL=C
LANG=C
tmpstr=

ltest0=$(grep -m 1 " / " /etc/mtab|cut -d ' ' -f 1)
if [ "$ltest0" == "/cow" ] || [ "$ltest0" == "aufs" ] || [ "$ltest0" == "rootfs" ]
then
 ltest1=$(grep ' /isodevice' /etc/mtab|cut -c 5-8)
 if [ "$ltest1" != "" ]
 then
  livedrive=$ltest1
 else
  ltest1=$(grep ' /cdrom' /etc/mtab|cut -c 5-8)
  if [ "$ltest1" != "" ]
  then
   livedrive=$ltest1
  else
   ltest1=$(grep ' iso9660' /etc/mtab|grep -v ' /media'|grep -v ' /mnt'|cut -c 5-8)
   if [ "$ltest1" != "" ]
   then
    livedrive=$ltest1
   else
    livedrive="not_found"
   fi
  fi
 fi
elif [ "${ltest0/\/dev\/[^f]d}" != "$test0" ]
then
 livedrive=$(grep " / " /etc/mtab|cut -c 5-8)
else
 livedrive="not_found"
fi
 #echo ltest0=$ltest0
 #echo ltest1=$ltest1
 #echo livedrive=$livedrive


 tmpstr=$(list_drives)

#echo $tmpstr
#read -p "press enter to continue"

# check if all drives should be available at start (not only USB)
# usbonly default true, can be changed with parameter2 'all'

if [ "$p1" == "all" ] || [ "$p1" == "anh" ]
then
 usbonly=false
else
 usbonly=true
fi

tmpstr="$tmpstr\nBooted from: /dev$livedrive"

while [ "$ans" == "u" ] || [ $ans -lt 0 ]
do

#echo "usbonly=$usbonly"

 if $usbonly; then ufl=usb; else ufl=d;fi

# usbstr=$(for i in $(find /sys/devices -name "[^f]d?"|grep usb|sed s#.*/##)
# do
#  fdisk -lu /dev/$i 2>/dev/null|grep "Disk /dev"
# done |sed 's/Disk/USB device: /'|sort)
 
 dialog --backtitle "$version - Select device - warning and overview" \
 --colors --no-collapse --cr-wrap --no-shadow --msgbox \
"       \Z1WARNING: the device will be completely overwritten\Zn\n
       Get more help to select device at the Starter menu\n
---    \Z1Unmount the target device if mounted\Zn   --------------------\n
$tmpstr\n$usbstr" 0 0

 aind=0
 bind=0
 unset adev
 unset atxt
 unset bvar

#echo "usbonly=$usbonly"
#list_drives "$usbonly"|tr -s ' ' ' '|cut -d ' ' -f 4
#read -p 'Press enter to continue at list_drives'
 
for j in $(list_drives "$usbonly"|tr -s ' ' ' '|cut -d ' ' -f 4)
 do
# echo $j
  busy=0
  for k in $(grep $j /etc/mtab|sed s#\ /.*##)
  do umount $k 2>/dev/null; if [ "$?" == "1" ];then busy=1;fi ;done
  tj="$(list_drives $usbonly|grep $j)"

#  echo "${tj:0:7}"
#  echo "${tj}"
#  read -p "select-device at Warning and Error"
  if [ "${tj:1:7}" == "Warning" ] || [ "${tj:1:5}" == "Error" ]
  then
    tj=$(fdisk -lu "$j" 2>/dev/null|grep "$j":|sed s/\,\ .*//)
  fi

  if [ "$j" == "/dev$livedrive" ]
  then
   adev[$aind]="q"
   atxt[$aind]="\Z5\Zb${tj/Name:/Live:}\Zn"
   aind=$(($aind + 1))
  elif [ "$busy" == "1" ]
  then
   adev[$aind]="q"
   atxt[$aind]="\Z4\Zb${tj/Name:/Busy:}\Zn"
   aind=$(($aind + 1))
  else
   adev[$aind]="$j"
   atxt[$aind]="$tj"
   aind=$(($aind + 1))
  fi
 done
 adev[$aind]="u"
 if $usbonly
 then
  atxt[$aind]="toggle USB-only; show all drives"
 else
  atxt[$aind]="toggle USB-only; hide other drives"
 fi
 aind=$(($aind + 1))
 adev[$aind]="q"
 atxt[$aind]="quit installing"
 aind=$(($aind + 1))
 for (( xi=0; xi < $aind ; xi++ ))
 do
  if [ "${adev[$xi]}" == "u" ]
  then
   bvar[$bind]="u"
  elif [ "${adev[$xi]}" == "q" ]
  then
   bvar[$bind]="q"
  else
   bvar[$bind]=$(($xi+1))
  fi
  bind=$(($bind + 1))
  bvar[$bind]="${atxt[$xi]}"
  bind=$(($bind + 1))
 done

# select device - the Mkusb button to go

#echo "aind=$aind"
#echo "${bvar[@]}"
#read -p 'stop at Go ahead menu'

 ans=$(dialog \
 --backtitle "$version - Select device - and go ahead" \
 --title "Please select a device with the arrow keys, scroll if necessary" \
 --colors --no-shadow --ok-label "Go" --cancel-label "Quit" --no-shadow \
 --menu "Available devices" \
 17 100 $aind "${bvar[@]}"  3>&1 1>&2 2>&3 3>&- )

 echo "ans=$ans"

 if [ "$ans" == "u" ]
 then
  choice=1
  if $usbonly
  then
   usbonly=false
  else
   usbonly=true
  fi
 elif [ "$ans" == "q" ] || [ "$ans" == "" ] || [ $ans -lt 0 ]
 then
  exit
 else
  choice=$(($ans - 1))
  target="${adev[$choice]}"
  cmdsav="${atxt[$choice]}"
 fi
# echo "ans=$ans at end of while loop"
done

# here we go

if [ "$source" == "/dev/zero" ]
then
 doing="wiping\n\n"
else
 doing="installing\n\nthe operating system \Z4$system\Zn\nto "
fi

#echo "$system" "$imagefile" "$target"
#echo "$cmdsav"

cmdsav="${cmdsav/USB\:\ }"
cmdsav="${cmdsav/Name\:\ usb\-}"
cmdsav="${cmdsav/Name\:\ ata\-}"
cmdsav="${cmdsav/Dev\:}"
cmdsav="${cmdsav/Size\:}"
cmdsav=$(tr -s ' ' ' ' <<< "$cmdsav")
cmdsav=$(sed 's/ /  /g' <<< "$cmdsav")

#echo "$cmdsav"
#read -p 'stop'
LANG="$curlang"

 warning_dialog

 ans=$(dialog --cr-wrap --no-collapse --defaultno --colors --no-shadow \
--backtitle "$version - Select device - final WARNING" \
--yesno "$prgnam is prepared to start ${doing}the target  \Z4$cmdsav\Zn\n\n
\Z1This is the \Z7 FINAL WARNING \Z1 before wiping and installing !!!\n
Do you really want to wipe and install to this device?\Zn" 13 "$width" \
 3>&1 1>&2 2>&3 3>&- )

 ans="$?"
 normal_dialog

 if [ "$ans" != "0" ]
 then
  exit
 fi

 swapoff "$target"* 2>/dev/null
 sync

# echo "ans=$ans at end of select_device"

# echo target="$target"
# echo "at end of select_device"
#  exit  #-------------------------------------------  skarpt läge
}

#######################################################################
#######################################################################

function general_file_select {

#echo $imagefile
if test -f "$imagefile"
then
 choice="$imagefile"	
else
 choice="$PWD/"
fi

if $zenityplug
then
ans=$(zenity --file-selection --title "$version - Select source file" \
--filename="$choice" --file-filter=*.tar.?z )
#--filename="$choice" --file-filter=*.{iso,img,img.?z} )
else
ans=$(dialog --backtitle \
'space-bar: copy selection to text-entry box, slash: show directory content' \
--no-shadow --fselect "$choice" 7 200  3>&1 1>&2 2>&3 3>&- )
fi

imagefile="$ans"

# write selection or blank

#echo "imagefile=$imagefile in 'general_file_select'"
#read -p 'press Enter to continue'
}

#######################################################################
#######################################################################

function select_source {

LANG="$curlang"

 menutitle=\
"Find and select among iso and image files, or with a general selector."

if $zenityplug
then
 level="normal, file type filter"
 menutitle="$menutitle\n\n
\Z1Increase the size of the terminal window, particularly the width!\Zn\n
Then the menus to 'select file' work better to show long file names.\n\n
\Z1Avoid spaces and special characters in directory and file names!\Zn"
else
 level="difficult"
 menutitle="$menutitle\n
\Z1Avoid spaces and special characters in directory and file names!\Zn\n
Use a high resolution screen, if possible more than 80 characters wide!\n
Then the menus to 'select file' work better to show long file names.\n\n
\Z1Type text in the bottom text box!\Zn Use the upper text box(es) only to\n
paste text into the bottom window (with the space key). The slash key\n
can be used to 'finish' moving to another directory."
fi

#while [ "$ans" != "q" ] && [ "$ans" != "" ]
#do

ans=$(dialog --no-shadow --backtitle "$version - Select source file" \
--colors --cancel-label Quit \
--title "Please select an action with a letter key or the arrow keys" \
--menu "$menutitle" 18 80 4 \
s "iso and image file selector (easy), no search for linked files" \
l "iso and image file selector (easy), include Linked files (may fail)" \
g "General File selector ($level)" \
q " Quit" \
  3>&1 1>&2 2>&3 3>&- )

 if [ "$ans" == "s" ]
 then
  special_file_select
 elif [ "$ans" == "l" ]
 then
  special_file_select "-L"
 elif [ "$ans" == "g" ]
 then
  general_file_select
 fi
#done

test -f "$imagefile"
if [ $? -ne 0 ]
then
 imagefile=""
fi

# write selection

#echo "imagefile=$imagefile in 'select_source'"
#read -p 'press Enter to continue'

echo "$imagefile" > "$selected"

echo "select_source: imagefile=$imagefile"

ans=""
#LANG=C
}

#######################################################################
#######################################################################

function ch_display {

menutitle=\
"Among files in the previously selected directory tree,\n
select file type to display\n"

ans=$(dialog --no-shadow --backtitle "$version - Select file type to display" \
--colors --cancel-label Quit --default-item "$1" \
--title "Please select an action with a letter key or the arrow keys" \
--menu "$menutitle" 18 80 4 \
a "all files suitable as source files" \
s "only ISO files" \
m "only image files (with and without compression)" \
q " Quit" \
  3>&1 1>&2 2>&3 3>&- )

 if [ "$ans" == "a" ] || [ "$ans" == "s" ] || [ "$ans" == "m" ]
 then
  ch_item="$ans"
 fi
}

