#! /bin/sh
#----------------------------------------------------------------------------
# change-url - change download url
#
# Copyright (c) 2001-2003 Frank Meyer <frank@fli4l.de>
#
# Creation:	04.11.2001  fm
# Last Update:  19.07.2003  fm
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Bookmark support by: Michell Schimanski <M.Schimanski@tu-bs.de>
#----------------------------------------------------------------------------
# Variables:
#
# old_url 		- old URL
# new_url		- new URL
# n_his			- number of history entries
# n_bm			- number of bookmark entries
# url_his_[anz_his] 	- array of history entries
# url_bm_[anz_bm]	- array of bookmark entries
# history, bookmarks	- strings for output
# in1, in2, in3 	- input-Strings
# change		- change status
# i,j			- counters
#----------------------------------------------------------------------------

cd /tmp

clrhome
colecho "Change URL for download" gn
echo
echo
old_url=`cat /var/install/url`

echo "  Current URL: $old_url"
echo

n_his=0
i=0

if [ -f /var/install/url-history ]
then
    while read line
    do
	i=`expr $i + 1`
	eval url_his_$i='"$line"'

	if [ $i = 1 ]
	then
	    echo "  History   $i: $line"
	else
	    echo "            $i: $line"
	fi
    done < /var/install/url-history
    n_his=$i
fi

i=0
n_bm=0

if [ -f /var/install/url-bookmarks ]
then
    while read line
    do
	i=`expr $i + 1`
	eval url_bm_$i='"$line"'
    done < /var/install/url-bookmarks
fi

n_bm=$i

echo
if [ $n_his -gt 0 ]
then
    history=", history: 1-$n_his"
fi

if [ $n_bm -gt 0 ]
then
    bookmarks=", bookmarks: b"
fi

echo "  Please enter new URL (ENTER=no change$history$bookmarks): "
echo
echo -e "  \c"
read in1
echo

change=1

case "$in1"
in
    "") 
	echo "URL not Changed"
	new_url=$old_url
	change=0
	;;

    [1-$n_his])
	eval new_url='$url_his_'$in1
	echo "New URL is: $new_url"
	;;

    b)  
	echo "Available bookmarks:"
	i=0
	while [ $i -lt $n_bm ]
	do
	    i=`expr $i + 1`
	    eval dummy='$url_bm_'$i
	    echo -e "$i: $dummy"
	done

	echo
	echo -e "Choose bookmark (ENTER=no change, a: Add, d: Del): \c"
	read in2
	case "$in2"
	in
	    "") 
		echo "URL not Changed"
		new_url=$old_url
		change=0
		;;

	    # [1-$n_bm])    # fm: this works only for single characters
	    [1-9]*)
		eval new_url='$url_bm_'$in2

		if [ "$new_url" = "" ]
		then
		    colecho "No valid bookmark" br x br
		    new_url=$old_url
		    change=0
		else
		    echo "New URL is: $new_url"
		fi
		;;

	    a)	
		echo
		echo -e "Enter new Bookmark (ENTER=cancel, c=Current URL, 1-$n_his=Take from history): \c"
		read in3
		case "$in3"
		in
		    "")
			;;
		    "c")
			echo $old_url >> /var/install/url-bookmarks
			echo "Added Bookmark: $old_url"
			;;
		    [1-$n_his])
			eval dummy='$url_his_'$in3
			echo $dummy >> /var/install/url-bookmarks
			echo "Added Bookmark: $dummy"
			;;
		    *)	
			echo $in3 >> /var/install/url-bookmarks
			echo "Added Bookmark: $in3"
			;;
		esac
		echo "URL not Changed"
		new_url=$old_url
		change=0
		;;

	    d)
		echo
		echo -e "Enter bookmark to delete (ENTER=cancel): \c"
		read in3
		if [ "$in3" != "" ]
		then
		    rm -f /var/install/url-bookmarks
		    eval url_bm_$in3=""
		    i=0
		    while [ $i -lt $n_bm ]
		    do
			i=`expr $i + 1`
			eval dummy='$url_bm_'$i
			if [ "$dummy" != "" ] 
			then
			    echo -e "$dummy" >> /var/install/url-bookmarks
			fi
		    done
		    echo "Deleted bookmark: $in3"
		fi
		echo "URL not Changed"
		new_url=$old_url
		change=0
		;;
    
	    *)
		echo "Error - no valid input"
		echo "URL not Changed"
		new_url=$old_url
		change=0
		;;
	esac
	;;
    *)
	new_url=$in1
	echo "New URL is: $new_url"
	
esac

echo $new_url >/var/install/url

if grep "^$new_url$" /var/install/url-history >/dev/null
then
    if [ $change = 1 ]
    then
    (
	echo $new_url
	i=1
	if [ $n_his -ge 5 ]
	then
	    n_his=4
	fi
	
	while [ $i -le $n_his ]
	do
	    eval a='$url_his_'$i
	    echo $a
	    i=`expr $i + 1`
	done
    ) >/var/install/url-history
    fi
fi

echo
/var/install/bin/anykey
exit 0