#
# set -x
# C. Ostheimer and <dynamic>: 08-2008
#put user defined post install commands into file cfg.customize 
#maximal size of tar archive with asterisk config files
#which is written to /var/flash/cfg.configs
addon=
app_ver=0.51
script_ver=1.231
cfg_common_req=2.351
addon_bin=

#server=http://192.168.222.10 #or =ftp://user17:secret@1.2.3.4
server=http://www.spblinux.de/fbox.new/0.9.29
#server=http://c2a2b2.com/fbox/0.9.29
# cfg.common and cfg.customize are downloaded from $server and
# firmware version specific files are downloaded from $server$svr26 
# default sub directories are /26, /26-ar7, /26-ur8.

##############################################################################
# Copyright (C) cfg_sqf,  C. Ostheimer and <dynamic>, licensed under
# GNU General Public License version 2 (GPL v2.0)
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details; spblinux.de/fbox.new/gpl-2.0.txt

usage()
{
 echo -e "usage:\n" \
         "*******************************************************************************\n" \
	 " ${0##*/} [-v] [-s alternate_sever ] mount|umount filename.sqf\n" \
         "   mount:     downloads and mounts filename.sqf to ramdisk as Read-Only\n" \
         "   umount:   unmounts filename.sqf and released ramdisk\n" \
         "   -v:          enables verbose mode for debugging purposes\n" \
         "   -s [ftp://[id:secret@]my_ftp_server.com|http://my_http_server.com]\n" \
         "\n" \
         "*******************************************************************************\n"
 exit
}

# command line option parsing
unset verbose forced disable downloaded_latest version_status
[ $# -eq 0 ] && usage
while [ x$1 != x ]; do case $1 in
 -v) verbose=1;;
 -s) echo $2 | grep -q -E "(http|ftp)://.*\..+" || (echo -e "\nParameter error: Wrong \"-s ...\" Option usage!" && usage )
     server="$2";shift;;
 -h | -\? | --help) usage;;
 mount | umount ) 
  if [ $# -eq 2 ];then
   addon=${2%.sqf}
   break
  else
   echo -e "Paramater error: No sqf-file defined!"
   usage
  fi
  ;;
 -*) echo "warning: unknown option $1";;
 *) break
esac; shift; done

# Check version of cfg.common required for installation and download latest version if required
f=cfg.common
cfg_common_script=${0%cfg_*}$f
while true;do
 if [ -x $cfg_common_script ];then
  version_status="`$cfg_common_script cmp_version $cfg_common_req`"
  [ "$verbose" ] && echo $version_status
  if echo $version_status | grep -q "\-> OK";then
   break;
  else
   if ! [ $downloaded_latest ];then
    echo -e "Fatal Error: Cannot locate version v$cfg_common_req of $f file"
    exit 1 
   fi 
  fi 
 fi 
 cfg_common_script=/var/$f
 if ! wget $server/$f -O $cfg_common_script;then
  echo -e "Fatal Error: Couldn't download $f from $server"
  exit 1 
 fi 
 chmod +x $cfg_common_script
 downloaded_latest=1
done 

# Source common functions for installation from cfg.common
. $cfg_common_script

modext=`_fct modext`
svrsub=`_fct svrsub`
export PATH=$PATH:/sbin:/usr/sbin

[ "$modext" = "o" ] && errorexit "Error: In this version of cfg_$addon only kernel 2.6 is supported."
case $1 in         
mount )
 if [ "$INST_DIR" ];then
  if [ -d "$INST_DIR" ]; then
   echo -n "INFORMATION: Using user provided mount-point: $INST_DIR"
   echo "(if this is not correct run: unset INST_DIR)"
  else
   echo "WARNING:     INST_DIR(=$INST_DIR) defined, but not a directory"
   echo "             will default mount to /var/$addon"
   INST_DIR="/var/$addon"
   sleep 10
  fi
 else
  INST_DIR="/var/$addon"
  echo -n "INFORMATION: Using default mount-point: $INST_DIR"
 fi
 [ -d $INST_DIR ] || mkdir $INST_DIR
  ############ COPY SQF-FILE TO RAMDISK AND MOUNT TO /var/$addon ###########
 # get common install scripts / tools / modules from $server$svrsub
 _fct wgetx@$server$svrsub:$INST_DIR:freeramdisk:rd.$modext
 chmod +x $INST_DIR/freeramdisk $INST_DIR/cfg.customize
 _fct rdsk@start
 _fct rdsk@new:ramdev
 echo "$addon.sqf -> $ramdev: "
 _fct wgetx@$server$svrsub:$ramdev:$addon.sqf
 mount $ramdev $INST_DIR || _fct fallback@$addon.sqf
 if mount |grep -q "on $INST_DIR";then
  echo "COMPLETED:   $addon.sqf has been successfully mounted to: $INST_DIR"
 else
  errorexit "Error: failed to download and mount $addon to: $INST_DIR"
 fi
 ;;
umount )
 INST_DIR=${INST_DIR-"/var"}
 if _fct rdsk@get_device:ramdev:$INST_DIR/$addon;then
  umount $INST_DIR/$addon
  _fct rdsk@free:$ramdev
  _fct rdsk@stop
  rm -r $INST_DIR/$addon
 else
  errorexit "ERROR: No ramdisk mounted on $INST_DIR/$addon"
 fi
 ;;
*)
 usage
esac
