#!/bin/bash
VERSION="1.2" # Version number of wiw

W3MIMGDISPLAY="/usr/lib/w3m/w3mimgdisplay" # Were w3mimgdisplay is located
DEL="-" # The default delimiter between items
DFW="7" # Default font width
DFH="16" # Default font height

CLEAR="0" # If this is set to 1 it clears the teminal before printing the image
FIT="1" # If this is set to 1 then wiw will limit the size of the image
ECHO="0" # If this is set to 1 then wiw will output the command that should be piped into w3mimgdisplay, instead of printing the image
CENTER="0" # If this is set to 1 then wiw will center the image between x,y and mw,mh

# Print help
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
	echo "wiw: w3mimg Wrapper			Version: $VERSION"
	echo "USAGE"
	echo "	wiw [-h --help]			Display this"
	echo "	wiw --size IMAGE		Print the width and height of the image, returned as 'WIDTH HEIGHT'"
	echo "	wiw [ARGS] IMAGE		Display an image on the terminal"
	echo "	wiw --echo [ARGS] IMAGE		Print out the command to produce the desired image"
	echo "	wiw --direct CMD		Pass a command directly into w3mimgdisplay"
	echo "ARGS"
	echo "	--clear				Clear the terminal before printing the image"
	echo "	--no-fit			Don't force the image to fit the terminal"
	echo "	--center			Tells wiw to center the image in the given space"
	echo "	-f width${DEL}height			Set the width and height of the font, used to determine max term size and items specified in U" 
	echo "						(default width = ${DFW}, default height = ${DFH})"
	echo "	-g x${DEL}y${DEL}width${DEL}height		Set the x, y, width, and height of the image"
	echo "						(default x = 0, default y = 0, default w = term width, default h = term height)"
	echo "						You can specify these as a #U (were # is any number) to use the unit of the font"
	echo "						width and height can be set to R, this will preserve image ratio"
	echo "	-s x${DEL}y${DEL}width${DEL}height		Set the x, y, width, and height of the source image"
	echo "						(default x = 0, default y = 0, default w = image width, default h = image height)"
	echo "						You can specify these as a #U (were # is any number) to use the unit of the font"
	echo "						This command is a bit weird, I would reccomend not using it unless really needed"
	echo "	-m width${DEL}height			Set the max width and height the image can consume"
	echo "						(default w = full term width, default h = full term height)"
	echo "						You can specify these as a #U (were # is any number) to use the unit of the font"
	echo "						You can specify these as a #N (were # is any number) to subtract from the max term size"
	echo "	-c width${DEL}height			Set the cursor position for after the image is printed"
	echo "						(default x = 0, default y = line after image)"
	echo "						x and y can be set to LAST, to be placed at the last element"
	echo "	-d delimiter			Set the delimiter between items in the -f, -g, and -s args (MUST BE A SINGLE CHARACTER)"
	echo "						(default delimiter = '${DEL}')"
	exit
# Print the size of the image
elif [[ "$1" == "--size" ]]; then
	echo -e "5;$2" | $W3MIMGDISPLAY
	exit
# Pass a command directly into w3mimgdisplay
elif [[ "$1" == "--direct" ]]; then
	echo -e "$2" | $W3MIMGDISPLAY
	exit
fi



# Cycle through args
while [ $# != 1 ]; do
	if [[ "$1" == "--clear" ]]; then
		CLEAR="1"
	elif [[ "$1" == "--no-fit" ]]; then
		FIT="0"
	elif [[ "$1" == "--echo" ]]; then
		ECHO="1"
	elif [[ "$1" == "--center" ]]; then
		CENTER="1"
	elif [[ "$1" == "-f" ]]; then
		FW=`echo "$2" | cut -d "$DEL" -f1`
		FH=`echo "$2" | cut -d "$DEL" -f2`
		shift
	elif [[ "$1" == "-g" ]]; then
		X=`echo "$2" | cut -d "$DEL" -f1`
		Y=`echo "$2" | cut -d "$DEL" -f2`
		W=`echo "$2" | cut -d "$DEL" -f3`
		H=`echo "$2" | cut -d "$DEL" -f4`
		shift
	elif [[ "$1" == "-s" ]]; then
		SX=`echo "$2" | cut -d "$DEL" -f1`
		SY=`echo "$2" | cut -d "$DEL" -f2`
		SW=`echo "$2" | cut -d "$DEL" -f3`
		SH=`echo "$2" | cut -d "$DEL" -f4`
		shift
	elif [[ "$1" == "-c" ]]; then
		CX=`echo "$2" | cut -d "$DEL" -f1`
		CY=`echo "$2" | cut -d "$DEL" -f2`
		shift
	elif [[ "$1" == "-m" ]]; then
		MW=`echo "$2" | cut -d "$DEL" -f1`
		MH=`echo "$2" | cut -d "$DEL" -f2`
		shift
	elif [[ "$1" == "-d" ]]; then
		DEL="$2"
		shift
	fi
	shift
done

# Set the file to the only remaining param
FN="$1"
shift
# Test to make sure that the file exists
test -e "$FN" || exit

### SET DEFAULTS IF THEY WERE NOT SET BY ARGS

# If an font width or height was not set then use the default
if [[ "$FW" == "" ]]; then
	FW="$DFW"
fi
if [[ "$FH" == "" ]]; then
	FH="$DFH"
fi

# If an X or Y was not set then set them to 0
if [[ "$X" == "" ]]; then
	X="0";
fi
if [[ "$Y" == "" ]]; then
	Y="0";
fi

# If an x or y is in U
if [[ "$X" == *"U" ]]; then
	X=`echo "$X" | cut -d "U" -f1`
	X=$(($X * $FW))
fi
if [[ "$Y" == *"U" ]]; then
	Y=`echo "$Y" | cut -d "U" -f1`
	Y=$(($Y * $FH))
fi

# Full image  width and height
read IW IH <<< `echo -e "5;$FN" | $W3MIMGDISPLAY`

# If no width or height were specified use the temp
if [[ "$W" == "" ]]; then
	W="$IW";
fi
if [[ "$H" == "" ]]; then
	H="$IH";
fi

# If a width or height is in U
if [[ "$W" == *"U" ]]; then
	W=`echo "$W" | cut -d "U" -f1`
	W=$(($W * $FW))
fi
if [[ "$H" == *"U" ]]; then
	H=`echo "$H" | cut -d "U" -f1`
	H=$(($H * $FH))
fi

# If no sx or sy were set then set them to 0
if [[ "$SX" == "" ]]; then
	SX="0";
fi
if [[ "$SY" == "" ]]; then
	SY="0";
fi

# If an sx or sy is in U
if [[ "$SX" == *"U" ]]; then
	SX=`echo "$SX" | cut -d "U" -f1`
	SX=$(($SX * $FW))
fi
if [[ "$SY" == *"U" ]]; then
	SY=`echo "$SY" | cut -d "U" -f1`
	SY=$(($SY * $FH))
fi

# If no s width or s height were specified use the temp
if [[ "$SW" == "" ]]; then
	SW="$IW";
fi
if [[ "$SH" == "" ]]; then
	SH="$IH";
fi

# If a width or height is in U
if [[ "$SW" == *"U" ]]; then
	SW=`echo "$SW" | cut -d "U" -f1`
	SW=$(($SW * $FW))
fi
if [[ "$SH" == *"U" ]]; then
	SH=`echo "$SH" | cut -d "U" -f1`
	SH=$(($SH * $FH))
fi

# Get column and line count
COL=`tput cols`
LIN=`tput lines`

# TEMPORARY VARIABLES FOR THE ABILITY TO USE BOTH U AND N 
TMW="$MW"
TMH="$MH"

# If no max width or height were set then use the default
if [[ "$TMW" == "" ]]; then
	MW=$((($FW * $COL) - $X + $SX))
fi
if [[ "$TMH" == "" ]]; then
	MH=$((($FH * $(($LIN - 2)) - $Y + $SY))) # substract lines for prompt
fi

# If a width or height is in U
if [[ "$TMW" == *"U"* ]]; then
	MW=`echo "$MW" | cut -d "U" -f1 | cut -d "N" -f1`
	MW=$(($MW * $FW))
fi
if [[ "$TMH" == *"U"* ]]; then
	MH=`echo "$MH" | cut -d "U" -f1 | cut -d "N" -f1`
	MH=$(($MH * $FH))
fi

# If a width or height is in U
if [[ "$TMW" == *"N"* ]]; then
	MW=`echo "$MW" | cut -d "U" -f1 | cut -d "N" -f1`
	MW=$((($FW * $COL) - $X + $SX - $MW))
fi
if [[ "$TMH" == *"N"* ]]; then
	MH=`echo "$MH" | cut -d "U" -f1 | cut -d "N" -f1`
	MH=$((($FH * $(($LIN - 2)) - $Y + $SY - $MH)))
fi

### DONE SETTING DEFAULTS

if [[ "$W" == "R" ]] && [[ "$H" == "R" ]]; then
	W="$IW";
	H="$IH";
elif [[ "$W" == "R" ]]; then
	W=$(($IW * $H))
	W=`printf "%.0f" $(echo "scale=2;$W/$IH" | bc)`
elif [[ "$H" == "R" ]]; then
	H=$(($IH * $W))
	H=`printf "%.0f" $(echo "scale=2;$H/$IW" | bc)`
fi

# If --no-fit was not run then limit size
if [[ "$FIT" == "1" ]]; then
	if test $W -gt $MW; then
		H=$(($H * $MW / $W))
		W=$MW
	fi
	if test $H -gt $MH; then
		W=$(($W * $MH / $H))
		H=$MH
	fi
fi

# If --center was run
if [[ "$CENTER" == "1" ]]; then
	X=$((($MW - $W) / 2))
	Y=$((($MH - $H) / 2))
fi

# Format the command
WC="0;1;$X;$Y;$W;$H;$SX;$SY;$SW;$SH;$FN\n4;\n3;"

# If no cx or cy was set then use the defaults
if [[ "$CX" == "" ]]; then
	CX="0";
fi
if [[ "$CY" == "" ]]; then
	CY=$(($H + $Y - $SY))
	CY=`printf "%.0f" $(echo "scale=2;$CY/$FH" | bc)`
fi

# If cx or cy was set to LAST
if [[ "$CX" == "LAST" ]]; then
	CX="$COL";
fi
if [[ "$CY" == "LAST" ]]; then
	CY="$LIN"
fi


# If --echo was run
if [[ "$ECHO" == "1" ]]; then 
	echo -e "$WC"
else 
	# If --clear was run
	if [[ "$CLEAR" == "1" ]]; then
		clear
	fi
	tput cup $CY $CX
	echo -e "$WC" | $W3MIMGDISPLAY
fi