#!/bin/sh
#] 
#] *********************
#] $ bash "$d_bin"'starter/2_app.sh'  - select applications, window [Size, Posn, move]
#]      programs -> [filemgr, terminal, browser, editor, email, office, pdf] 
# www.BillHowell.ca  08Jan2017 initial from RaspPi
# 21Aug2023 This file was separated from the "quick switch" '2_app.sh', so it is much easier for my other scripts to use the finctions here...
# 21Aug2023 could use app_winPosnSize_open_winID?  app_start simpler?



#WARNING!:  [perpetual, deep] flaw was lack of escaping of "problematic regexpr" chrs :
#	eg win_PosnSize="0,$1,$2,$3,$4" -> should be "0\,$1\,$2\,$3\,$4" 
#	(or not so practical : "0"','"$1"','"$2"','"$3"','"$4")
# but win_move_last- just use space-separated quoted strs : 0  "$left"  "$top"  "$width"  "$height"  
# 21Aug2021 WARNING! in case of spaces in [dir, fname] not apo-surround :
#		bash "$goApp"	"$delay_brs"	"browser"	"'""$d_temp""'"	"'""$fCaptionRefcHtml""'"
#48************************************************48




#24************************24
# Table of Contents, generate with :
# $ grep  "^#]"  "$d_bin"'starter/2_app.sh' |  sed "s/^#\] /#  /" 
#
#  *********************
#  $ bash "$d_bin"'starter/2_app.sh'  - select applications, window [Size, Posn, move]
#       programs -> [filemgr, terminal, browser, editor, email, office, pdf] 
#  basic screen measures
#  winID_from_name()    - 
#  winID_last()  - 
#  win_move_last()  - 
#  win_left()  - 
#  win_rght()    - 
#  win_HalfUpp()    - 
#  win_HalfDwn()    - 
#  win_leftUpp()    - 
#  win_leftDwn()    - 
#  win_tab()  - NOT to be used as there should be no movement of window!!! 
#  win_full()  - 
#  win_left_LO()  - 
#  win_rght_LO()    - 
#  win_full_LO()  - 
#  win_ToDos()  - 
#  win_calendar()  - 
#  win_ToDosClass()  - 
#  win_mnthcnt()  - 
#  delayer()  - 
#  xargsit()  - 
#  app_start()   - 


#24************************24
# setup

# no headers needed


#24************************24


# select screen size OR[HDMI, HPlaptop] 
terminal=$( cat  "$d_bin"'starter/9 monitor selection OR[HDMI, HPlaptop].txt'  )



#08********08
# code


#] basic screen measures
# I should use xdotools to determine 

if   [[ "$HOWELL_computer" = "Suse" || "$HOWELL_computer" = "LinuxMint"  || "$HOWELL_computer" = "Ubuntu" ]]; then
	if		[ "$terminal" = 'HDMI' ]; then 
		width_HDMI=1920
		height_HDMI=1080
	elif  [ "$terminal" = 'HPlaptop'   ]; then 
		width_HDMI=1600
		height_HDMI=900
	fi
   height_menu_top=20
   height_menu_btm=20
   width_HDMI_half="$(($width_HDMI/2-5))"
	height_working="$(($height_HDMI-$height_menu_top-$height_menu_btm))"
   positn_top="$height_menu_top"
   # half-[width,height] windows
	height_HDMI_half="$(($height_HDMI/2))"    # +2*$height_menu_top
	height_working_half="$(($height_HDMI_half-$height_menu_top-$height_menu_btm))"
   positn_halfDwn="$(($height_HDMI_half-$height_menu_top-$height_menu_btm))"
   X_right="$(($width_HDMI_half+10))"
   # Applications - special adjustments
   top_LO="$(($height_menu_top+30))"
   height_LO="$(($height_HDMI-$top_LO))"
#  width_ToDos=1110							# <27Sep2021 "$(($width_HDMI-$width_calendar-$width_mnthcnt-20))"
#  width_calendar=800						# <27Sep2021 380
#  width_mnthcnt=800							# <27Sep2021 425
#  left_calendar=1180						# <27Sep2021 "$(($width_ToDos+10))"
#  left_mnthcnt=1180							# <27Sep2021 "$(($left_calendar+$width_calendar+10))"
   width_ToDos="$width_HDMI_half"
   width_calendar="$width_HDMI_half"
   width_ToDosClass="$width_HDMI_half"
   width_mnthcnt="$width_HDMI_half"
   left_calendar="$X_right"
   left_mnthcnt="$X_right"
elif [ "$HOWELL_computer" = "RaspPi" ]; then
   width_HDMI=1824
   width_HDMI_half="$(($width_HDMI/2-5))"
   height_HDMI=960
   height_menu_top=16
   height_menu_btm=0
   height_working="$(($height_HDMI-$height_menu_top-$height_menu_btm))"
   X_right="$(($width_HDMI_half+10))"
   top_LO="$(($height_menu_top+30))"
   height_LO="$(($height_HDMI-$top_LO+15))"
   width_calendar=380
   width_mnthcnt=420
   width_ToDos="$(($width_HDMI-$width_calendar-$width_mnthcnt-20))"
   left_calendar="$(($width_ToDos+10))"
   left_mnthcnt="$(($left_calendar+$width_calendar+10))"
else echo "Error : HOWELL_computer= $HOWELL_computer unknown  in '/home/bill/bin/starter/2_app.sh' window specs"
fi

# LibreOffice specials :
#echo "height_LO  	 width_calendar   width_mnthcnt   width_ToDos   left_calendar   left_mnthcnt"
#echo "$height_LO  $width_calendar  $width_mnthcnt  $width_ToDos  $left_calendar  $left_mnthcnt"


# tail - output the last part of files (-n --lines=K)
# cut - remove sections from each line of files  -d, --delimiter=DELIM,  use DELIM instead of TAB for field delimiter

#] winID_from_name()    - 

	winID_from_name()  
{ wmctrl -l | grep "$1" | tail -1 | cut -f1 -d" "  
} 

#] winID_last()  - 

	winID_last()
{ wmctrl -l |             tail -1 | cut -f1 -d" "  
} 


#] win_move_last()  - 

	win_move_last()
{  
	gravi="$1"
	XPosn="$2"
	YPosn="$3"
	width="$4"
	hight="$5"
   winID=$(winID_last)
	#	echo "win_move_last winID = $winID"
   sleep 1s
   win_PosnSize="$gravi,$XPosn,$YPosn,$width,$hight"
	#	echo "win_move_last win_PosnSize = $win_PosnSize"
   wmctrl -i -r "$winID" -e  "$win_PosnSize"
   sleep 1s
}  


#] win_left()  - 

	win_left()
{  left=0; 					top=$height_menu_top;  width=$width_HDMI_half;		height=$height_working
}  

#] win_rght()    - 

	win_rght()  
{  left=$X_right;			top=$height_menu_top;  width=$width_HDMI_half;		height=$height_working  
}  

#] win_HalfUpp()    - 

	win_HalfUpp()  
{  left=0;					top=$height_menu_top;  width=$width_HDMI;				height=$height_working_half  
}  

#] win_HalfDwn()    - 

	win_HalfDwn()  
{  left=0;					top=$positn_halfDwn;		width=$width_HDMI;			height=$height_working_half  
}  

#] win_leftUpp()    - 

	win_leftUpp()  
{  left=0;					top=$height_menu_top;	width=$width_HDMI_half;		height=$height_working_half  
}  

#] win_leftDwn()    - 

	win_leftDwn()  
{  left=0;					top=$positn_halfDwn;		width=$width_HDMI_half;		height=$height_working_half  
}  

#] win_tab()  - NOT to be used as there should be no movement of window!!! 

	win_tab()
{  left=0;					top=$height_menu_top;	width=$width_HDMI;			height=$height_working
}  

#] win_full()  - 

	win_full()
{  left=0;					top=$height_menu_top;	width=$width_HDMI;			height=$height_working
}  

#] win_left_LO()  - 

	
	win_left_LO()
{  
	left=0;					top=$top_LO;				width=$width_HDMI_half;		height=$height_LO
}  

#] win_rght_LO()    - 

	win_rght_LO()  
{  left=$X_right;			top=$top_LO;				width=$width_HDMI_half;		height=$height_LO
}  

#] win_full_LO()  - 

	win_full_LO()
{  left=0;					top=$top_LO;				width=$width_HDMI;			height=$height_LO
}  

#] win_ToDos()  - 

	win_ToDos()
{  left=0;					top=$top_LO;				width=$width_HDMI;			height=$height_LO  
}  

#] win_calendar()  - 

	win_calendar()
{  left=$left_calendar;	top=$top_LO;				width=$width_calendar;		height=$height_LO
}  

#] win_ToDosClass()  - 

	win_ToDosClass()
{  left=$left_mnthcnt;	top=$top_LO;				width=$width_mnthcnt;		height=$height_LO
}  

#] win_mnthcnt()  - 

	win_mnthcnt()
{  left=$left_mnthcnt;	top=$top_LO;				width=$width_mnthcnt;		height=$height_LO
}  


# IMPORTANT -> ?RaspPi? bash REQUIRES "=" instead of "==" for strings!!!
# from "/media/bill/HOWELL_BASE/Qnial/MY_NDFS/windows -  position, size, move, desktop opens.ndf"
#    operator : "fileOpen  IS OP  app  d_name  f_name"
#	browser	:= '/usr/bin/firefox '  ;  
#	email		:= 'evolution '   ;  		% ONLY for LMDE!! ; 
#	email		:= 'browser '   ;  			% for [Suse, Raspi] ; 
#	fileMgr	:= 'nemo '  ;  				% also [dolphin,krusader]  ;  
#	libroff	:= '/usr/lib/libreoffice/pr#ogram/soffice -o '  ; 
#	terminal	:= '/usr/bin/gnome-terminal '  ;  
#	editor	:= 'kwrite '  ;	 			% kwrite is my favourite, gedit is backup ; 

# search "Linux and if conditional for multiple string compares"
# https://stackoverflow.com/questions/21157435/bash-string-compare-to-multiple-correct-values
#Instead of saying:
#if [ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]; then
#say:
#if [[ "$cms" != "wordpress" && "$cms" != "meganto" && "$cms" != "typo3" ]]; then
#You might also want to refer to Conditional Constructs.
#answered Jan 16 '14 at 9:07, devnull

#] apps selection (preferences)

if   [[ "$HOWELL_computer" = "LinuxMint"  || "$HOWELL_computer" = "Ubuntu"  ]]; then
	filemgr="nemo"
#  filemgr="dolphin"
	terminal="gnome-terminal"
	browser="firefox"
	editor="geany -i "
	email="thunderbird"
#	editor="emacs"
#	editor="kwrite" ->  27Nov2019 kwrite crashes Linux Mint Debian Edition (LMDE)
	libroff="libreoffice"
	pdf="xreader"
elif [ "$HOWELL_computer" = "RaspPi" ]; then
	filemgr="pcmanfm"
	terminal="lxterminal"
#  browser="epiphany"
	browser="firefox"
	editor="emacs"
	email="firefox "
	libroff="libreoffice"
	pdf="xpdf"
elif [ "$HOWELL_computer" = "Suse" ]; then
#  filemgr="nemo"
	filemgr="dolphin"
	terminal="konsole"
	browser="falkon "			# 28Sep2022 changed from browser="firefox ", due to vastly delyed connection problem
									#	from start : falkon seems to open an extra window  first, then adds tabs?
#  editor="emacs"
	editor="kwrite"
	email="evolution"
	libroff="libreoffice"
	pdf="xreader"
else 
	echo "Error : HOWELL_computer= $HOWELL_computer unknown  in '/home/bill/bin/starter/2_app.sh' appNames"
fi


#*****************************************
# delay times

# 31Jan2018 Standard application delay times
# may be overridden
# see "$d_bin"'starter/0_start menu.sh' !!
#		export  delay_fmg=" 3s"
#		export  delay_trm=" 2s"
#		export  delay_brs=" 4s"
#		export  delay_edt=" 3s"
#		export  delay_LOf=" 4s"
#		export  delay_pdf=" 4s"


#] delayer()  - 

	delayer()
{  
	delayT="$( echo "$1" |  sed 's/[^0-9]*//g' )"
if	[ "$HOWELL_computer" = "RaspPi" ]; then
	delayT="$(($delayT*2))"
fi
	sleep "$delayT" 
	#	echo  "$delayT"
}

# test - middle expression isn't useful
# echo " 3s" |  sed 's/ //'  |  sed 's/[^0-9]*//g'


#] xargsit()  - 	$1			$2			$3		$4
#						delay		appcln	path	options

	xargsit()
{  
	lag="$1"
	app="$2"
	pth="$3"
	opt="$4"

if	[ "$HOWELL_computer" = "RaspPi" ]; then
	if    [  "$app" = "lxterminal" ]; then
		echo  "$app             &"   |  xargs  -0  sh  -c  
   elif  [  "$app" = "leafpad"    ]; then
      echo  "$app  --tab-width=3  \"$pth\"  &"   |  xargs  -0  sh  -c  
   else
		echo  "$app  $opt  $pth     &"   |  xargs  -0  sh  -c  
   fi
else
		cmd="$app  $opt  "'"'$pth'"'"    & "  
		#	echo  "cmd=  $cmd"  
		eval  "$cmd"  
fi
delayer "$lag"
}  




# test - comment out "app_start" below, and visa versa
# $ bash "/media/bill/SWAPPER/bin/starter/2_app.sh"
# These examples work!!
#xargsit  "1s"  "ls"  "/media/bill/SWAPPER/bin"  "-1"  
#xargsit  "1s"  "gnome-terminal"  ""  ""
#xargsit  "1s"  "nemo"  "/media/bill/SWAPPER/Lucas - Universal Force"  ""  

# Firefox - test of bash "/home/bill/bin/firehowell.sh"
# firehowell.sh  " --new-window "  "http://www.ecomp.poli.br/~wcci2018/"
# firefox  --new-window  "http://www.ecomp.poli.br/~wcci2018/"

# for simple one-commands, xargs and bash are similar : 
# $ echo  "ls  -1  \"/media/bill/SWAPPER/bin\" &"   |  xargs  -0  sh  -c
# $ echo  "ls  -1  \"/media/bill/SWAPPER/bin\" &"   |  bash
# but use xargs and get used to it


#****************************
# run apps

# https://www.systutorials.com/docs/linux/man/1-xargs/
#    I didn't really use the URL in detail
# xargs_it   $1     $2       $3       $4
#            delay  command  options  argument_string
#else echo  -n  "$3"   |  xargs   --max-args=1  $2  "\"$4\""
#fox
# search "Linux and how do I pass options to a command for xargs"
# https://stackoverflow.com/questions/34669239/how-to-pass-command-with-parameters-to-xargs
#The -c flag to sh only accepts one argument while xargs is splitting the arguments on whitespace - that's why the double #quoting works (one level to make it a single word for the shell, one for xargs).
#If you use the -0 or null argument to xargs your particular case will work:
#echo ls -l -a / | xargs -0 sh -c
#answered Jan 8 '16 at 4:18, darklion
# ...
#Source: http://www.unixmantra.com/2013/12/xargs-all-in-one-tutorial-guide.html
#as mentioned by @darklion, -I denoted the argument list marker and -c denotes bash command to run on every input line to #xargs.
#Simply print the input to xargs:
#ls -d */ | xargs echo
#One at a time
#ls -d */ | xargs -n1 echo
#More operations on every input:
#ls -d */  | xargs -n1 -I {} /bin/bash -c ' echo {}; ls -l {}; '
#You can replace {} with customized string as:
#ls -d */  | xargs -n1 -I file /bin/bash -c ' echo file; ls -l file; '
#answered Jan 8 '16 at 7:42, dhirajforyou


#] app_start()  - run the app with [option, input]s 

	app_start() 
{  
	delay="$1"
	app="$2"
	dir="$3"
	fname="$4"
	pth="$3$4"
	
	#	echo  "delay=  $1"
	#	echo  "app=    $2"
	#	echo  "dir=    $3"
	#	echo  "fname=  $4"
	#	echo  "pth=    $3$4"

if	[ "$app" = "filemgr"  ]; then 
	if	[ "$HOWELL_computer" = "RaspPi" ]; then
		  options=" --new-win "
	else options=""
	fi
	#xargsit		delay			app			pth		options
	xargsit		"$delay"		"$filemgr"	"$pth"	" $options "  
	win_left 
elif [ "$app" = "fmgrRght"  ]; then 
	if	[ "$HOWELL_computer" = "RaspPi" ]; then
		  options=" --new-win "
	else options=""
	fi
	xargsit  "$delay"  "$filemgr"	"$pth"  " $options "  
	win_rght 
elif [ "$app" = "terminal" ]; then 
	xargsit  "$delay"  "$terminal"	"$pth"  ""  
	win_left 
# 15Jan2018 RaspPi error - seemingly from options arg.  I reverted to simple form
#  xargsit  "$terminal"  
#  sleep "$delay"  
# 19Jan2018 - reverted back
elif [ "$app" = "termRght" ]; then 
	xargsit  "$delay"  "$terminal"	"$pth"  ""  
	win_rght 
elif [ "$app" = "browFull" ]; then 
	xargsit  "$delay"  "$browser"	"$pth"  " 2>/dev/null  --new-window "
	win_full
elif [ "$app" = "browLeft" ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_left 
elif [ "$app" = "browRght" ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_rght 
elif [ "$app" = "browtab"  ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  "  
elif [ "$app" = "browHU"   ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_HalfUpp 
elif [ "$app" = "browHD"   ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_HalfDwn 
elif [ "$app" = "browLU"   ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_leftUpp 
elif [ "$app" = "browLD"   ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_leftDwn 
elif [ "$app" = "browRU"   ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_rghtUpp 
elif [ "$app" = "browRD"   ]; then 
	xargsit  "$delay"  "$browser"		"$pth"  " 2>/dev/null  --new-window "
	win_rghtDwn 
elif [ "$app" = "email"    ]; then 
	xargsit  "$delay"  "$email"		"$pth"  " 2>/dev/null  "
	win_left 
elif [ "$app" = "editRght"   ]; then 
	xargsit  "$delay"  "$editor"		"$pth"  ""
	win_rght 
elif [ "$app" = "editLeft" ]; then 
	xargsit  "$delay"  "$editor"		"$pth"  ""
	win_left 
elif [ "$app" = "libroff"  ]; then 
	xargsit  "$delay"  "$libroff"		"$pth"  ""
	win_rght_LO 
elif [ "$app" = "librLeft" ]; then 
	libreoffice								"$pth"
	delayer "$delay"
	win_left_LO 
elif [ "$app" = "librHU"   ]; then 
	xargsit  "$delay"  "$libroff"		"$pth"		""
	win_HalfUpp 
elif [ "$app" = "ToDos"    ]; then 
	libreoffice								"$pth"  
	delayer "$delay"
	win_ToDos 
elif [ "$app" = "calendar" ]; then 
	libreoffice								"$pth"  
	delayer "$delay"
	win_calendar 
elif [ "$app" = "ToDosClass" ]; then 
	libreoffice								"$pth"  
	delayer "$delay"
	win_ToDosClass 
elif [ "$app" = "mnthcnt"  ]; then 
	libreoffice								"$pth"  
	delayer "$delay"
	win_mnthcnt 
elif [ "$app" = "pdfLeft"      ]; then 
	xargsit  "$delay"   "$pdf"			"$pth"  ""  
	win_left
elif [ "$app" = "pdfRght"  ]; then 
	xargsit  "$delay"   "$pdf"			"$pth"  ""  
	win_rght
elif [ "$app" = "gimp"     ]; then 
	gimp										"$pth"  ""  
	delayer "$delay"
	win_rght
else 
	echo "app_laucher.sh - app name $app not recognized"
fi

sleep "$delay"
#	echo "app_start	[gravity=0,left,top,width,height] 
if [ "$app" <> "browtab"  ]; then 
	win_move_last  0  "$left"  "$top"  "$width"  "$height"  
fi

}  


# tests
# bash "$d_bin"'starter/2_app.sh'  "3s"  "editor"		"$d_web""economics, markets/"   "market news.txt" 

# app_start args in "$d_start"'2_app0.sh' :
#				delay		appln		dir		filnm
#app_start	"$1"		"$2"		"$3"		"$4"  







# enddoc
