#! /bin/sh
#----------------------------------------------------------------------------
# change-url - change download url
#
# Creation:     2001-11-04  fm
# Last Update:  $Id: change-url 10542 2007-04-25 19:52:07Z hbfl $
#
# Copyright (c) 2001-2007 the eisfair team, team(at)eisfair(dot)org
#
# 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.
#----------------------------------------------------------------------------

# include eislib
. /var/install/include/eislib

# standard_url: standard eisfair URL
standard_url='http://download.eisfair.org/packages/eis-list.txt'

# current url file
current_url_file='/var/install/url'

# url history file
url_history_file='/var/install/url-history'

# max_his: maximum number of elements in history
max_his=18

#
# ask_url
# =======
#
# Ask for new URL
# Validate URL
#
ask_url()
{
   valid=0
   while [ "$valid" = 0 ]
   do
      /var/install/bin/ask "URL:" "" "*" > /tmp/ask.$$
      rc=$?
      input_url=`cat /tmp/ask.$$`
      rm -f /tmp/ask.$$
      if [ $rc = 255 ]
      then
         exit 1
      fi

      if [ "$input_url" = '' ]
      then
         input_url=$old_url
         return
      fi

      case "$input_url" in
      http://*)   valid=1;;
      https://*)  valid=1;;
      ftp://*)    valid=1;;
      file://*)   valid=1;;
      *)          valid=0;;
      esac

      if [ "$valid" = 0 ]
      then
         mecho -error "Wrong URL! Press ENTER to exit loop"
      fi
   done
}


#
# update_url_history
# ==================
#
# Update url history file
#
update_url_history()
{
   if ! grep -q "^$new_url$" $url_history_file &&
      ! grep -q "^$new_url # " $url_history_file
   then
      if [ $change = 1 ]
      then
      (
         echo $new_url
         i=1
         if [ $n_his -ge $max_his ]
         then
            n_his=`expr $max_his - 1`
         fi

         while [ $i -le $n_his ]
         do
            eval a='$url_his_'$i
            eval b='$url_his_'$i'_comment'
            if [ "$b" = "" ]
            then 
               echo "$a"
            else
               echo "$a # $b"
            fi
            i=`expr $i + 1`
         done
      ) > $url_history_file
      fi
   fi
}

#
# read_and_display_url_history
# ============================
#
# Read url history file into variables url_his_#
# Set n_his
# Display history
#
read_and_display_url_history()
{
   n_his=0
   i=0

   if [ -f $url_history_file ]
   then
      while read line
      do
         i=`expr $i + 1`
         url_his="$(echo "$line" | sed -e 's/ #.*$//g')"
         eval 'url_his_'$i='"$url_his"'
         url_his_comment="$(echo "$line" | sed -e 's/^.* # //g')"
         eval 'url_his_'$i'_comment'='"$url_his_comment"'

         if [ $i = 1 ]
         then
            mecho "History   $i: $url_his"
         else
            if [ $i -gt 9 ]
            then
               mecho "         $i: $url_his"
            else
               mecho "          $i: $url_his"
            fi
         fi
      done < $url_history_file
      n_his=$i
   fi
}

cd /tmp

clrhome
mecho -info "Change URL for download"
mecho
old_url=`cat $current_url_file 2>/dev/null`

if [ "$old_url" = '' ]
then
   mecho -n "Current URL: "
   mecho -error "-- Error: URL is not set --"
else
   mecho "Current URL: $old_url"
fi
mecho

read_and_display_url_history

mecho

if [ $n_his -gt 0 ]
then
   /var/install/bin/ask "URL" "" "^$=no change"                 \
                                 "1-$n_his=from history"        \
                                 "s=standard"                   \
                                 "n=new" > /tmp/ask.$$
else
   /var/install/bin/ask "URL" "" "^$=no change"                 \
                                  "s=standard"                  \
                                  "n=new" > /tmp/ask.$$
fi

rc=$?
in1=`cat /tmp/ask.$$`
rm -f /tmp/ask.$$
if [ $rc = 255 ]
   then
   exit 1
fi

case "$in1" in
"")
   new_url=$old_url
   ;;
[1-9]|[1-9][0-9])
   eval new_url='$url_his_'$in1
   ;;
s|S)
   new_url="$standard_url"
   ;;
n)
   ask_url
   new_url=$input_url
   ;;
esac

if [ "$new_url" = '' ]
then
   mecho -warn "Replacing empty URL with eisfair standard URL"
   new_url="$standard_url"
fi

if [ "$new_url" = "$old_url" ]
then
   mecho "URL not changed"
   change=0
else
   mecho "New URL is: $new_url"
   change=1
fi

echo $new_url > $current_url_file
update_url_history

mecho
/var/install/bin/anykey

exit 0