#!/usr/bin/env bash
# g: jebug29's Gopher Shortcut Tool
# Written by jebug29 on SDF
# Created: 04 Jul 2018
# Updated: 26 Mar 2019

VERSION="0.3"
DESTINATION=$1
CLIENT=$2

use_defaults=1
domain="gopher.club"
client="gopher"
item_type=1
overwrite_client="1"
prefix="users/"

#check for arguments
if [ $# -eq 0 ];then
	echo "
g $VERSION by jebug29 (c) 2019
Usage: g (username|domain.ext/) [client] [itemtype]
For more information, type "g -?"
"
	exit 1
fi

#check if user needs help
if [[ $1 == "--about" || $1 == "--help" || $1 == "-?" ]];then
echo "
g: jebug29's Gopher Shortcut Tool (c) 2019 | Version:  $VERSION
==========================================================
Usage: g (username|domain.ext/) [client] [itemtype]
OR:    g --default-config (outputs default .grc to stdout)

Thank you for  using  g - jebug29's  gopher shortcut tool!
This  program was written because  I  wanted a  better way
to get  to a  user's  gopher hole without having to either
navigate through  SDF,  type the  full  link,  or create a
bunch of aliases in my .profile (which is basically what's
already happened).

You  can  reach  me via  e-mail at jebug29@sdf.org  or  on
Mastodon at jebug29@mastodon.sdf.org.

Thank you very much for using my software! Goodbye!
"
exit 0
fi

output_default_config () {
echo "# Thank you for using g!
# If  you have any  issues, feel  free to  contact me by e-mail
# at jebug29@sdf.org or on Mastodon (@jebug29@mastodon.sdf.org)

# g will use the following options in a command like this:
# client gopher://domain/item_type/prefixUsername

# The default client is gopher. Two other good clients are
# Lynx and VF-1.

# To output the defaults, run "g --default-config"
# For example configurations, run "g --example-config"

# A system-wide config can be placed in /etc/g

#client=$client
#domain=$domain
#item_type=$item_type
#prefix=$prefix

# If set to 1, overwrites client in dotfile when
# specified in the commandline. This is the default behavior
#overwrite_client=$overwrite_client

# You can specify additional command line arguments for your
# client using client_args_CLIENT
#client_args_gopher=
#client_args_lynx=
#client_args_vf1=
"
}

output_example_config () {
echo "# example config for SDF (gopher.club)
domain=gopher.club
item_type=1
prefix=users/

# example config for Cosmic Voyage
domain=cosmic.voyage
item_type=1
prefix=ships/

# example config for tilde.team
domain=tilde.team
item_type=1
prefix=~
"
}

if [ "$1" = "--default-config" ];then
	echo "### These settings will be read from /etc/g and ~/.grc"
	output_default_config
	exit 0
fi

if [ "$1" = "--example-config" ];then
	output_example_config
	exit 0
fi

if [ ! -f "$HOME/.grc" ];then
	output_default_config > "$HOME/.grc"
fi

if [[ $DESTINATION = *"."* && $DESTINATION = *"/"* ]];then
	use_defaults=0
fi

#checks for /etc/g .grc file and reads it
#saves line numbers in case an option should be overwritten in
#the config file
read_config() {
if [ -f "$1" ]; then
	line_num=0
	client_num=-1
	while read -r line || [ -n "$line" ]; do
		let line_num++
		IFS='=' lineop=($line)
		line_var=${lineop[1]}
		for i in "${lineop[@]:2}"; do
			IFS=''
			line_var="$line_var=$i"
		done
		case "${lineop[0]}" in
		client)
			client=$line_var
			client_num=$line_num
			;;
		client_args_$client)
			client_args_x[0]=$line_var
			;;
		cleint_args_$CLIENT)
			client_args_x[1]=$line_var
			;;
		domain)
			domain=$line_var
			;;
		item_type)
			item_type=$line_var
			;;
		overwrite_client)
			overwrite_client=$line_var
			;;
		prefix)
			prefix=$line_var
			;;
		esac
	done < "$1"
fi
}
if [ -f "/etc/g" ];then
	read_config "/etc/g"
fi

if [ -f "$HOME/.grc" ];then
	read_config "$HOME/.grc"
fi

#sets the client as default, if specified
#will write this to the appropriate line in .grc
if [ ! -z "$CLIENT" ];then
	if [ "$overwrite_client" -eq 1 ]; then
		if [ "$client_num" -ge 0 ]; then
			sed -i "${client_num}s/.*/client=$CLIENT/" "$HOME/.grc"
		else
			echo "client=$CLIENT" >> "$HOME/.grc"
		fi
	fi
	client="$CLIENT"
	client_args=${client_args_x[1]}
else
	client_args=${client_args_x[0]}
fi

#checks for a single character in $3 for the specified item type
#will use $2 if $3 is not specified and $2 is a single char
if [ ! -z "$3" ] && [ "${#3}" -eq 1 ];then
	item_type=$3
elif [ ! -z "$2" ] && [ "${#2}" -eq 1 ];then
	item_type=$2
fi

if [ "$use_defaults" -eq 1 ]; then
	$client $client_args "gopher://$domain/$item_type/$prefix$DESTINATION"
else
	$client $client_args "gopher://$DESTINATION"
fi

#like jmcgann's School Thoughts, there's nothing to see here. Carry on.
#Thank you for using my tool! -Jesse (jebug29)
#The password is always password
