#!/bin/sh #set -x # -s=3 =4 =8 =16 =32 =64 gives ramdisk of 4MB, 8MB, 16MB (default), ... # -b=2 =3 =4 ... uses /dev/ram2, ram3, ram4 ... # -d=/my_mount_dir defines mount point # -y skips confirmation dialog # -l used by /etc/loadaddon (quits before opening mc) # -L used by /etc/loadaddon (continues after opening mc) # -q quiet unset is_new if [ "${0##*/}" = "newaddon.fbox" -o "${0##*/}" = "createaddon.fbox" ]; then f=none for x in $@; do case ${x##*/} in -*) continue;; *.sqf) f=$x;; *.*) echo "error: only sqf extension allowed": exit 1;; *) f=$x.sqf;; esac break done [ "$f" = "none" ] && echo "usage: ${0##*/} addonname" && exit 1 [ -f "$f" ] && echo "error: $f does already exist" && exit 1 echo "creating empty addon $f" mkdir /tmp/squashfs-root.$PPID cp /etc/empty.sqf $f is_new=1 fi if [ "${0##*/}" = "changeaddon.fbox" -o "${0##*/}" = "openaddon.fbox" -o "$is_new" ]; then for x in $@; do [ "$is_new" ] || f=none case `echo $x |tr [A-Z] [a-z]` in -*) ;; *) [ "$is_new" ] || for f in $x $x.sqf ${x}sqf; do [ -f $f ] && break 2 done ;; esac done if [ "$f" != "none" -a -f "$f" ]; then if [ "$is_new" ]; then echo "x$@" |grep -q "x-q" || echo "calling `dirname $0`/change${f##*.}.fbox `echo $@ |sed "s@$x@$f@"`" `dirname $0`/change${f##*.}.fbox `echo $@ |sed "s@$x@$f@"` else echo "x$@" |grep -q "x-q" || echo "calling ${0%addon.fbox}${f##*.}.fbox `echo $@ |sed "s@$x@$f@"`" ${0%addon.fbox}${f##*.}.fbox `echo $@ |sed "s@$x@$f@"` fi exit 0 else echo "error: file not found: $1 ($1.sqf | ${1}sqf " exit 1 fi fi ramdisksize=16384 # default 16384, except: changeramdisk size is autodetected # (the used ramdisk sizes are listed at line 316) ram="ram2" # default /dev/ram2 ramdir="/ram_tmp" # default /ram_tmp key="n" # default: action has to be confirmed no_mc= # default: use mc and unmount afterwards unset is_ro # set if opening and not changing unset quiet sqmode . /etc/functions while echo $1 |grep -q "^-"; do case $1 in -s=[0-9]*) [ `echo "$1" |grep "^-s=3"` ] && ramdisksize=3072 [ `echo "$1" |grep "^-s=4"` ] && ramdisksize=4096 [ `echo "$1" |grep "^-s=8"` ] && ramdisksize=8192 [ `echo "$1" |grep "^-s=1"` ] && ramdisksize=16384 [ `echo "$1" |grep "^-s=32"` ] && ramdisksize=32768 [ `echo "$1" |grep "^-s=6"` ] && ramdisksize=65536 [ `echo "$1" |grep "^-s"` ] && shift && with_size=1 ;; -b=[0-9]*) ram="ram`echo $1 |sed "s/-b=//"`" shift ;; -m=[1-9]) unset sqmode shift ;; -m=0) sqmode="-nolzma" shift ;; -d=*) ramdir=`echo $1 |sed "s/-d=//"` shift ;; -y) key="y" shift ;; -l) no_mc=1 shift ;; -L) no_mc=2 shift ;; -q) quiet=1 shift ;; *) echo "warning: unknown argument $1" shift ;; esac done [ -b /dev/$ram ] || mknod /dev/$ram b 1 `echo $ram |sed "s/ram//"` [ -f /etc/spb.conf ] && rescue=yes input_1="" use_mc="" extract_type="" archive_type="sqf" if [ "`pwd`" = "/" ]; then archive_dir="/" source_dir="/" else archive_dir="`pwd`/" source_dir="`pwd`/" fi #make absolute paths case $0 in *changesqf.fbox | *opensqf.fbox) input_1="file" if [ "$1" ]; then archive_name=$1 if echo $archive_name |grep -q "^/"; then archive_dir="" elif echo $archive_name |grep -q "^\./"; then archive_name="`echo $archive_name |sed "s@\./@@"`" fi fi ;; esac case $0 in *opensqf.fbox) is_ro=1 use_mc=1 extract_type=${0##*open}; extract_type=${extract_type%.fbox} archive_type=$extract_type [ "$quiet" ] || echo "create a new ramdisk ($ramdisksize kB) mounted on $ramdir" [ "$quiet" ] || echo "and copy the contents of $1 to the ramdisk" ;; *changesqf.fbox) use_mc=1 extract_type=${0##*change}; extract_type=${extract_type%.fbox} archive_type=$extract_type [ "$quiet" ] || echo "Change the contents of ${0##*change} archive $1" ;; *) echo "error: this script must be called by a (symbolic) link named" echo " changeaddon.fbox, newaddon.fbox, createaddon.fbox, openaddon.fbox," echo " opensqf.fbox or changesqf.fbox" exit 1 ;; esac case $0 in *sqf.fbox) EXT=sqf ;; *) unset EXT esac if [ "$input_1" ]; then [ "$quiet" ] || echo "(using a temporary ramdisk ($ramdisksize kB) mounted on /dev/$ram)" if [ "$archive_type" = "sqf" ]; then file_description="${archive_type#tar_} archive (squashfs)" fi fi if [ "$input_1" -a $# = 0 ]; then echo echo "required argument:" case $input_1 in file) echo " filename of the $file_description" ;; esac exit 1 fi echo if [ "$key" != "y" ]; then case $input_1 in file) echo "enter y to change or open $archive_dir$archive_name (y/n)" ;; *) echo "enter y to continue (y/n)" ;; esac echo echo "Warning: any existing ramdisk on /dev/$ram will be deleted." read key else [ "$quiet" ] || echo "Using /dev/$ram;" fi if [ "$key" != "y" ]; then echo "script terminated by user"; exit 1; fi case $input_1 in file) if [ ! -f $archive_dir$archive_name ]; then echo "error: file $archive_dir$archive_name does not exist"; exit 1; fi ;; esac if [ "$no_mc" = "2" ]; then if [ "$archive_type" = "sqf" ]; then sqffile=$archive_dir$archive_name archive_basename=`basename $archive_name |sed "s/.sqf$//" |sed "s/.SQF$//` fi fi if [ "$no_mc" != "2" ]; then if [ "`cat /proc/mounts | grep /dev/$ram`" != "" ]; then umount /dev/$ram; fi if [ $? != 0 ]; then echo "error: umount /dev/$ram failed"; exit 1; fi if [ "$archive_type" = "sqf" ]; then #new ramdisk: if [ "$rescue" ]; then if [ ! -f /etc/ramdisk$ramdisksize.gz ]; then echo "error: /etc/ramdisk$ramdisksize.gz not found"; exit 1; fi gzip -dc /etc/ramdisk$ramdisksize.gz >/dev/$ram else #zeros give better compression dd if=/dev/zero of=/dev/$ram bs=1k count=$ramdisksize #format mke2fs -m 0 -b 1024 /dev/$ram $ramdisksize fi fi [ -d $ramdir ] || mkdir $ramdir if ! mount -t ext2 /dev/$ram $ramdir; then echo "error: mount ramdisk failed"; exit 1; fi [ "$rescue" ] || rmdir $ramdir/lost+found [ "$extract_type" = "sqf" ] && [ -z "$quiet" ] && echo -n "copying files to ramdisk ..." mydir="`pwd`" cd $ramdir case $extract_type in sqf) sqffile=$archive_dir$archive_name unsquashfs-lzma.fbox -dest /tmp/$ramdir.$PPID $sqffile cp -a /tmp/$ramdir.$PPID/* $ramdir && rm -r /tmp/$ramdir.$PPID if [ $? != 0 ]; then echo " error: extracting the archive failed; (ramdisk to small? try $0 -s=64 ?)"; exit 1; fi ;; esac [ "$quiet" ] || echo " done" fi if [ ! "$no_mc" ]; then cd $mydir if [ "$use_mc" ]; then echo "Now the Midnight Commander will be started that the contents of" echo -n "the $file_description (ramdisk in $ramdir) may be " if [ "$is_ro" ]; then echo "viewed (inside a temporary ramdisk)." echo "When Midnight Commander is closed the ramdisk gets removed." else echo "controlled and modified." echo "Quit the Midnight Commander to continue this script." fi sleep 5 mc -c $ramdir ./ if [ "$is_ro" ]; then key=n else sync echo "Enter y to accept the changes (y/n)" key=n read key fi else [ "$quiet" ] || echo "use changeaddon.fbox" [ "$quiet" ] || echo "to check and modify the archive with the Midnight Commander" key=y fi else [ "$no_mc" = "1" ] && exit if [ "$is_ro" ]; then key=n; else key=y; fi fi if [ "$key" = "y" ]; then case $archive_type in sqf) [ "$quiet" ] || echo -n "compressing the $file_description..." if [ $archive_type = sqf ]; then if ! mksquashfs-lzma.fbox $ramdir $sqffile -noappend && chmod 644 $sqffile; then echo " error"; exit 1; fi fi [ "$quiet" ] || echo " done" umount $ramdir && freeramdisk /dev/$ram && rmdir $ramdir && [ -z "$quiet" ] && echo "ramdisk unmounted" ;; esac case $input_1 in file) [ "$quiet" ] || echo "$archive_dir$archive_name has been changed." ;; esac true else umount $ramdir && freeramdisk /dev/$ram && rmdir $ramdir && [ -z "$quiet" ] && echo "ramdisk unmounted" if [ "$is_ro" ]; then true; else false; fi fi